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

     1  'use strict';
     2  
     3  var Wallet = require('nebulas');
     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 = 8000;
    17      SendTimes = 1;
    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 if (env == "mariana") {
    69      neb.setRequest(new HttpRequest("http://127.0.0.1:9685")); //https://testnet.nebulas.io
    70      ChainID = 1111;
    71      from = new Wallet.Account("830ccbac2029b880eb07aa9a19c65ce6dad41702d409771eada791d6a6a83a1e");
    72      nodes.push("http://127.0.0.1:9685");
    73  } else {
    74      console.log("please input correct env local testneb1 testneb2")
    75      return;
    76  }
    77  
    78  neb.api.getAccountState(from.getAddressString()).then(function (resp) {
    79      console.log("master accountState resp:" + JSON.stringify(resp));
    80      lastnonce = parseInt(resp.nonce);
    81      console.log("lastnonce:", lastnonce);
    82  
    83      claimTokens(lastnonce);
    84  });
    85  
    86  function claimTokens(nonce) {
    87      accountArray = new Array();
    88      for (var i = 0; i < AddressNumber; i++) {
    89          var account = Wallet.Account.NewAccount();
    90          accountArray.push(account);
    91  
    92          sendTransaction(0, 1, from, account, "1000000000000000", ++nonce);
    93  
    94          sleep(10);
    95      }
    96  
    97      checkClaimTokens();
    98  }
    99  
   100  function sendTransaction(index, totalTimes, from, to, value, nonce) {
   101      if (index < totalTimes) {
   102          var transaction = new Wallet.Transaction(ChainID, from, to, value, nonce);
   103          transaction.signTransaction();
   104          var rawTx = transaction.toProtoString();
   105          neb.api.sendRawTransaction(rawTx).then(function (resp) {
   106              console.log("send raw transaction resp:" + JSON.stringify(resp));
   107              if (resp.txhash) {
   108                  sendTransaction(++index, totalTimes, from, to, value, ++nonce);
   109              }
   110          });
   111      }
   112  }
   113  
   114  function checkClaimTokens() {
   115      var interval = setInterval(function () {
   116          neb.api.getAccountState(from.getAddressString()).then(function (resp) {
   117              console.log("master accountState resp:" + JSON.stringify(resp));
   118              if (resp.nonce >= lastnonce + AddressNumber) {
   119                  clearInterval(interval);
   120  
   121                  sendTransactionsForTps();
   122              }
   123          });
   124  
   125      }, 2000);
   126  }
   127  
   128  function sendTransactionsForTps() {
   129  
   130      console.log("start tps transaction sending...");
   131  
   132      startTime = new Date().getTime();
   133  
   134      for (var i = 0; i < AddressNumber; i++) {
   135  
   136          var node = nodes[i % nodes.length];
   137          neb.setRequest(new HttpRequest(node));
   138  
   139          sendTransaction(0, SendTimes, accountArray[i], to, "1", 1);
   140          sleep(10);
   141      }
   142  
   143      checkTps();
   144  }
   145  
   146  function checkTps() {
   147      var interval = setInterval(function () {
   148          neb.api.getAccountState(to.getAddressString()).then(function (resp) {
   149              console.log("to address state:" + JSON.stringify(resp));
   150              if (resp.balance >= (AddressNumber * SendTimes) * 2 / 3) {
   151                  clearInterval(interval);
   152  
   153                  var endTime = new Date().getTime();
   154  
   155                  console.log("====================");
   156                  console.log("env is ", env);
   157                  console.log("concurrency number is ", AddressNumber);
   158                  console.log("total number is ", AddressNumber * SendTimes);
   159                  console.log("tps is: ", parseInt(resp.balance) / ((endTime - startTime) / 1000));
   160                  console.log("====================")
   161              }
   162          });
   163  
   164      }, 1000);
   165  }