git.delta.rocks / unique-network / refs/commits / 76d8b567eabc

difftreelog

source

tests/src/rpc.load.ts4.6 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 {findUnusedAddress} from './util/helpers';11import fs from 'fs';12import privateKey from './substrate/privateKey';1314const value = 0;15const gasLimit = 500000n * 1000000n;16const endowment = '1000000000000000';1718/*eslint no-async-promise-executor: "off"*/19function 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          // here we have an additional field in the result, containing the blueprint26          resolve(result.blueprint);27          unsub();28        }29      });30  });31}3233/*eslint no-async-promise-executor: "off"*/34function 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  // Find unused address49  const deployer = await findUnusedAddress(api);5051  // Transfer balance to it52  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      /* eslint no-constant-condition: "off" */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      // Deploy smart contract116      const [contract, deployer] = await deployLoadTester(api);117118      // Fill smart contract up with data119      const bob = privateKey('//Bob');120      const tx = contract.tx.bloat(value, gasLimit, 200);121      await submitTransactionAsync(bob, tx);122123      // Run load test124      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      /* eslint no-constant-condition: "off" */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});