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

     1  'use strict';
     2  
     3  var Wallet = require('nebulas');
     4  var sleep = require("system-sleep");
     5  var HttpRequest = require("../../node-request");
     6  
     7  var args = process.argv.splice(2);
     8  
     9  if (args.length != 3) {
    10  	console.log("please input args 0:env(local,testneb1,testneb2,testneb3) 1:address number(concurrency) 2:sendtimes");
    11  	return;
    12  }
    13  
    14  var env = args[0]; // local testneb1 testneb2
    15  
    16  const AddressNumber = parseInt(args[1]);
    17  const SendTimes = parseInt(args[2]);
    18  
    19  if (AddressNumber <= 0 || SendTimes <= 0) {
    20  
    21  	console.log("please input correct AddressNumber and SendTimes");
    22  	return;
    23  }
    24  
    25  var Neb = Wallet.Neb;
    26  var neb = new Neb();
    27  
    28  var ChainID;
    29  var from;
    30  
    31  //local
    32  if (env == 'local') {
    33  	neb.setRequest(new HttpRequest("http://127.0.0.1:8685")); //https://testnet.nebulas.io
    34  	ChainID = 100;
    35  	from = new Wallet.Account("a6e5eb290e1438fce79f5cb8774a72621637c2c9654c8b2525ed1d7e4e73653f");
    36  } else if (env == 'testneb1') {
    37  	neb.setRequest(new HttpRequest("http://35.182.48.19:8685"));
    38  	ChainID = 1001;
    39  	from = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3");
    40  } else if (env == "testneb2") {
    41  	neb.setRequest(new HttpRequest("http://34.205.26.12:8685"));
    42  	ChainID = 1002;
    43  	from = new Wallet.Account("25a3a441a34658e7a595a0eda222fa43ac51bd223017d17b420674fb6d0a4d52");
    44  } else if (env == "testneb3") {
    45  	neb.setRequest(new HttpRequest("http://35.177.214.138:8685"));
    46  	ChainID = 1003;
    47  	from = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3");
    48  } else {
    49  	console.log("please input correct env local testneb1 testneb2 testneb3");
    50  	return;
    51  }
    52  
    53  var FS = require("fs");
    54  
    55  var lastnonce = 0;
    56  
    57  // new account  to get address
    58  var accountArray = new Array();
    59  for (var i = 0; i < AddressNumber; i++) {
    60  	var account = Wallet.Account.NewAccount();
    61  	//var hash = account.getAddressString();
    62  	accountArray.push(account);
    63  }
    64  
    65  neb.api.getAccountState(from.getAddressString()).then(function (resp) {
    66  	console.log("master accountState resp:", JSON.stringify(resp), ", env: ", env);
    67  	lastnonce = parseInt(resp.nonce);
    68  	console.log("lastnonce:", lastnonce);
    69  });
    70  
    71  sleep(2000);
    72  
    73  cliamTokens();
    74  
    75  var ContractHash;
    76  var ContractAddress;
    77  
    78  var intervalAccount = setInterval(function () {
    79  	neb.api.getAccountState(from.getAddressString()).then(function (resp) {
    80  		console.log("master accountState resp:" + JSON.stringify(resp));
    81  		var nonce = parseInt(resp.nonce);
    82  		console.log("lastnonce:", lastnonce, "resp_nonce:", nonce);
    83  
    84  		if (lastnonce <= nonce) {
    85  			clearInterval(intervalAccount);
    86  			deployContract();
    87  		}
    88  	});
    89  }, 2000);
    90  
    91  function cliamTokens() {
    92  	var nonce = lastnonce + 1;
    93  	for (var j = 0; j < AddressNumber; j++) {
    94  		sendTransaction(nonce, accountArray[j]);
    95  		++nonce;
    96  		sleep(30);
    97  	}
    98  
    99  	lastnonce = nonce - 1;
   100  }
   101  
   102  function deployContract() {
   103  
   104  	var nonce = lastnonce;
   105  	console.log("nonce:" + nonce);
   106  	// create contract
   107  	var dateAndRand = FS.readFileSync("../../../nf/nvm/test/contract_date_and_random.js", "utf-8");
   108  	var contract = {
   109  		"source": dateAndRand,
   110  		"sourceType": "js",
   111  		"args": ""
   112  	};
   113  
   114  	var transaction = new Wallet.Transaction(ChainID, from, from, "0", ++nonce, "1000000", "20000000000", contract);
   115  	transaction.signTransaction();
   116  	var rawTx = transaction.toProtoString();
   117  
   118  	// console.log("contract:" + rawTx);
   119  
   120  	neb.api.sendRawTransaction(rawTx).then(function (resp) {
   121  		console.log("send raw contract transaction resp:" + JSON.stringify(resp));
   122  		if (resp.balance === "0") {
   123  			throw new Error("balance is 0");
   124  		}
   125  		ContractHash = resp.txhash;
   126  		ContractAddress = resp.contract_address;
   127  
   128  		checkContractDeployed();
   129  	});
   130  
   131  	++lastnonce;
   132  }
   133  
   134  function checkContractDeployed() {
   135  
   136  	var retry = 0;
   137  
   138  	// contract status and get contract_address 
   139  	var interval = setInterval(function () {
   140  		console.log("getTransactionReceipt hash:" + ContractHash);
   141  		neb.api.getTransactionReceipt(ContractHash).then(function (resp) {
   142  
   143  			console.log("tx receipt:" + resp.status);
   144  
   145  			if (resp.status && resp.status === 1) {
   146  				clearInterval(interval);
   147  				sendMutilContractTransaction(ContractAddress)
   148  			}
   149  		}).catch(function (err) {
   150  			retry++;
   151  			console.log("error!", JSON.stringify(err.error));
   152  			if (retry > 10) {
   153  				console.log(JSON.stringify(err.error));
   154  				clearInterval(interval);
   155  			}
   156  		});
   157  
   158  	}, 2000);
   159  }
   160  
   161  
   162  function sendTransaction(nonce, address) {
   163  	var transaction = new Wallet.Transaction(ChainID, from, address, neb.nasToBasic(1), nonce);
   164  	transaction.signTransaction();
   165  	var rawTx = transaction.toProtoString();
   166  	neb.api.sendRawTransaction(rawTx).then(function (resp) {
   167  		console.log("send raw transaction resp:" + JSON.stringify(resp));
   168  	});
   169  }
   170  
   171  
   172  
   173  // get current height
   174  var BeginHeight;
   175  //
   176  function sendMutilContractTransaction(address) {
   177  
   178  	neb.api.getNebState().then(function (resp) {
   179  		BeginHeight = resp.height;
   180  		console.log("get NebState resp:" + JSON.stringify(resp));
   181  	});
   182  
   183  	sleep(1000);
   184  	var nonce = lastnonce;
   185  	var t1 = new Date().getTime();
   186  	for (var j = 0; j < AddressNumber; j++) {
   187  		nonce = 0;
   188  		sendContractTransaction(0, nonce, accountArray[j], address);
   189  		//nonce = nonce + SendTimes;
   190  	}
   191  
   192  	lastnonce = SendTimes;
   193  	sleep(1000 * SendTimes)
   194  	getTransactionNumberByHeight();
   195  }
   196  
   197  
   198  
   199  function sendContractTransaction(sendtimes, nonce, from_address, contract_address) {
   200  	if (sendtimes < SendTimes) {
   201  
   202  		var X = Math.floor(Math.random() * Math.floor(6));
   203  
   204  		var call = {
   205  			"function": X % 2 == 0 ? "testDate" : "testRandom",
   206  			"args": ""
   207  		}
   208  
   209  		console.log("send contract nonce:", nonce, ", call:", JSON.stringify(call));
   210  		var transaction = new Wallet.Transaction(ChainID, from_address, contract_address, "0", ++nonce, "1000000", "2000000000", call);
   211  		transaction.signTransaction();
   212  		var rawTx = transaction.toProtoString();
   213  		neb.api.sendRawTransaction(rawTx).then(function (resp) {
   214  			console.log("send raw contract transaction resp:" + JSON.stringify(resp));
   215  			sendtimes++;
   216  			if (resp.txhash) {
   217  				sendContractTransaction(sendtimes, nonce, from_address, contract_address);
   218  			}
   219  		});
   220  	}
   221  }
   222  
   223  function getTransactionNumberByHeight() {
   224  
   225  	var intervalHeight = setInterval(function () {
   226  		neb.api.getAccountState(accountArray[0].getAddressString()).then(function (resp) {
   227  			console.log("master accountState resp:" + JSON.stringify(resp));
   228  			var nonce = parseInt(resp.nonce);
   229  			console.log("lastnonce:", lastnonce, "resp_nonce:", nonce);
   230  
   231  			if (lastnonce <= nonce) {
   232  				clearInterval(intervalHeight)
   233  				sleep(2000)
   234  				neb.api.getNebState().then(function (resp) {
   235  					var EndHeight = resp.height
   236  					console.log("BeginHeight:" + BeginHeight + " EndHeight:" + EndHeight);
   237  					var sum = 0;
   238  					var max = 0;
   239  					var height = BeginHeight
   240  					var h = EndHeight - BeginHeight
   241  					for (; height <= EndHeight; height++) {
   242  						neb.api.getBlockByHeight(height, false).then(function (resp) {
   243  							if (resp.transactions) {
   244  								//console.log("master accountState resp:" + JSON.stringify(resp));
   245  								console.log(resp.height, resp.transactions.length)
   246  								sum += resp.transactions.length
   247  								max = resp.transactions.length > max ? resp.transactions.length : max
   248  							} else {
   249  								console.log(resp.height, 0)
   250  							}
   251  							--h;
   252  						});
   253  						sleep(10)
   254  					}
   255  
   256  					sleep(1000)
   257  					var intervalH = setInterval(function () {
   258  						if (h < 0) {
   259  							clearInterval(intervalH);
   260  							console.log("====================")
   261  							console.log("env is ", env)
   262  							console.log("concurrency number is ", AddressNumber)
   263  							console.log("total number is ", AddressNumber * SendTimes)
   264  							console.log("height from ", BeginHeight, " to ", EndHeight)
   265  							console.log("max of block is ", max)
   266  							console.log("avg of block is ", sum / (EndHeight - BeginHeight))
   267  							console.log("max of tps is ", max / 5)
   268  							console.log("avg of tps is ", sum / (5 * (EndHeight - BeginHeight)))
   269  							console.log("====================")
   270  						}
   271  					}, 2000);
   272  
   273  				});
   274  			}
   275  		})
   276  	}, 1000);
   277  }