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) {58 59 const deployer = await findUnusedAddress(api);6061 62 const keyring = new Keyring({type: 'sr25519'});63 const alice = keyring.addFromUri('//Alice');64 const amount = BigInt(endowment) + 10n**15n;65 const tx = api.tx.balances.transfer(deployer.address, amount);66 await submitTransactionAsync(alice, tx);6768 return deployer;69}7071async function deployLoadTester(api: ApiPromise): Promise<[Contract, IKeyringPair]> {72 const metadata = JSON.parse(fs.readFileSync('./src/load_test_sc/metadata.json').toString('utf-8'));73 const abi = new Abi(metadata);7475 const deployer = await prepareDeployer(api);7677 const wasm = fs.readFileSync('./src/load_test_sc/loadtester.wasm');7879 const code = new CodePromise(api, abi, wasm);8081 const blueprint = await deployBlueprint(deployer, code);82 const contract = (await deployContract(deployer, blueprint))['contract'] as Contract;8384 return [contract, deployer];85}8687async function getScData(contract: Contract, deployer: IKeyringPair) {88 const result = await contract.query.get(deployer.address, value, gasLimit);8990 if(!result.result.isSuccess) {91 throw 'Failed to get value';92 }93 return result.result.asSuccess.data;94}959697describe('RPC Tests', () => {98 it('Simple RPC Load Test', async () => {99 await usingApi(async api => {100 let count = 0;101 let hrTime = process.hrtime();102 let microsec1 = hrTime[0] * 1000000 + hrTime[1] / 1000;103 let rate = 0;104 const checkPoint = 1000;105106 107 while (true) {108 await api.rpc.system.chain();109 count++;110 process.stdout.write(`RPC reads: ${count} times at rate ${rate} r/s \r`);111112 if (count % checkPoint == 0) {113 hrTime = process.hrtime();114 const microsec2 = hrTime[0] * 1000000 + hrTime[1] / 1000;115 rate = 1000000*checkPoint/(microsec2 - microsec1);116 microsec1 = microsec2;117 }118 }119 });120 });121122 it('Smart Contract RPC Load Test', async () => {123 await usingApi(async (api, privateKeyWrapper) => {124125 126 const [contract, deployer] = await deployLoadTester(api);127128 129 const bob = privateKeyWrapper!('//Bob');130 const tx = contract.tx.bloat(value, gasLimit, 200);131 await submitTransactionAsync(bob, tx);132133 134 let count = 0;135 let hrTime = process.hrtime();136 let microsec1 = hrTime[0] * 1000000 + hrTime[1] / 1000;137 let rate = 0;138 const checkPoint = 10;139140 141 while (true) {142 await getScData(contract, deployer);143 count++;144 process.stdout.write(`SC reads: ${count} times at rate ${rate} r/s \r`);145146 if (count % checkPoint == 0) {147 hrTime = process.hrtime();148 const microsec2 = hrTime[0] * 1000000 + hrTime[1] / 1000;149 rate = 1000000*checkPoint/(microsec2 - microsec1);150 microsec1 = microsec2;151 }152 }153 });154 });155156});