github.com/igggame/nebulas-go@v2.1.0+incompatible/nebtestkit/cases/stress/stress.test.single.js (about)

     1  'use strict';
     2  
     3  var HttpRequest = require("../../node-request");
     4  var Wallet = require('../../../cmd/console/neb.js/lib/wallet.js');
     5  var neb = new Wallet.Neb(new HttpRequest("http://127.0.0.1:8685"));
     6  
     7  
     8  var sleep = require("system-sleep");
     9  
    10  const AddressNumber = 200;
    11  const SendTimes = 40;
    12  var lastnonce = 0;
    13  
    14  var chainID = 100;
    15  
    16  // var master = Wallet.Account.NewAccount();
    17  var from = new Wallet.Account("a6e5eb290e1438fce79f5cb8774a72621637c2c9654c8b2525ed1d7e4e73653f");
    18  
    19  
    20  neb.api.getAccountState(from.getAddressString()).then(function (resp) {
    21      console.log("master accountState resp:" + JSON.stringify(resp));
    22      lastnonce = parseInt(resp.nonce);
    23      console.log("lastnonce:", lastnonce);
    24  });
    25  
    26  sleep(10000);
    27  
    28  var accountArray = new Array();
    29  for (var i = 0; i < AddressNumber; i++) {
    30      var account = Wallet.Account.NewAccount();
    31      var hash = account.getAddressString();
    32      accountArray.push(hash);
    33  }
    34  
    35  var nonce = lastnonce;
    36  var t1 = new Date().getTime();
    37  for (var j = 0; j < AddressNumber; j++) {
    38      sendTransaction(0, nonce, accountArray[j]);
    39      nonce = nonce + SendTimes;
    40  }
    41  
    42  sleep(2000);
    43  
    44  var interval = setInterval(function () {
    45      // for (var i = 0; i < AddressNumber; i++) {
    46      //     neb.api.getAccountState(accountArray[i]).then(function (resp) {
    47      //         console.log("accountState resp:" + JSON.stringify(resp));
    48      //     });
    49      // }
    50      neb.api.getAccountState(from.getAddressString()).then(function (resp) {
    51          console.log("master accountState resp:" + JSON.stringify(resp));
    52          if (resp.nonce == lastnonce + AddressNumber * SendTimes) {
    53              var t2 = new Date().getTime();
    54              console.log("Time consumption:" + (t2 - t1) / 1000);
    55              console.log("Tps:" + AddressNumber * SendTimes * 1000 / (t2 - t1));
    56              clearInterval(interval);
    57          }
    58      });
    59  
    60  }, 2000);
    61  
    62  function sendTransaction(sendtimes, nonce, address) {
    63      if (sendtimes < SendTimes) {
    64          var transaction = new Wallet.Transaction(chainID, from, address, "1", ++nonce);
    65          transaction.signTransaction();
    66          var rawTx = transaction.toProtoString();
    67          neb.api.sendRawTransaction(rawTx).then(function (resp) {
    68              console.log("send raw transaction resp:" + JSON.stringify(resp));
    69              sendtimes++;
    70              if (resp.txhash) {
    71                  sendTransaction(sendtimes, nonce, address);
    72              }
    73          });
    74      }
    75  
    76  }