Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const EnergyToken = artifacts.require("EnergyToken");
- const assert = require('assert');
- const truffleAssert = require('truffle-assertions');
- const { performance } = require('perf_hooks');
- contract("Energy Heavy Load Test", async accounts => {
- const transactionPerNode = 400;
- const totalTransactions = 2000;
- let submissionTimes = [];
- let latencies = [];
- let startTime = 0;
- let finishTime = 0;
- //Initialize confiramtion times array with 0s
- for(let cnt = 0; cnt< transactionPerNode; cnt++){
- latencies[cnt] = 0;
- };
- let contract;
- let i = 0;
- //Function for rounding numbers
- function round(value, precision) {
- var multiplier = Math.pow(10, precision || 0);
- return Math.round(value * multiplier) / multiplier;
- }
- //executed before each test
- before(async () => {
- contract = await EnergyToken.at("0x5BbD383bD43aC3896B86207eFe88cf0628ad06F0");
- });
- //executed after each test
- after(async () => {
- });
- //Test mining time of $numberOfTransactions transaction calls on the EnergyToken smart contract's transfer() method
- it("send " + numberOfTransactions + " transactions of 1 EnergyToken from account 0 to account 1", (done) => {
- let subscription = contract.Transfer(function(error, event){
- if(error){
- console.log(error, 'error');
- }
- else{
- // if(event.returnValues.from == accounts[0] & event.returnValues.to == accounts[1]){
- i++;
- latencies[event.returnValues.value-1] = performance.now() - submissionTimes[event.returnValues.value - 1];
- if(i == 1)
- startTime = performance.now();
- if( i == totalTransactions){
- finishTime = performance.now();
- let timeInSeconds = round((finishTime - startTime)/1000,2);
- console.log("Transactions confirmed!" + " It took " + timeInSeconds + " seconds to confirm " + totalTransactions + " transactions - " + round(totalTransactions/timeInSeconds,2) + " tps" );
- let averageLatency = 0;
- for(let cnt = 0; cnt <totalTransactions; cnt++){
- averageLatency = latencies[cnt];
- }
- console.log("Average latency for the transactions is: " + round(averageLatency/totalTransactions,2) + "ms");
- done();
- // }
- }
- }
- });
- console.log("Sending transactions...");
- for (let j = 1; j <= totalTransactions; j++) {
- contract.transfer(accounts[1],j,{from:accounts[0],gasPrice: web3.utils.toWei("1", "gwei")});
- submissionTimes.push(performance.now());
- }
- //subscription = null;
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement