github.com/igggame/nebulas-go@v2.1.0+incompatible/nebtestkit/cases/stress/muti-nvm.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  var FS = require("fs");
     7  var TestNetConfig = require("../testnet_config.js");
     8  var expect = require('chai').expect;
     9  
    10  var env; // local testneb1 testneb2
    11  var AddressNumber;
    12  var SendTimes;
    13  var ContractNumber;
    14  var testType //1:binary 2:合约调用 3:多级合约调用
    15  
    16  var args = process.argv.splice(2);
    17  
    18  if (args.length != 5) {
    19      // give default config
    20      env = "testneb2";
    21      AddressNumber = 4000; 
    22      SendTimes = 10;
    23      ContractNumber = 20;
    24      testType = 1;
    25  } else {
    26      env = args[0]; // local testneb1 testneb2
    27  
    28      AddressNumber = parseInt(args[1]);
    29      SendTimes = parseInt(args[2]);
    30      ContractNumber = parseInt(args[3]);
    31      testType = parseInt(args[4]);
    32  }
    33  var testNetConfig = new TestNetConfig(env);
    34  
    35  if (AddressNumber <= 0 || SendTimes <= 0) {
    36  
    37      console.log("please input correct AddressNumber and SendTimes");
    38      return;
    39  }
    40  var Neb = Wallet.Neb;
    41  var Transaction = Wallet.Transaction;
    42  var from;
    43  var accountArray;
    44  var to = Wallet.Account.NewAccount();
    45  var toAddresses;
    46  var nonce = 0;
    47  
    48  // statics for tps check start time.
    49  var startTime;
    50  
    51  var nodes = new Array();
    52  
    53  var neb = new Neb();
    54  var ChainID = testNetConfig.ChainId;
    55  var sourceAccount = testNetConfig.sourceAccount;
    56  var coinbase = testNetConfig.coinbase;
    57  var apiEndPoint = testNetConfig.apiEndPoint;
    58  neb.setRequest(new HttpRequest(apiEndPoint));
    59  
    60  var calleeContractSrc = FS.readFileSync("nf/nvm/test/kvStore.js", "utf-8");
    61  var callerContractSrc = FS.readFileSync("nf/nvm/test/kvStoreProxy.js", "utf-8");
    62  var deployContractSrc = FS.readFileSync("nebtestkit/cases/stress/game.ts", "utf-8");
    63  var calleeContractAddresses;
    64  var callerContractAddresses;
    65  var to = Wallet.Account.NewAccount();
    66  
    67  var maxCheckTime = 60;
    68  var checkTimes = 0;
    69  var beginCheckTime;
    70  
    71  function checkTransaction(hash, callback) {
    72      if (checkTimes === 0) {
    73          beginCheckTime = new Date().getTime();
    74      }
    75      checkTimes += 1;
    76      if (checkTimes > maxCheckTime) {
    77          console.log("check tx receipt timeout:" + hash);
    78          checkTimes = 0;
    79          callback();
    80          return;
    81      }
    82  
    83      neb.api.getTransactionReceipt(hash).then(function (resp) {
    84  
    85          console.log("tx receipt status:" + resp.status);
    86          if (resp.status === 2) {
    87              setTimeout(function () {
    88                  checkTransaction(hash, callback);
    89              }, 2000);
    90          } else {
    91              checkTimes = 0;
    92              var endCheckTime = new Date().getTime();
    93              console.log("check tx time: : " + (endCheckTime - beginCheckTime) / 1000);
    94              callback(resp);
    95          }
    96      }).catch(function (err) {
    97          console.log("fail to get tx receipt hash: " + hash);
    98          console.log("it may becuase the tx is being packing, we are going on to check it!");
    99          // console.log(err);
   100          setTimeout(function () {
   101              checkTransaction(hash, callback);
   102          }, 2000);
   103      });
   104  }
   105  
   106  if (testType == 4) {
   107      console.log("test from deploy contract");
   108      testBinary();
   109  } else if (testType == 3) {
   110      console.log("test for muti-nvm");
   111      deployContracts();
   112  } else if (testType == 2) {
   113      console.log("test for normal call contracts");
   114      deployContracts();
   115  } else if (testType == 1) {
   116      console.log("test for noraml binary");
   117      testBinary();
   118  }
   119  
   120  function testBinary() {
   121      toAddresses = new Array();
   122      for (var i = 0; i < ContractNumber; i++) {
   123          toAddresses.push(Wallet.Account.NewAccount());
   124      }
   125      neb.api.getAccountState(sourceAccount.getAddressString()).then(function(resp){
   126          nonce = parseInt(resp.nonce);
   127          claimTokens();
   128      })
   129  }
   130  
   131  function deployContracts() {
   132      calleeContractAddresses = new Array();
   133      callerContractAddresses = new Array();
   134      try {
   135          var calleeContract = {
   136              "source": calleeContractSrc,
   137              "sourceType": "js",
   138              "args": ''
   139          };
   140          var callerContract = {
   141              "source": callerContractSrc,
   142              "sourceType": "js",
   143              "args": ''
   144          };
   145          neb.api.getAccountState(sourceAccount.getAddressString()).then(function (resp) {
   146              console.log("----step0. get source account state: " + JSON.stringify(resp));
   147              nonce = parseInt(resp.nonce);
   148  
   149              for (var i = 0; i < ContractNumber - 1; i++) {
   150                  var tx = new Transaction(ChainID, sourceAccount, sourceAccount, 0, ++nonce, 1000000, 20000000, calleeContract);
   151                  tx.signTransaction();
   152                  neb.api.sendRawTransaction(tx.toProtoString()).then(function (resp) {
   153                      calleeContractAddresses.push(resp.contract_address);
   154                  });
   155  
   156                  tx = new Transaction(ChainID, sourceAccount, sourceAccount, 0, ++nonce, 1000000, 20000000, callerContract);
   157                  tx.signTransaction();
   158                  neb.api.sendRawTransaction(tx.toProtoString()).then(function (resp) {
   159                      callerContractAddresses.push(resp.contract_address);
   160                  })
   161              }
   162              var tx = new Transaction(ChainID, sourceAccount, sourceAccount, 0, ++nonce, 1000000, 20000000, calleeContract);
   163              tx.signTransaction();
   164              return neb.api.sendRawTransaction(tx.toProtoString());
   165          }).then(function (resp) {
   166              console.log("----step1. deploy last callee contract: " + JSON.stringify(resp));
   167              calleeContractAddresses.push(resp.contract_address);
   168  
   169              var tx = new Transaction(ChainID, sourceAccount, sourceAccount, 0, ++nonce, 1000000, 20000000, callerContract);
   170              tx.signTransaction();
   171              console.log(tx.contract);
   172              return neb.api.sendRawTransaction(tx.toProtoString());
   173          }).then(function (resp) {
   174              console.log("----step2. deploy last caller contract: " + JSON.stringify(resp));
   175              callerContractAddresses.push(resp.contract_address);
   176              checkTransaction(resp.txhash, function (resp) {
   177                  try {
   178                      expect(resp).to.not.be.a('undefined');
   179                      expect(resp.status).to.be.equal(1);
   180                      console.log("----step3. have been on chain, to claim tokens");
   181                      claimTokens();
   182                  } catch (err) {
   183                      console.log("check tx err :" + err);
   184                      return;
   185                  }
   186              });
   187          }).catch(function (err) {
   188              console.log("unexpected err: " + err);
   189          });
   190      } catch (err) {
   191          console.log("unexpected err: " + err);
   192      }
   193  }
   194  
   195  
   196  function claimTokens() {
   197      accountArray = new Array();
   198      for (var i = 0; i < AddressNumber; i++) {
   199          var account = Wallet.Account.NewAccount();
   200          accountArray.push(account);
   201  
   202          sendTransaction(0, 1, sourceAccount, account, "1000000000000000", ++nonce);
   203          sleep(10);
   204      }
   205      checkClaimTokens();
   206  }
   207  
   208  function sendTransaction(index, totalTimes, from, to, value, nonce) {
   209      if (index < totalTimes) {
   210          var transaction = new Wallet.Transaction(ChainID, from, to, value, nonce);
   211          transaction.signTransaction();
   212          var rawTx = transaction.toProtoString();
   213          neb.api.sendRawTransaction(rawTx).then(function (resp) {
   214              console.log("send raw transaction (claim token)resp:" + JSON.stringify(resp));
   215              if (resp.txhash) {
   216                  sendTransaction(++index, totalTimes, from, to, value, ++nonce);
   217              }
   218          });
   219      }
   220  }
   221  
   222  function checkClaimTokens() {
   223      console.log("=========", nonce);
   224      var interval = setInterval(function () {
   225          neb.api.getAccountState(sourceAccount.getAddressString()).then(function (resp) {
   226              console.log("check claim token")
   227              console.log("master accountState resp:" + JSON.stringify(resp));
   228              if (resp.nonce >= nonce) {
   229                  clearInterval(interval);
   230                  sendTransactionsForTps();
   231              }
   232          });
   233  
   234      }, 2000);
   235  }
   236  
   237  function sendTransactionsForTps() {
   238  
   239      console.log("start tps transaction sending...");
   240  
   241      startTime = new Date().getTime();
   242  
   243      console.log(SendTimes);
   244      console.log(AddressNumber);
   245  
   246      for (var i = 0; i < AddressNumber; i++) {
   247          for (var j = 0; j < SendTimes; j++) {
   248              var contract_index = i % ContractNumber;
   249              callMutiLevelNvm(accountArray[i], j + 1, contract_index);
   250              // neb.setRequest(new HttpRequest(node));
   251  
   252              // sendTransaction(0, SendTimes, accountArray[i], to, "1", 1);
   253              //to call()
   254              //sleep(10);
   255          };
   256          console.log("sending transaction... address number: ", i);
   257          sleep(10);
   258      }
   259  
   260      checkTps();
   261  }
   262  
   263  var callTimes = 0
   264  var receiveReciptTimes = 0;
   265  
   266  function callMutiLevelNvm(from, nonce, contract_index) {
   267      // console.log(callTimes);
   268      callTimes += 1;
   269      var tx;
   270      var contract;
   271  
   272      if (testType == 3) {
   273          contract = {
   274              "function": "testTpsForMutiNvm",
   275              "args": "[\"" + calleeContractAddresses[contract_index] + "\"]"
   276          };
   277      } else if (testType == 2) {
   278  
   279          contract = {
   280              "function": "testTpsForNormalCall",
   281              "args": "[]"
   282          };
   283      } else if (testType == 4) {
   284          contract = {
   285              "source": deployContractSrc,
   286              "sourceType": "ts",
   287              "args": "[]"
   288          };
   289      }
   290  
   291  
   292      //send 1 wei by the way
   293      if (testType == 3 || testType == 2) {
   294          tx = new Transaction(ChainID, from, callerContractAddresses[contract_index], 1, nonce, 1000000, 2000000, contract);
   295      } else if (testType == 1) {
   296          tx = new Transaction(ChainID, from, toAddresses[contract_index], 1, nonce, 1000000, 2000000);
   297      } else if (testType == 4) {
   298          tx = new Transaction(ChainID, from, from, 1, nonce, 1000000, 2000000, contract);
   299      } else {
   300          throw "no test type";
   301      }
   302      // var tx = new Transaction(ChainID, from, to, 1, nonce, 1000000, 2000000);
   303  
   304      // tx.to = contractAddress;
   305      tx.signTransaction();
   306      // console.log("silent_debug");
   307      try {
   308       neb.api.sendRawTransaction(tx.toProtoString());
   309      // .then(function(resp) {
   310      //     ++receiveReciptTimes;
   311      //     // console.log(JSON.stringify(resp));
   312      // }).catch(function(err) {
   313      //     ++receiveReciptTimes;
   314      // });
   315      } catch (err) {
   316          console.log(err);
   317      }
   318  }
   319  
   320  function checkTps() {
   321  
   322      // console.log("send times: ", callTimes)
   323      // console.log("receive times: ", receiveReciptTimes);
   324  
   325      // var times = 1;
   326      // var interval = setInterval(function () {
   327      //     neb.api.getAccountState(to.getAddressString()).then(function (resp) {
   328      //         console.log("to address state:" + JSON.stringify(resp), "times :", times);
   329      //         times += 1;
   330      //         if (resp.balance >= receiveReciptTimes) {
   331      //             clearInterval(interval);
   332  
   333      //             var endTime = new Date().getTime();
   334  
   335      //             console.log("====================");
   336      //             console.log("env is ", env);
   337      //             console.log("concurrency number is ", AddressNumber);
   338      //             console.log("total number is ", AddressNumber * SendTimes);
   339      //             console.log("tps is: ", parseInt(resp.balance) / ((endTime - startTime) / 1000));
   340      //             console.log("====================")
   341      //         }
   342      //     });
   343  
   344      // }, 1000);
   345  }