git.delta.rocks / unique-network / refs/commits / 5ca51b5e5337

difftreelog

source

tests/src/rpc.load.ts5.1 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) {58  // Find unused address59  const deployer = await findUnusedAddress(api);6061  // Transfer balance to it62  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      /* eslint no-constant-condition: "off" */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      // Deploy smart contract126      const [contract, deployer] = await deployLoadTester(api);127128      // Fill smart contract up with data129      const bob = privateKeyWrapper('//Bob');130      const tx = contract.tx.bloat(value, gasLimit, 200);131      await submitTransactionAsync(bob, tx);132133      // Run load test134      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      /* eslint no-constant-condition: "off" */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});