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';23import privateKey from './substrate/privateKey';2425const value = 0;26const gasLimit = 500000n * 1000000n;27const endowment = '1000000000000000';282930function deployBlueprint(alice: IKeyringPair, code: CodePromise): Promise<Blueprint> {31 return new Promise<Blueprint>(async (resolve) => {32 const unsub = await code33 .createBlueprint()34 .signAndSend(alice, (result) => {35 if (result.status.isInBlock || result.status.isFinalized) {36 37 resolve(result.blueprint);38 unsub();39 }40 });41 });42}434445function deployContract(alice: IKeyringPair, blueprint: Blueprint) : Promise<any> {46 return new Promise<any>(async (resolve) => {47 const unsub = await blueprint.tx48 .new(endowment, gasLimit)49 .signAndSend(alice, (result) => {50 if (result.status.isInBlock || result.status.isFinalized) {51 unsub();52 resolve(result);53 }54 });55 });56}5758async function prepareDeployer(api: ApiPromise) {59 60 const deployer = await findUnusedAddress(api);6162 63 const keyring = new Keyring({type: 'sr25519'});64 const alice = keyring.addFromUri('//Alice');65 const amount = BigInt(endowment) + 10n**15n;66 const tx = api.tx.balances.transfer(deployer.address, amount);67 await submitTransactionAsync(alice, tx);6869 return deployer;70}7172async function deployLoadTester(api: ApiPromise): Promise<[Contract, IKeyringPair]> {73 const metadata = JSON.parse(fs.readFileSync('./src/load_test_sc/metadata.json').toString('utf-8'));74 const abi = new Abi(metadata);7576 const deployer = await prepareDeployer(api);7778 const wasm = fs.readFileSync('./src/load_test_sc/loadtester.wasm');7980 const code = new CodePromise(api, abi, wasm);8182 const blueprint = await deployBlueprint(deployer, code);83 const contract = (await deployContract(deployer, blueprint))['contract'] as Contract;8485 return [contract, deployer];86}8788async function getScData(contract: Contract, deployer: IKeyringPair) {89 const result = await contract.query.get(deployer.address, value, gasLimit);9091 if(!result.result.isSuccess) {92 throw 'Failed to get value';93 }94 return result.result.asSuccess.data;95}969798describe('RPC Tests', () => {99 it('Simple RPC Load Test', async () => {100 await usingApi(async api => {101 let count = 0;102 let hrTime = process.hrtime();103 let microsec1 = hrTime[0] * 1000000 + hrTime[1] / 1000;104 let rate = 0;105 const checkPoint = 1000;106107 108 while (true) {109 await api.rpc.system.chain();110 count++;111 process.stdout.write(`RPC reads: ${count} times at rate ${rate} r/s \r`);112113 if (count % checkPoint == 0) {114 hrTime = process.hrtime();115 const microsec2 = hrTime[0] * 1000000 + hrTime[1] / 1000;116 rate = 1000000*checkPoint/(microsec2 - microsec1);117 microsec1 = microsec2;118 }119 }120 });121 });122123 it('Smart Contract RPC Load Test', async () => {124 await usingApi(async api => {125126 127 const [contract, deployer] = await deployLoadTester(api);128129 130 const bob = privateKey('//Bob');131 const tx = contract.tx.bloat(value, gasLimit, 200);132 await submitTransactionAsync(bob, tx);133134 135 let count = 0;136 let hrTime = process.hrtime();137 let microsec1 = hrTime[0] * 1000000 + hrTime[1] / 1000;138 let rate = 0;139 const checkPoint = 10;140141 142 while (true) {143 await getScData(contract, deployer);144 count++;145 process.stdout.write(`SC reads: ${count} times at rate ${rate} r/s \r`);146147 if (count % checkPoint == 0) {148 hrTime = process.hrtime();149 const microsec2 = hrTime[0] * 1000000 + hrTime[1] / 1000;150 rate = 1000000*checkPoint/(microsec2 - microsec1);151 microsec1 = microsec2;152 }153 }154 });155 });156157});