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 {findUnusedAddress} from './util/helpers';11import fs from 'fs';12import privateKey from './substrate/privateKey';1314const value = 0;15const gasLimit = 500000n * 1000000n;16const endowment = '1000000000000000';171819function deployBlueprint(alice: IKeyringPair, code: CodePromise): Promise<Blueprint> {20 return new Promise<Blueprint>(async (resolve) => {21 const unsub = await code22 .createBlueprint()23 .signAndSend(alice, (result) => {24 if (result.status.isInBlock || result.status.isFinalized) {25 26 resolve(result.blueprint);27 unsub();28 }29 });30 });31}323334function deployContract(alice: IKeyringPair, blueprint: Blueprint) : Promise<any> {35 return new Promise<any>(async (resolve) => {36 const unsub = await blueprint.tx37 .new(endowment, gasLimit)38 .signAndSend(alice, (result) => {39 if (result.status.isInBlock || result.status.isFinalized) {40 unsub();41 resolve(result);42 }43 });44 });45}4647async function prepareDeployer(api: ApiPromise) {48 49 const deployer = await findUnusedAddress(api);5051 52 const keyring = new Keyring({type: 'sr25519'});53 const alice = keyring.addFromUri('//Alice');54 const amount = BigInt(endowment) + 10n**15n;55 const tx = api.tx.balances.transfer(deployer.address, amount);56 await submitTransactionAsync(alice, tx);5758 return deployer;59}6061async function deployLoadTester(api: ApiPromise): Promise<[Contract, IKeyringPair]> {62 const metadata = JSON.parse(fs.readFileSync('./src/load_test_sc/metadata.json').toString('utf-8'));63 const abi = new Abi(metadata);6465 const deployer = await prepareDeployer(api);6667 const wasm = fs.readFileSync('./src/load_test_sc/loadtester.wasm');6869 const code = new CodePromise(api, abi, wasm);7071 const blueprint = await deployBlueprint(deployer, code);72 const contract = (await deployContract(deployer, blueprint))['contract'] as Contract;7374 return [contract, deployer];75}7677async function getScData(contract: Contract, deployer: IKeyringPair) {78 const result = await contract.query.get(deployer.address, value, gasLimit);7980 if(!result.result.isSuccess) {81 throw 'Failed to get value';82 }83 return result.result.asSuccess.data;84}858687describe('RPC Tests', () => {88 it('Simple RPC Load Test', async () => {89 await usingApi(async api => {90 let count = 0;91 let hrTime = process.hrtime();92 let microsec1 = hrTime[0] * 1000000 + hrTime[1] / 1000;93 let rate = 0;94 const checkPoint = 1000;9596 97 while (true) {98 await api.rpc.system.chain();99 count++;100 process.stdout.write(`RPC reads: ${count} times at rate ${rate} r/s \r`);101102 if (count % checkPoint == 0) {103 hrTime = process.hrtime();104 const microsec2 = hrTime[0] * 1000000 + hrTime[1] / 1000;105 rate = 1000000*checkPoint/(microsec2 - microsec1);106 microsec1 = microsec2;107 }108 }109 });110 });111112 it('Smart Contract RPC Load Test', async () => {113 await usingApi(async api => {114115 116 const [contract, deployer] = await deployLoadTester(api);117118 119 const bob = privateKey('//Bob');120 const tx = contract.tx.bloat(value, gasLimit, 200);121 await submitTransactionAsync(bob, tx);122123 124 let count = 0;125 let hrTime = process.hrtime();126 let microsec1 = hrTime[0] * 1000000 + hrTime[1] / 1000;127 let rate = 0;128 const checkPoint = 10;129130 131 while (true) {132 await getScData(contract, deployer);133 count++;134 process.stdout.write(`SC reads: ${count} times at rate ${rate} r/s \r`);135136 if (count % checkPoint == 0) {137 hrTime = process.hrtime();138 const microsec2 = hrTime[0] * 1000000 + hrTime[1] / 1000;139 rate = 1000000*checkPoint/(microsec2 - microsec1);140 microsec1 = microsec2;141 }142 }143 });144 });145146});