123456import usingApi, { submitTransactionAsync } from './substrate/substrate-api';7import { IKeyringPair } from '@polkadot/types/types';8import { Abi, BlueprintPromise as Blueprint, CodePromise, ContractPromise as Contract } from '@polkadot/api-contract';9import { ApiPromise, Keyring } from '@polkadot/api';10import { BigNumber } from 'bignumber.js';11import { findUnusedAddress } from './util/helpers';12import fs from 'fs';13import privateKey from './substrate/privateKey';1415const value = 0;16const gasLimit = 500000n * 1000000n;17const endowment = '1000000000000000';181920function deployBlueprint(alice: IKeyringPair, code: CodePromise): Promise<Blueprint> {21 return new Promise<Blueprint>(async (resolve) => {22 const unsub = await code23 .createBlueprint()24 .signAndSend(alice, (result) => {25 if (result.status.isInBlock || result.status.isFinalized) {26 27 resolve(result.blueprint);28 unsub();29 }30 });31 });32}333435function deployContract(alice: IKeyringPair, blueprint: Blueprint) : Promise<any> {36 return new Promise<any>(async (resolve) => {37 const unsub = await blueprint.tx38 .new(endowment, gasLimit)39 .signAndSend(alice, (result) => {40 if (result.status.isInBlock || result.status.isFinalized) {41 unsub();42 resolve(result);43 }44 }); 45 });46}4748async function prepareDeployer(api: ApiPromise) {49 50 const deployer = await findUnusedAddress(api);5152 53 const keyring = new Keyring({ type: 'sr25519' });54 const alice = keyring.addFromUri('//Alice');55 let amount = new BigNumber(endowment);56 amount = amount.plus(1e15);57 const tx = api.tx.balances.transfer(deployer.address, amount.toFixed());58 await submitTransactionAsync(alice, tx);5960 return deployer;61}6263async function deployLoadTester(api: ApiPromise): Promise<[Contract, IKeyringPair]> {64 const metadata = JSON.parse(fs.readFileSync('./src/load_test_sc/metadata.json').toString('utf-8'));65 const abi = new Abi(metadata);6667 const deployer = await prepareDeployer(api);6869 const wasm = fs.readFileSync('./src/load_test_sc/loadtester.wasm');7071 const code = new CodePromise(api, abi, wasm);7273 const blueprint = await deployBlueprint(deployer, code);74 const contract = (await deployContract(deployer, blueprint))['contract'] as Contract;7576 return [contract, deployer];77}7879async function getScData(contract: Contract, deployer: IKeyringPair) {80 const result = await contract.query.get(deployer.address, value, gasLimit);8182 if(!result.result.isSuccess) {83 throw 'Failed to get value';84 }85 return result.result.asSuccess.data;86}878889describe('RPC Tests', () => {90 it('Simple RPC Load Test', async () => {91 await usingApi(async api => {92 let count = 0;93 let hrTime = process.hrtime();94 let microsec1 = hrTime[0] * 1000000 + hrTime[1] / 1000;95 let rate = 0;96 const checkPoint = 1000;9798 99 while (true) {100 await api.rpc.system.chain();101 count++;102 process.stdout.write(`RPC reads: ${count} times at rate ${rate} r/s \r`);103 104 if (count % checkPoint == 0) {105 hrTime = process.hrtime();106 const microsec2 = hrTime[0] * 1000000 + hrTime[1] / 1000;107 rate = 1000000*checkPoint/(microsec2 - microsec1);108 microsec1 = microsec2;109 }110 }111 });112 });113114 it('Smart Contract RPC Load Test', async () => {115 await usingApi(async api => {116117 118 const [contract, deployer] = await deployLoadTester(api);119120 121 const bob = privateKey('//Bob');122 const tx = contract.tx.bloat(value, gasLimit, 200);123 await submitTransactionAsync(bob, tx);124125 126 let count = 0;127 let hrTime = process.hrtime();128 let microsec1 = hrTime[0] * 1000000 + hrTime[1] / 1000;129 let rate = 0;130 const checkPoint = 10;131132 133 while (true) {134 await getScData(contract, deployer);135 count++;136 process.stdout.write(`SC reads: ${count} times at rate ${rate} r/s \r`);137 138 if (count % checkPoint == 0) {139 hrTime = process.hrtime();140 const microsec2 = hrTime[0] * 1000000 + hrTime[1] / 1000;141 rate = 1000000*checkPoint/(microsec2 - microsec1);142 microsec1 = microsec2;143 }144 }145 });146 });147148});