git.delta.rocks / unique-network / refs/commits / e961e84cf5d1

difftreelog

source

tests/src/rpc.load.ts4.7 KiBsourcehistory
1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56import 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';1819/*eslint no-async-promise-executor: "off"*/20function 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          // here we have an additional field in the result, containing the blueprint27          resolve(result.blueprint);28          unsub();29        }30      });31  });32}3334/*eslint no-async-promise-executor: "off"*/35function 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  // Find unused address50  const deployer = await findUnusedAddress(api);5152  // Transfer balance to it53  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      /* eslint no-constant-condition: "off" */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      // Deploy smart contract118      const [contract, deployer] = await deployLoadTester(api);119120      // Fill smart contract up with data121      const bob = privateKey('//Bob');122      const tx = contract.tx.bloat(value, gasLimit, 200);123      await submitTransactionAsync(bob, tx);124125      // Run load test126      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      /* eslint no-constant-condition: "off" */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});