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

difftreelog

source

tests/src/rpc.load.ts5.2 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';23import privateKey from './substrate/privateKey';2425const value = 0;26const gasLimit = 500000n * 1000000n;27const endowment = '1000000000000000';2829/*eslint no-async-promise-executor: "off"*/30function 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          // here we have an additional field in the result, containing the blueprint37          resolve(result.blueprint);38          unsub();39        }40      });41  });42}4344/*eslint no-async-promise-executor: "off"*/45function 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  // Find unused address60  const deployer = await findUnusedAddress(api);6162  // Transfer balance to it63  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      /* eslint no-constant-condition: "off" */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      // Deploy smart contract127      const [contract, deployer] = await deployLoadTester(api);128129      // Fill smart contract up with data130      const bob = privateKey('//Bob');131      const tx = contract.tx.bloat(value, gasLimit, 200);132      await submitTransactionAsync(bob, tx);133134      // Run load test135      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      /* eslint no-constant-condition: "off" */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});