Skip to content

Commit

Permalink
Runtime benchmarks: start (paritytech#136)
Browse files Browse the repository at this point in the history
* runtime benchmarks: start

* merge tests + benchmarks infrastructure

* fix compilation

* Fix compilation issues with runtime-benchmark feature flag

Mainly involved pulling in correct dependencies and adding some functions
which were called but didn't yet exist.

* Fix broken compilation for tests

* Move header signing methods into trait

* Move signing related test helpers to own module

* Remove comment about feature flag

* Add constants to tests

* Add top level comment for testing utilities

Co-authored-by: Hernando Castano <castano.ha@gmail.com>
  • Loading branch information
2 people authored and serban300 committed Apr 9, 2024
1 parent 2f24d16 commit 8206c86
Show file tree
Hide file tree
Showing 20 changed files with 950 additions and 534 deletions.
21 changes: 21 additions & 0 deletions bridges/bin/node/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ version = "2.0.0-rc3"
rev = "606c56d2e2f69f68f3947551224be6a3515dff60"
git = "https://github.com/paritytech/substrate.git"

[dependencies.frame-benchmarking]
version = "2.0.0-rc3"
rev = "606c56d2e2f69f68f3947551224be6a3515dff60"
git = "https://github.com/paritytech/substrate.git"

[dependencies.frame-benchmarking-cli]
version = "2.0.0-rc3"
rev = "606c56d2e2f69f68f3947551224be6a3515dff60"
git = "https://github.com/paritytech/substrate.git"

[build-dependencies]
vergen = "3.1.0"

Expand All @@ -123,3 +133,14 @@ package = "substrate-build-script-utils"
version = "2.0.0-rc3"
rev = "606c56d2e2f69f68f3947551224be6a3515dff60"
git = "https://github.com/paritytech/substrate.git"

[build-dependencies.frame-benchmarking-cli]
version = "2.0.0-rc3"
rev = "606c56d2e2f69f68f3947551224be6a3515dff60"
git = "https://github.com/paritytech/substrate.git"

[features]
default = []
runtime-benchmarks = [
"bridge-node-runtime/runtime-benchmarks",
]
14 changes: 13 additions & 1 deletion bridges/bin/node/node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.

use sc_cli::{RunCmd, Subcommand};
use sc_cli::RunCmd;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
Expand All @@ -25,3 +25,15 @@ pub struct Cli {
#[structopt(flatten)]
pub run: RunCmd,
}

/// Possible subcommands of the main binary.
#[derive(Debug, StructOpt)]
pub enum Subcommand {
/// A set of base subcommands handled by `sc_cli`.
#[structopt(flatten)]
Base(sc_cli::Subcommand),

/// The custom benchmark subcommmand benchmarking runtime pallets.
#[structopt(name = "benchmark", about = "Benchmark runtime pallets.")]
Benchmark(frame_benchmarking_cli::BenchmarkCmd),
}
18 changes: 16 additions & 2 deletions bridges/bin/node/node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.

use crate::cli::Cli;
use crate::cli::{Cli, Subcommand};
use crate::service;
use bridge_node_runtime::Block;
use sc_cli::SubstrateCli;
use sp_consensus_aura::sr25519::AuthorityPair as AuraPair;

Expand Down Expand Up @@ -81,7 +82,20 @@ pub fn run() -> sc_cli::Result<()> {
let cli = Cli::from_args();

match &cli.subcommand {
Some(subcommand) => {
Some(Subcommand::Benchmark(cmd)) => {
if cfg!(feature = "runtime-benchmarks") {
let runner = cli.create_runner(cmd)?;

runner.sync_run(|config| cmd.run::<Block, service::Executor>(config))
} else {
println!(
"Benchmarking wasn't enabled when building the node. \
You can enable it with `--features runtime-benchmarks`."
);
Ok(())
}
}
Some(Subcommand::Base(subcommand)) => {
let runner = cli.create_runner(subcommand)?;
runner.run_subcommand(subcommand, |config| Ok(new_full_start!(config).0))
}
Expand Down
1 change: 1 addition & 0 deletions bridges/bin/node/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ native_executor_instance!(
pub Executor,
bridge_node_runtime::api::dispatch,
bridge_node_runtime::native_version,
frame_benchmarking::benchmarking::HostFunctions,
);

/// Starts a `ServiceBuilder` for a full service.
Expand Down
16 changes: 16 additions & 0 deletions bridges/bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ default-features = false
rev = "606c56d2e2f69f68f3947551224be6a3515dff60"
git = "https://github.com/paritytech/substrate/"

[dependencies.frame-benchmarking]
optional = true
version = "2.0.0-rc3"
default-features = false
rev = "606c56d2e2f69f68f3947551224be6a3515dff60"
git = "https://github.com/paritytech/substrate/"

[build-dependencies.wasm-builder-runner]
version = "1.0.5"
package = "substrate-wasm-builder-runner"
Expand All @@ -209,6 +216,7 @@ std = [
"pallet-bridge-eth-poa/std",
"pallet-bridge-currency-exchange/std",
"codec/std",
"frame-benchmarking/std",
"frame-executive/std",
"frame-support/std",
"frame-system/std",
Expand All @@ -234,3 +242,11 @@ std = [
"pallet-timestamp/std",
"pallet-transaction-payment/std",
]
runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-bridge-currency-exchange/runtime-benchmarks",
"pallet-bridge-eth-poa/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
2 changes: 1 addition & 1 deletion bridges/bin/node/runtime/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use wasm_builder_runner::WasmBuilder;
fn main() {
WasmBuilder::new()
.with_current_project()
.with_wasm_builder_from_crates("1.0.9")
.with_wasm_builder_from_crates("1.0.11")
.export_heap_base()
.import_memory()
.build()
Expand Down
21 changes: 21 additions & 0 deletions bridges/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,27 @@ impl_runtime_apis! {
None
}
}

#[cfg(feature = "runtime-benchmarks")]
impl frame_benchmarking::Benchmark<Block> for Runtime {
fn dispatch_benchmark(
pallet: Vec<u8>,
benchmark: Vec<u8>,
lowest_range_values: Vec<u32>,
highest_range_values: Vec<u32>,
steps: Vec<u32>,
repeat: u32,
) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, sp_runtime::RuntimeString> {
use frame_benchmarking::{Benchmarking, BenchmarkBatch, add_benchmark};
let mut batches = Vec::<BenchmarkBatch>::new();
let params = (&pallet, &benchmark, &lowest_range_values, &highest_range_values, &steps, repeat);

add_benchmark!(params, batches, b"bridge-eth-poa", BridgeEthPoA);

if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }
Ok(batches)
}
}
}

#[cfg(test)]
Expand Down
15 changes: 12 additions & 3 deletions bridges/modules/currency-exchange/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ default-features = false
rev = "606c56d2e2f69f68f3947551224be6a3515dff60"
git = "https://github.com/paritytech/substrate/"

[dependencies.frame-benchmarking]
optional = true
version = "2.0.0-rc3"
default-features = false
rev = "606c56d2e2f69f68f3947551224be6a3515dff60"
git = "https://github.com/paritytech/substrate/"

[dev-dependencies.sp-core]
version = "2.0.0-rc3"
rev = "606c56d2e2f69f68f3947551224be6a3515dff60"
Expand All @@ -48,11 +55,13 @@ git = "https://github.com/paritytech/substrate/"
[features]
default = ["std"]
std = [
"serde",
"codec/std",
"sp-std/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"sp-runtime/std",
"serde",
"sp-currency-exchange/std",
"sp-std/std",
"sp-runtime/std",
]
runtime-benchmarks = ["frame-benchmarking"]
30 changes: 25 additions & 5 deletions bridges/modules/ethereum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2018"
[dependencies]
serde = { version = "1.0", optional = true }
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false }
hex-literal = "0.2"
primitives = { package = "sp-bridge-eth-poa", path = "../../primitives/ethereum-poa", default-features = false }

# Substrate Based Dependencies
Expand Down Expand Up @@ -41,21 +42,40 @@ default-features = false
rev = "606c56d2e2f69f68f3947551224be6a3515dff60"
git = "https://github.com/paritytech/substrate/"

[dependencies.frame-benchmarking]
optional = true
version = "2.0.0-rc3"
default-features = false
rev = "606c56d2e2f69f68f3947551224be6a3515dff60"
git = "https://github.com/paritytech/substrate/"

[dependencies.libsecp256k1]
optional = true
version = "0.3.4"
default-features = false
features = ["hmac"]

# Dev Dependencies
[dev-dependencies]
# TODO: Stop renaming this on import
primitives = { package = "sp-bridge-eth-poa", path = "../../primitives/ethereum-poa", features = ["std", "test-helpers"] }
parity-crypto = { version = "0.6", features = ["publickey"] }
libsecp256k1 = { version = "0.3.4", features = ["hmac"] }

[features]
default = ["std"]
std = [
"serde",
"codec/std",
"sp-std/std",
"frame-benchmarking/std",
"frame-support/std",
"sp-runtime/std",
"frame-system/std",
"sp-io/std",
"primitives/std",
"serde",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
]
runtime-benchmarks = [
"frame-benchmarking",
"libsecp256k1",
"primitives/test-helpers",
]
60 changes: 60 additions & 0 deletions bridges/modules/ethereum/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
// This file is part of Parity Bridges Common.

// Parity Bridges Common is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity Bridges Common is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.

use super::*;

use crate::test_utils::{build_custom_header, build_genesis_header, validator_utils::*};

use frame_benchmarking::benchmarks;
use frame_system::RawOrigin;
use primitives::U256;

benchmarks! {
_ { }

// Benchmark `import_unsigned_header` extrinsic with the best possible conditions:
// * Parent header is finalized.
// * New header doesn't require receipts.
// * Nothing is finalized by new header.
// * Nothing is pruned by new header.
import_unsigned_header_best_case {
let n in 1..1000;

// initialize storage with some initial header
let initial_header = build_genesis_header(&validator(0));
let initial_header_hash = initial_header.compute_hash();
let initial_difficulty = initial_header.difficulty;
initialize_storage::<T>(
&initial_header,
initial_difficulty,
&validators_addresses(2),
);

// prepare header to be inserted
let header = build_custom_header(
&validator(1),
&initial_header,
|mut header| {
header.gas_limit = header.gas_limit + U256::from(n);
header
},
);

}: import_unsigned_header(RawOrigin::None, header, None)
verify {
assert_eq!(BridgeStorage::<T>::new().best_block().0.number, 1);
}
}
Loading

0 comments on commit 8206c86

Please sign in to comment.