difftreelog
tests: add transfer load script
in: master
3 files changed
tests/package.jsondiffbeforeafterboth--- a/tests/package.json
+++ b/tests/package.json
@@ -20,6 +20,7 @@
"scripts": {
"test": "mocha --timeout 9999999 -r ts-node/register ./**/*.test.ts",
"load": "mocha --timeout 9999999 -r ts-node/register ./**/*.load.ts",
+ "loadTransfer": "ts-node src/transfer.nload.ts",
"testAddCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/addCollectionAdmin.test.ts",
"testSetSchemaVersion": "mocha --timeout 9999999 -r ts-node/register ./**/setSchemaVersion.test.ts",
"testSetVariableMetaData": "mocha --timeout 9999999 -r ts-node/register ./**/setVariableMetaData.test.ts",
tests/src/transfer.load.tsdiffbeforeafterboth1import { ApiPromise } from "@polkadot/api";2import { IKeyringPair } from '@polkadot/types/types';3import privateKey from "./substrate/privateKey";4import usingApi, { submitTransactionAsync } from "./substrate/substrate-api";5import waitNewBlocks from "./substrate/wait-new-blocks";6import { findUnusedAddresses } from "./util/helpers";7import * as cluster from 'cluster';8import os from 'os';910// Innacurate transfer fee11const FEE = 10n ** 8n;1213let counters: { [key: string]: number } = {};14function increaseCounter(name: string, amount: number) {15 if (!counters[name]) {16 counters[name] = 0;17 }18 counters[name] += amount;19}20function flushCounterToMaster() {21 if (Object.keys(counters).length === 0) {22 return;23 }24 process.send!(counters);25 counters = {};26}2728async function distributeBalance(source: IKeyringPair, api: ApiPromise, totalAmount: bigint, stages: number) {29 let accounts = [source];30 // we don't need source in output array31 const failedAccounts = [0];3233 const finalUserAmount = 2 ** stages - 1;34 accounts.push(...await findUnusedAddresses(api, finalUserAmount));35 // findUnusedAddresses produces at least 1 request per user36 increaseCounter('requests', finalUserAmount);3738 for (let stage = 0; stage < stages; stage++) {39 let usersWithBalance = 2 ** stage;40 let amount = totalAmount / (2n ** BigInt(stage)) - FEE * BigInt(stage);41 // console.log(`Stage ${stage}/${stages}, ${usersWithBalance} => ${usersWithBalance * 2} = ${amount}`);42 let txs = [];43 for (let i = 0; i < usersWithBalance; i++) {44 let newUser = accounts[i + usersWithBalance];45 // console.log(`${accounts[i].address} => ${newUser.address} = ${amountToSplit}`);46 const tx = api.tx.balances.transfer(newUser.address, amount);47 txs.push(submitTransactionAsync(accounts[i], tx).catch(e => {48 failedAccounts.push(i + usersWithBalance);49 increaseCounter('txFailed', 1);50 }));51 increaseCounter('tx', 1);52 }53 await Promise.all(txs);54 }5556 for (let account of failedAccounts.reverse()) {57 accounts.splice(account, 1);58 }59 return accounts;60}6162if (cluster.isMaster) {63 let testDone = false;64 usingApi(async (api) => {65 let prevCounters: { [key: string]: number } = {};66 while (!testDone) {67 for (let name in counters) {68 if (!(name in prevCounters)) {69 prevCounters[name] = 0;70 }71 if(counters[name] === prevCounters[name]) {72 continue;73 }74 console.log(`${name.padEnd(15)} = ${counters[name]-prevCounters[name]}`);75 prevCounters[name] = counters[name];76 }77 await waitNewBlocks(api, 1);78 }79 });80 let waiting: Promise<void>[] = [];81 console.log(`Starting ${os.cpus().length} workers`);82 usingApi(async (api) => {83 const alice = privateKey('//Alice');84 for (let id in os.cpus()) {85 const WORKER_NAME = `//LoadWorker${id}_${Date.now()}`;86 const workerAccount = privateKey(WORKER_NAME);87 const tx = api.tx.balances.transfer(workerAccount.address, 400n * 10n ** 23n);88 await submitTransactionAsync(alice, tx);8990 let worker = cluster.fork({91 WORKER_NAME,92 STAGES: id + 293 });94 worker.on('message', msg => {95 for (let key in msg) {96 increaseCounter(key, msg[key]);97 }98 });99 waiting.push(new Promise(res => worker.on('exit', res)));100 }101 await Promise.all(waiting);102 testDone = true;103 })104} else {105 increaseCounter('startedWorkers', 1);106 usingApi(async (api) => {107 await distributeBalance(privateKey(process.env.WORKER_NAME as string), api, 400n * 10n ** 22n, 10);108 });109 const interval = setInterval(() => {110 flushCounterToMaster();111 }, 100);112 interval.unref();113}tests/src/transfer.nload.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/transfer.nload.ts
@@ -0,0 +1,113 @@
+import { ApiPromise } from "@polkadot/api";
+import { IKeyringPair } from '@polkadot/types/types';
+import privateKey from "./substrate/privateKey";
+import usingApi, { submitTransactionAsync } from "./substrate/substrate-api";
+import waitNewBlocks from "./substrate/wait-new-blocks";
+import { findUnusedAddresses } from "./util/helpers";
+import * as cluster from 'cluster';
+import os from 'os';
+
+// Innacurate transfer fee
+const FEE = 10n ** 8n;
+
+let counters: { [key: string]: number } = {};
+function increaseCounter(name: string, amount: number) {
+ if (!counters[name]) {
+ counters[name] = 0;
+ }
+ counters[name] += amount;
+}
+function flushCounterToMaster() {
+ if (Object.keys(counters).length === 0) {
+ return;
+ }
+ process.send!(counters);
+ counters = {};
+}
+
+async function distributeBalance(source: IKeyringPair, api: ApiPromise, totalAmount: bigint, stages: number) {
+ let accounts = [source];
+ // we don't need source in output array
+ const failedAccounts = [0];
+
+ const finalUserAmount = 2 ** stages - 1;
+ accounts.push(...await findUnusedAddresses(api, finalUserAmount));
+ // findUnusedAddresses produces at least 1 request per user
+ increaseCounter('requests', finalUserAmount);
+
+ for (let stage = 0; stage < stages; stage++) {
+ let usersWithBalance = 2 ** stage;
+ let amount = totalAmount / (2n ** BigInt(stage)) - FEE * BigInt(stage);
+ // console.log(`Stage ${stage}/${stages}, ${usersWithBalance} => ${usersWithBalance * 2} = ${amount}`);
+ let txs = [];
+ for (let i = 0; i < usersWithBalance; i++) {
+ let newUser = accounts[i + usersWithBalance];
+ // console.log(`${accounts[i].address} => ${newUser.address} = ${amountToSplit}`);
+ const tx = api.tx.balances.transfer(newUser.address, amount);
+ txs.push(submitTransactionAsync(accounts[i], tx).catch(e => {
+ failedAccounts.push(i + usersWithBalance);
+ increaseCounter('txFailed', 1);
+ }));
+ increaseCounter('tx', 1);
+ }
+ await Promise.all(txs);
+ }
+
+ for (let account of failedAccounts.reverse()) {
+ accounts.splice(account, 1);
+ }
+ return accounts;
+}
+
+if (cluster.isMaster) {
+ let testDone = false;
+ usingApi(async (api) => {
+ let prevCounters: { [key: string]: number } = {};
+ while (!testDone) {
+ for (let name in counters) {
+ if (!(name in prevCounters)) {
+ prevCounters[name] = 0;
+ }
+ if(counters[name] === prevCounters[name]) {
+ continue;
+ }
+ console.log(`${name.padEnd(15)} = ${counters[name] - prevCounters[name]}`);
+ prevCounters[name] = counters[name];
+ }
+ await waitNewBlocks(api, 1);
+ }
+ });
+ let waiting: Promise<void>[] = [];
+ console.log(`Starting ${os.cpus().length} workers`);
+ usingApi(async (api) => {
+ const alice = privateKey('//Alice');
+ for (let id in os.cpus()) {
+ const WORKER_NAME = `//LoadWorker${id}_${Date.now()}`;
+ const workerAccount = privateKey(WORKER_NAME);
+ const tx = api.tx.balances.transfer(workerAccount.address, 400n * 10n ** 23n);
+ await submitTransactionAsync(alice, tx);
+
+ let worker = cluster.fork({
+ WORKER_NAME,
+ STAGES: id + 2
+ });
+ worker.on('message', msg => {
+ for (let key in msg) {
+ increaseCounter(key, msg[key]);
+ }
+ });
+ waiting.push(new Promise(res => worker.on('exit', res)));
+ }
+ await Promise.all(waiting);
+ testDone = true;
+ })
+} else {
+ increaseCounter('startedWorkers', 1);
+ usingApi(async (api) => {
+ await distributeBalance(privateKey(process.env.WORKER_NAME as string), api, 400n * 10n ** 22n, 10);
+ });
+ const interval = setInterval(() => {
+ flushCounterToMaster();
+ }, 100);
+ interval.unref();
+}
\ No newline at end of file