1234567891011121314151617import usingApi, {submitTransactionAsync} from './substrate/substrate-api';18import {IKeyringPair} from '@polkadot/types/types';19import {Abi, BlueprintPromise as Blueprint, CodePromise, ContractPromise as Contract} from '@polkadot/api-contract';20import {ApiPromise, Keyring} from '@polkadot/api';21import {findUnusedAddress} from './util/helpers';22import fs from 'fs';2324const value = 0;25const gasLimit = 500000n * 1000000n;26const endowment = '1000000000000000';272829function deployBlueprint(alice: IKeyringPair, code: CodePromise): Promise<Blueprint> {30 return new Promise<Blueprint>(async (resolve) => {31 const unsub = await code32 .createBlueprint()33 .signAndSend(alice, (result) => {34 if (result.status.isInBlock || result.status.isFinalized) {35 36 resolve(result.blueprint);37 unsub();38 }39 });40 });41}424344function deployContract(alice: IKeyringPair, blueprint: Blueprint) : Promise<any> {45 return new Promise<any>(async (resolve) => {46 const unsub = await blueprint.tx47 .new(endowment, gasLimit)48 .signAndSend(alice, (result) => {49 if (result.status.isInBlock || result.status.isFinalized) {50 unsub();51 resolve(result);52 }53 });54 });55}5657async function prepareDeployer(api: ApiPromise, privateKeyWrapper: ((account: string) => IKeyringPair)) {58 59 const deployer = await findUnusedAddress(api, privateKeyWrapper);6061 62 const alice = privateKeyWrapper('//Alice');63 const amount = BigInt(endowment) + 10n**15n;64 const tx = api.tx.balances.transfer(deployer.address, amount);65 await submitTransactionAsync(alice, tx);6667 return deployer;68}6970async function deployLoadTester(api: ApiPromise, privateKeyWrapper: ((account: string) => IKeyringPair)): Promise<[Contract, IKeyringPair]> {71 const metadata = JSON.parse(fs.readFileSync('./src/load_test_sc/metadata.json').toString('utf-8'));72 const abi = new Abi(metadata);7374 const deployer = await prepareDeployer(api, privateKeyWrapper);7576 const wasm = fs.readFileSync('./src/load_test_sc/loadtester.wasm');7778 const code = new CodePromise(api, abi, wasm);7980 const blueprint = await deployBlueprint(deployer, code);81 const contract = (await deployContract(deployer, blueprint))['contract'] as Contract;8283 return [contract, deployer];84}8586async function getScData(contract: Contract, deployer: IKeyringPair) {87 const result = await contract.query.get(deployer.address, value, gasLimit);8889 if(!result.result.isSuccess) {90 throw 'Failed to get value';91 }92 return result.result.asSuccess.data;93}949596describe('RPC Tests', () => {97 it('Simple RPC Load Test', async () => {98 await usingApi(async api => {99 let count = 0;100 let hrTime = process.hrtime();101 let microsec1 = hrTime[0] * 1000000 + hrTime[1] / 1000;102 let rate = 0;103 const checkPoint = 1000;104105 106 while (true) {107 await api.rpc.system.chain();108 count++;109 process.stdout.write(`RPC reads: ${count} times at rate ${rate} r/s \r`);110111 if (count % checkPoint == 0) {112 hrTime = process.hrtime();113 const microsec2 = hrTime[0] * 1000000 + hrTime[1] / 1000;114 rate = 1000000*checkPoint/(microsec2 - microsec1);115 microsec1 = microsec2;116 }117 }118 });119 });120121 it('Smart Contract RPC Load Test', async () => {122 await usingApi(async (api, privateKeyWrapper) => {123124 125 const [contract, deployer] = await deployLoadTester(api, privateKeyWrapper);126127 128 const bob = privateKeyWrapper('//Bob');129 const tx = contract.tx.bloat(value, gasLimit, 200);130 await submitTransactionAsync(bob, tx);131132 133 let count = 0;134 let hrTime = process.hrtime();135 let microsec1 = hrTime[0] * 1000000 + hrTime[1] / 1000;136 let rate = 0;137 const checkPoint = 10;138139 140 while (true) {141 await getScData(contract, deployer);142 count++;143 process.stdout.write(`SC reads: ${count} times at rate ${rate} r/s \r`);144145 if (count % checkPoint == 0) {146 hrTime = process.hrtime();147 const microsec2 = hrTime[0] * 1000000 + hrTime[1] / 1000;148 rate = 1000000*checkPoint/(microsec2 - microsec1);149 microsec1 = microsec2;150 }151 }152 });153 });154155});