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

     1  'use strict';
     2  
     3  var Wallet = require('../../../cmd/console/neb.js/lib/wallet.js');
     4  var HttpRequest = require("../../node-request");
     5  var sleep = require("system-sleep");
     6  
     7  var env; // local testneb1 testneb2
     8  var AddressNumber;
     9  var SendTimes;
    10  
    11  var args = process.argv.splice(2);
    12  
    13  if (args.length != 3) {
    14      // give default config
    15      env = "local";
    16      AddressNumber = 4;
    17      SendTimes = 5000;
    18  } else {
    19      env = args[0]; // local testneb1 testneb2
    20  
    21      AddressNumber = parseInt(args[1]);
    22      SendTimes = parseInt(args[2]);
    23  }
    24  
    25  if (AddressNumber <= 0 || SendTimes <= 0) {
    26  
    27      console.log("please input correct AddressNumber and SendTimes");
    28      return;
    29  }
    30  
    31  var Neb = Wallet.Neb;
    32  var neb = new Neb();
    33  
    34  var ChainID;
    35  var from;
    36  var accountArray;
    37  var to = Wallet.Account.NewAccount();
    38  var lastnonce = 0;
    39  
    40  // statics for tps check start time.
    41  var startTime;
    42  
    43  var nodes = new Array();
    44  
    45  //local
    46  if (env == 'local') {
    47      neb.setRequest(new HttpRequest("http://127.0.0.1:8685")); //https://testnet.nebulas.io
    48      ChainID = 100;
    49      from = new Wallet.Account("a6e5eb290e1438fce79f5cb8774a72621637c2c9654c8b2525ed1d7e4e73653f");
    50      nodes.push("http://127.0.0.1:8685");
    51  } else if (env == 'testneb1') {
    52      neb.setRequest(new HttpRequest("http://35.182.48.19:8685"));
    53      ChainID = 1001;
    54      from = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3");
    55      nodes.push("http://35.182.48.19:8685");
    56      nodes.push("http://13.57.245.249:8685");
    57      nodes.push("http://54.219.151.126:8685");
    58      nodes.push("http://18.218.165.90:8685");
    59      nodes.push("http://18.219.28.97:8685");
    60      nodes.push("http://13.58.44.3:8685");
    61      nodes.push("http://35.177.214.138:8685");
    62      nodes.push("http://35.176.94.224:8685");
    63  } else if (env == "testneb2") {
    64      neb.setRequest(new HttpRequest("http://34.205.26.12:8685"));
    65      ChainID = 1002;
    66      from = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3");
    67      nodes.push("http://34.205.26.12:8685");
    68  } else {
    69      console.log("please input correct env local testneb1 testneb2")
    70      return;
    71  }
    72  
    73  neb.api.getAccountState(from.getAddressString()).then(function (resp) {
    74      console.log("master accountState resp:" + JSON.stringify(resp));
    75      lastnonce = parseInt(resp.nonce);
    76      console.log("lastnonce:", lastnonce);
    77  
    78      claimTokens(lastnonce);
    79  });
    80  
    81  function claimTokens(nonce) {
    82      accountArray = new Array();
    83      for (var i = 0; i < AddressNumber; i++) {
    84          var account = Wallet.Account.NewAccount();
    85          accountArray.push(account);
    86          sendTransaction(0, 1, from, account, "1000000000000000", ++nonce);
    87      }
    88  
    89      checkClaimTokens();
    90  }
    91  
    92  function sendTransaction(index, totalTimes, from, to, value, nonce) {
    93      if (index < totalTimes) {
    94          var transaction = new Wallet.Transaction(ChainID, from, to, value, nonce);
    95          transaction.signTransaction();
    96          var rawTx = transaction.toProtoString();
    97          neb.api.sendRawTransaction(rawTx).then(function (resp) {
    98              console.log("send raw transaction resp:" + JSON.stringify(resp));
    99              if (resp.txhash) {
   100                  sendTransaction(++index, totalTimes, from, to, value, ++nonce);
   101              }
   102          });
   103      }
   104  }
   105  
   106  function checkClaimTokens() {
   107      var interval = setInterval(function () {
   108          neb.api.getAccountState(from.getAddressString()).then(function (resp) {
   109              console.log("master accountState resp:" + JSON.stringify(resp));
   110              if (resp.nonce >= lastnonce + AddressNumber) {
   111                  clearInterval(interval);
   112  
   113                  sendTransactionsForTps();
   114              }
   115          });
   116  
   117      }, 2000);
   118  }
   119  
   120  function sendTransactionsForTps() {
   121  
   122      console.log("start tps transaction sending...");
   123  
   124      startTime = new Date().getTime();
   125  
   126      for (var i = 0; i < AddressNumber; i++) {
   127  
   128          var node = nodes[i % nodes.length];
   129          neb.setRequest(new HttpRequest(node));
   130  
   131          sendTransaction(0, SendTimes, accountArray[i], accountArray[i], "1", 1);
   132          sleep(10);
   133      }
   134  
   135      checkTps();
   136  }
   137  
   138  function checkTps() {
   139      var finish = 0
   140      var interval = setInterval(function () {
   141          for (var i = 0; i < AddressNumber; i++) {
   142              neb.api.getAccountState(accountArray[i].getAddressString()).then(function (resp) {
   143                  console.log("to address state:" + JSON.stringify(resp));
   144                  if (resp.nonce >= SendTimes) {
   145                      finish++;
   146                      if (finish == AddressNumber) {
   147                          clearInterval(interval);
   148  
   149                          var endTime = new Date().getTime();
   150                          console.log("====================");
   151                          console.log("env is ", env);
   152                          console.log("concurrency number is ", AddressNumber);
   153                          console.log("total number is ", AddressNumber * SendTimes);
   154                          console.log("tps is: ", (AddressNumber * SendTimes) / ((endTime - startTime) / 1000));
   155                          console.log("====================")
   156                      }
   157                  }
   158              });
   159          }
   160  
   161      }, 1000);
   162  }