git.delta.rocks / unique-network / refs/commits / 258c7f483482

difftreelog

source

tests/src/rpc.load.ts5.3 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import 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';2728/*eslint no-async-promise-executor: "off"*/29function 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          // here we have an additional field in the result, containing the blueprint36          resolve(result.blueprint);37          unsub();38        }39      });40  });41}4243/*eslint no-async-promise-executor: "off"*/44function 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, privateKeyWrapper: ((account: string) => IKeyringPair)) {58  // Find unused address59  const deployer = await findUnusedAddress(api, privateKeyWrapper);6061  // Transfer balance to it62  const alice = privateKeyWrapper('//Alice');63  const amount = BigInt(endowment) + 10n**15n;64  const tx = api.tx.balances.transfer(deployer.address, amount);65  await submitTransactionAsync(alice, tx);6667  return deployer;68}6970async function deployLoadTester(api: ApiPromise, privateKeyWrapper: ((account: string) => IKeyringPair)): Promise<[Contract, IKeyringPair]> {71  const metadata = JSON.parse(fs.readFileSync('./src/load_test_sc/metadata.json').toString('utf-8'));72  const abi = new Abi(metadata);7374  const deployer = await prepareDeployer(api, privateKeyWrapper);7576  const wasm = fs.readFileSync('./src/load_test_sc/loadtester.wasm');7778  const code = new CodePromise(api, abi, wasm);7980  const blueprint = await deployBlueprint(deployer, code);81  const contract = (await deployContract(deployer, blueprint))['contract'] as Contract;8283  return [contract, deployer];84}8586async function getScData(contract: Contract, deployer: IKeyringPair) {87  const result = await contract.query.get(deployer.address, value, gasLimit);8889  if(!result.result.isSuccess) {90    throw 'Failed to get value';91  }92  return result.result.asSuccess.data;93}949596describe('RPC Tests', () => {97  it('Simple RPC Load Test', async () => {98    await usingApi(async api => {99      let count = 0;100      let hrTime = process.hrtime();101      let microsec1 = hrTime[0] * 1000000 + hrTime[1] / 1000;102      let rate = 0;103      const checkPoint = 1000;104105      /* eslint no-constant-condition: "off" */106      while (true) {107        await api.rpc.system.chain();108        count++;109        process.stdout.write(`RPC reads: ${count} times at rate ${rate} r/s            \r`);110111        if (count % checkPoint == 0) {112          hrTime = process.hrtime();113          const microsec2 = hrTime[0] * 1000000 + hrTime[1] / 1000;114          rate = 1000000*checkPoint/(microsec2 - microsec1);115          microsec1 = microsec2;116        }117      }118    });119  });120121  it('Smart Contract RPC Load Test', async () => {122    await usingApi(async (api, privateKeyWrapper) => {123124      // Deploy smart contract125      const [contract, deployer] = await deployLoadTester(api, privateKeyWrapper);126127      // Fill smart contract up with data128      const bob = privateKeyWrapper('//Bob');129      const tx = contract.tx.bloat(value, gasLimit, 200);130      await submitTransactionAsync(bob, tx);131132      // Run load test133      let count = 0;134      let hrTime = process.hrtime();135      let microsec1 = hrTime[0] * 1000000 + hrTime[1] / 1000;136      let rate = 0;137      const checkPoint = 10;138139      /* eslint no-constant-condition: "off" */140      while (true) {141        await getScData(contract, deployer);142        count++;143        process.stdout.write(`SC reads: ${count} times at rate ${rate} r/s            \r`);144145        if (count % checkPoint == 0) {146          hrTime = process.hrtime();147          const microsec2 = hrTime[0] * 1000000 + hrTime[1] / 1000;148          rate = 1000000*checkPoint/(microsec2 - microsec1);149          microsec1 = microsec2;150        }151      }152    });153  });154155});