difftreelog
Merge branch 'develop' into feature/NFTPAR-237
in: master
7 files changed
tests/loadtester-src/.gitignorediffbeforeafterboth--- /dev/null
+++ b/tests/loadtester-src/.gitignore
@@ -0,0 +1,9 @@
+# Ignore build artifacts from the local tests sub-crate.
+/target/
+
+# Ignore backup files creates by cargo fmt.
+**/*.rs.bk
+
+# Remove Cargo.lock when creating an executable, leave it for libraries
+# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
+Cargo.lock
\ No newline at end of file
tests/loadtester-src/Cargo.tomldiffbeforeafterboth--- /dev/null
+++ b/tests/loadtester-src/Cargo.toml
@@ -0,0 +1,36 @@
+[package]
+name = "loadtester"
+version = "0.1.0"
+authors = ["[your_name] <[your_email]>"]
+edition = "2018"
+
+[dependencies]
+ink_primitives = { version = "3.0.0-rc2", default-features = false }
+ink_metadata = { version = "3.0.0-rc2", default-features = false, features = ["derive"], optional = true }
+ink_env = { version = "3.0.0-rc2", default-features = false }
+ink_storage = { version = "3.0.0-rc2", default-features = false }
+ink_lang = { version = "3.0.0-rc2", default-features = false }
+ink_prelude = { version = "3.0.0-rc2", default-features = false }
+
+scale = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive"] }
+scale-info = { version = "0.4.1", default-features = false, features = ["derive"], optional = true }
+
+[lib]
+name = "loadtester"
+path = "lib.rs"
+crate-type = [
+ # Used for normal contract Wasm blobs.
+ "cdylib",
+]
+
+[features]
+default = ["std"]
+std = [
+ "ink_metadata/std",
+ "ink_env/std",
+ "ink_storage/std",
+ "ink_primitives/std",
+ "scale/std",
+ "scale-info/std",
+]
+ink-as-dependency = []
tests/loadtester-src/lib.rsdiffbeforeafterboth--- /dev/null
+++ b/tests/loadtester-src/lib.rs
@@ -0,0 +1,53 @@
+#![cfg_attr(not(feature = "std"), no_std)]
+
+use ink_lang as ink;
+
+#[ink::contract]
+mod loadtester {
+ use ink_storage::collections::Vec as InkVec;
+
+ #[ink(storage)]
+ pub struct LoadTester {
+ vector: InkVec<u64>,
+ }
+
+ impl LoadTester {
+ #[ink(constructor)]
+ pub fn new() -> Self {
+ Self {
+ vector: InkVec::new(),
+ }
+ }
+
+ #[ink(message)]
+ pub fn bloat(&mut self, count: u64){
+ for i in 1..count+1 {
+ self.vector.push(i);
+ }
+ }
+
+ #[ink(message)]
+ pub fn get(&self) -> u128 {
+ let mut sum: u128 = 0;
+ for num in self.vector.iter() {
+ sum += *num as u128;
+ }
+ sum
+ }
+ }
+
+ #[cfg(test)]
+ mod tests {
+
+ use super::*;
+
+ #[test]
+ fn it_works() {
+ let mut lt = LoadTester::new();
+ lt.bloat(4);
+ assert_eq!(lt.get(), [1,2,3,4]);
+ lt.bloat(3);
+ assert_eq!(lt.get(), [1,2,3,4,1,2,3]);
+ }
+ }
+}
tests/package.jsondiffbeforeafterboth--- a/tests/package.json
+++ b/tests/package.json
@@ -16,7 +16,8 @@
"typescript": "^3.9.7"
},
"scripts": {
- "test": "mocha --timeout 9999999 -r ts-node/register ./**/*.test.ts"
+ "test": "mocha --timeout 9999999 -r ts-node/register ./**/*.test.ts",
+ "load": "mocha --timeout 9999999 -r ts-node/register ./**/*.load.ts"
},
"author": "",
"license": "Apache 2.0",
tests/src/load_test_sc/loadtester.wasmdiffbeforeafterbothbinary blob — no preview
tests/src/load_test_sc/metadata.jsondiffbeforeafterboth--- /dev/null
+++ b/tests/src/load_test_sc/metadata.json
@@ -0,0 +1,125 @@
+{
+ "metadataVersion": "0.1.0",
+ "source": {
+ "hash": "0x168cc3cba9657ad3950fb506e568751f99b90fb097685107f6101675662a8303",
+ "language": "ink! 3.0.0-rc2",
+ "compiler": "rustc 1.49.0-nightly"
+ },
+ "contract": {
+ "name": "loadtester",
+ "version": "0.1.0",
+ "authors": [
+ "[your_name] <[your_email]>"
+ ]
+ },
+ "spec": {
+ "constructors": [
+ {
+ "args": [],
+ "docs": [],
+ "name": [
+ "new"
+ ],
+ "selector": "0xd183512b"
+ }
+ ],
+ "docs": [],
+ "events": [],
+ "messages": [
+ {
+ "args": [
+ {
+ "name": "count",
+ "type": {
+ "displayName": [
+ "u64"
+ ],
+ "type": 2
+ }
+ }
+ ],
+ "docs": [],
+ "mutates": true,
+ "name": [
+ "bloat"
+ ],
+ "payable": false,
+ "returnType": null,
+ "selector": "0x49891c2a"
+ },
+ {
+ "args": [],
+ "docs": [],
+ "mutates": false,
+ "name": [
+ "get"
+ ],
+ "payable": false,
+ "returnType": {
+ "displayName": [
+ "u128"
+ ],
+ "type": 3
+ },
+ "selector": "0x1e5ca456"
+ }
+ ]
+ },
+ "storage": {
+ "struct": {
+ "fields": [
+ {
+ "layout": {
+ "struct": {
+ "fields": [
+ {
+ "layout": {
+ "cell": {
+ "key": "0x0000000000000000000000000000000000000000000000000000000000000000",
+ "ty": 1
+ }
+ },
+ "name": "len"
+ },
+ {
+ "layout": {
+ "array": {
+ "cellsPerElem": 1,
+ "layout": {
+ "cell": {
+ "key": "0x0000000001000000000000000000000000000000000000000000000000000000",
+ "ty": 2
+ }
+ },
+ "len": 4294967295,
+ "offset": "0x0100000000000000000000000000000000000000000000000000000000000000"
+ }
+ },
+ "name": "elems"
+ }
+ ]
+ }
+ },
+ "name": "vector"
+ }
+ ]
+ }
+ },
+ "types": [
+ {
+ "def": {
+ "primitive": "u32"
+ }
+ },
+ {
+ "def": {
+ "primitive": "u64"
+ }
+ },
+ {
+ "def": {
+ "primitive": "u128"
+ }
+ }
+ ]
+}
\ No newline at end of file
tests/src/rpc.load.tsdiffbeforeafterboth1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56import { expect, assert } from "chai";7import usingApi, { submitTransactionAsync, submitTransactionExpectFailAsync } from "./substrate/substrate-api";8import { IKeyringPair } from "@polkadot/types/types";9import { Abi, BlueprintPromise as Blueprint, CodePromise, ContractPromise as Contract } from "@polkadot/api-contract";10import { ApiPromise, Keyring } from "@polkadot/api";11import { ApiTypes, SubmittableExtrinsic } from "@polkadot/api/types";12import { BigNumber } from 'bignumber.js';13import { findUnusedAddress } from './util/helpers'14import fs from "fs";15import privateKey from "./substrate/privateKey";1617const value = 0;18const gasLimit = 500000n * 1000000n;19const endowment = `1000000000000000`;202122function deployBlueprint(alice: IKeyringPair, code: CodePromise): Promise<Blueprint> {23 return new Promise<Blueprint>(async (resolve, reject) => {24 const unsub = await code25 .createBlueprint()26 .signAndSend(alice, (result) => {27 if (result.status.isInBlock || result.status.isFinalized) {28 // here we have an additional field in the result, containing the blueprint29 resolve(result.blueprint);30 unsub();31 }32 })33 });34}3536function deployContract(alice: IKeyringPair, blueprint: Blueprint) : Promise<any> {37 return new Promise<any>(async (resolve, reject) => {38 const unsub = await blueprint.tx39 .new(endowment, gasLimit)40 .signAndSend(alice, (result) => {41 if (result.status.isInBlock || result.status.isFinalized) {42 unsub();43 resolve(result);44 }45 }); 46 });47}4849async function prepareDeployer(api: ApiPromise) {50 // Find unused address51 const deployer = await findUnusedAddress(api);5253 // Transfer balance to it54 const keyring = new Keyring({ type: 'sr25519' });55 const alice = keyring.addFromUri(`//Alice`);56 let amount = new BigNumber(endowment);57 amount = amount.plus(1e15);58 const tx = api.tx.balances.transfer(deployer.address, amount.toFixed());59 await submitTransactionAsync(alice, tx);6061 return deployer;62}6364async function deployLoadTester(api: ApiPromise): Promise<[Contract, IKeyringPair]> {65 const metadata = JSON.parse(fs.readFileSync('./src/load_test_sc/metadata.json').toString('utf-8'));66 const abi = new Abi(metadata);6768 const deployer = await prepareDeployer(api);6970 const wasm = fs.readFileSync('./src/load_test_sc/loadtester.wasm');7172 const code = new CodePromise(api, abi, wasm);7374 const blueprint = await deployBlueprint(deployer, code);75 const contract = (await deployContract(deployer, blueprint))['contract'] as Contract;7677 return [contract, deployer];78}7980async function getScData(contract: Contract, deployer: IKeyringPair) {81 const result = await contract.query.get(deployer.address, value, gasLimit);8283 if(!result.result.isSuccess) {84 throw `Failed to get value`;85 }86 return result.result.asSuccess.data;87}888990describe('RPC Tests', () => {91 it('Simple RPC Load Test', async () => {92 await usingApi(async api => {93 let count = 0;94 let hrTime = process.hrtime();95 let microsec1 = hrTime[0] * 1000000 + hrTime[1] / 1000;96 let rate = 0;97 const checkPoint = 1000;98 while (true) {99 await api.rpc.system.chain();100 count++;101 process.stdout.write(`RPC reads: ${count} times at rate ${rate} r/s \r`);102 103 if (count % checkPoint == 0) {104 hrTime = process.hrtime();105 let microsec2 = hrTime[0] * 1000000 + hrTime[1] / 1000;106 rate = 1000000*checkPoint/(microsec2 - microsec1);107 microsec1 = microsec2;108 }109 }110 });111 });112113 it.only('Smart Contract RPC Load Test', async () => {114 await usingApi(async api => {115116 // Deploy smart contract117 const [contract, deployer] = await deployLoadTester(api);118119 // Fill smart contract up with data120 const bob = privateKey("//Bob");121 const tx = contract.tx.bloat(value, gasLimit, 200);122 await submitTransactionAsync(bob, tx);123124 // Run load test125 let count = 0;126 let hrTime = process.hrtime();127 let microsec1 = hrTime[0] * 1000000 + hrTime[1] / 1000;128 let rate = 0;129 const checkPoint = 10;130 while (true) {131 await getScData(contract, deployer);132 count++;133 process.stdout.write(`SC reads: ${count} times at rate ${rate} r/s \r`);134 135 if (count % checkPoint == 0) {136 hrTime = process.hrtime();137 let microsec2 = hrTime[0] * 1000000 + hrTime[1] / 1000;138 rate = 1000000*checkPoint/(microsec2 - microsec1);139 microsec1 = microsec2;140 }141 }142 });143 });144145});