Advertisement
Guest User

Untitled

a guest
Mar 10th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. const EnergyToken = artifacts.require("EnergyToken");
  3. const assert = require('assert');
  4. const truffleAssert = require('truffle-assertions');
  5. const { performance } = require('perf_hooks');
  6.  
  7. contract("Energy Heavy Load Test", async accounts => {
  8.    
  9.     const transactionPerNode = 400;
  10.     const totalTransactions = 2000;
  11.  
  12.    
  13.  
  14.     let submissionTimes = [];
  15.     let latencies = [];
  16.     let startTime = 0;
  17.     let finishTime = 0;
  18.    
  19.     //Initialize confiramtion times array with 0s
  20.     for(let cnt = 0; cnt< transactionPerNode; cnt++){
  21.         latencies[cnt] = 0;
  22.     };
  23.     let contract;
  24.     let i = 0;
  25.  
  26.    
  27.  
  28.     //Function for rounding numbers
  29.     function round(value, precision) {
  30.     var multiplier = Math.pow(10, precision || 0);
  31.     return Math.round(value * multiplier) / multiplier;
  32.    
  33.     }
  34.    
  35.     //executed before each test
  36.     before(async () => {
  37.        
  38.         contract =  await EnergyToken.at("0x5BbD383bD43aC3896B86207eFe88cf0628ad06F0");
  39.        
  40.        
  41.     });
  42.    
  43.     //executed after each test
  44.     after(async () => {
  45.        
  46.     });
  47.    
  48.     //Test mining time of $numberOfTransactions transaction calls on the EnergyToken smart contract's transfer() method
  49.     it("send " + numberOfTransactions + " transactions of 1 EnergyToken from account 0 to account 1", (done) => {
  50.        
  51.     let subscription = contract.Transfer(function(error, event){
  52.         if(error){
  53.         console.log(error, 'error');
  54.         }
  55.         else{
  56.         // if(event.returnValues.from == accounts[0] & event.returnValues.to == accounts[1]){
  57.             i++;
  58.             latencies[event.returnValues.value-1] = performance.now() - submissionTimes[event.returnValues.value - 1];
  59.             if(i == 1)
  60.             startTime = performance.now();
  61.            
  62.             if( i == totalTransactions){
  63.    
  64.                 finishTime = performance.now();
  65.                 let timeInSeconds = round((finishTime - startTime)/1000,2);
  66.                 console.log("Transactions confirmed!" + " It took " + timeInSeconds + " seconds to confirm " +    totalTransactions + " transactions - " + round(totalTransactions/timeInSeconds,2) + " tps" );
  67.                 let averageLatency = 0;
  68.                 for(let cnt = 0; cnt <totalTransactions; cnt++){
  69.                     averageLatency = latencies[cnt];
  70.                 }
  71.                 console.log("Average latency for the transactions is: " + round(averageLatency/totalTransactions,2) + "ms");
  72.                 done();
  73.         //  }
  74.          }
  75.         }
  76.         });
  77.         console.log("Sending transactions...");
  78.               for (let j = 1; j <= totalTransactions; j++) {
  79.                     contract.transfer(accounts[1],j,{from:accounts[0],gasPrice: web3.utils.toWei("1", "gwei")});
  80.                     submissionTimes.push(performance.now());
  81.                 }      
  82.         //subscription = null;
  83.            
  84.            
  85.     });
  86.  
  87.    
  88. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement