Skip to content

Commit

Permalink
Added pallet benchmarking command
Browse files Browse the repository at this point in the history
  • Loading branch information
akru committed Feb 28, 2020
1 parent ccd2582 commit d2f5919
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 21 deletions.
22 changes: 21 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ sc-rpc = { git = "https://github.com/paritytech/substrate" }

# CLI-specific dependencies
sc-cli = { git = "https://github.com/paritytech/substrate", optional = true }
frame-benchmarking-cli = { git = "https://github.com/paritytech/substrate", optional = true }

# Browser bundle build support
wasm-bindgen = { version = "0.2.57", optional = true }
Expand All @@ -90,6 +91,7 @@ robonomics-ros-api = { path = "../../../robonomics/ros-api", optional = true }

[build-dependencies]
sc-cli = { git = "https://github.com/paritytech/substrate", optional = true }
frame-benchmarking-cli = { git = "https://github.com/paritytech/substrate", optional = true }
substrate-build-script-utils = { git = "https://github.com/paritytech/substrate" }
vergen = { version = "3.0.4", optional = true }
structopt = { version = "0.3.8", optional = true }
Expand All @@ -109,6 +111,7 @@ cli = [
"vergen",
"structopt",
"sc-service/rocksdb",
"frame-benchmarking-cli",
]

## Enable browser bundle features.
Expand Down
4 changes: 2 additions & 2 deletions bin/node/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ fn main() {
#[cfg(feature = "cli")]
mod cli {
include!("src/cli.rs");
use structopt::clap::Shell;
use std::{fs, env, path::Path};
use sc_cli::{structopt::clap::Shell, RunCmd};
use vergen::{ConstantsFlags, generate_cargo_keys};

pub fn main() {
Expand Down Expand Up @@ -57,6 +57,6 @@ mod cli {

fs::create_dir(&path).ok();

RunCmd::clap().gen_completions("robonomics", *shell, &path);
sc_cli::RunCmd::clap().gen_completions("robonomics", *shell, &path);
}
}
33 changes: 17 additions & 16 deletions bin/node/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,27 @@

use structopt::StructOpt;

#[allow(missing_docs)]
/// An overarching CLI command definition.
#[derive(Clone, Debug, StructOpt)]
#[structopt(settings = &[
structopt::clap::AppSettings::GlobalVersion,
structopt::clap::AppSettings::ArgsNegateSubcommands,
structopt::clap::AppSettings::SubcommandsNegateReqs,
])]
pub struct Cli {
#[allow(missing_docs)]
#[structopt(subcommand)]
pub subcommand: Option<Subcommand>,
#[allow(missing_docs)]
#[structopt(flatten)]
pub run: sc_cli::RunCmd,
/// Possible subcommand with parameters.
#[structopt(subcommand)]
pub subcommand: Option<Subcommand>,
#[allow(missing_docs)]
#[structopt(flatten)]
pub run: sc_cli::RunCmd,
}

#[allow(missing_docs)]
/// Possible subcommands of the main binary.
#[derive(Clone, Debug, StructOpt)]
pub enum Subcommand {
#[allow(missing_docs)]
#[structopt(flatten)]
Base(sc_cli::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),
}
11 changes: 11 additions & 0 deletions bin/node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ pub fn run(version: VersionInfo) -> sc_cli::Result<()> {
}
}
},
Some(Subcommand::Benchmark(cmd)) => {
cmd.init(&version)?;
cmd.update_config(&mut config, load_spec, &version)?;

let is_ipci = config.chain_spec.as_ref().map_or(false, |s| s.is_ipci());
if is_ipci {
cmd.run::<_, _, node_primitives::Block, IpciExecutor>(config)
} else {
cmd.run::<_, _, node_primitives::Block, RobonomicsExecutor>(config)
}
},
Some(Subcommand::Base(cmd)) => {
cmd.init(&version)?;
cmd.update_config(&mut config, load_spec, &version)?;
Expand Down
2 changes: 2 additions & 0 deletions bin/node/ipci-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pallet-staking = { git = "https://github.com/paritytech/substrate", default-feat
pallet-staking-reward-curve = { git = "https://github.com/paritytech/substrate" }
pallet-sudo = { git = "https://github.com/paritytech/substrate", default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", default-features = false }
frame-system-rpc-runtime-api = { git = "https://github.com/paritytech/substrate", default-features = false }
pallet-utility = { git = "https://github.com/paritytech/substrate", default-features = false }
Expand Down Expand Up @@ -90,6 +91,7 @@ std = [
"sp-session/std",
"pallet-sudo/std",
"frame-support/std",
"frame-benchmarking/std",
"frame-system/std",
"frame-system-rpc-runtime-api/std",
"pallet-utility/std",
Expand Down
83 changes: 82 additions & 1 deletion bin/node/ipci-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use frame_support::{
traits::Randomness, weights::Weight,
};
use sp_runtime::{
ApplyExtrinsicResult, Perbill,
ApplyExtrinsicResult, Perbill, RuntimeString,
generic, create_runtime_str, impl_opaque_keys,
};
use sp_runtime::transaction_validity::TransactionValidity;
Expand Down Expand Up @@ -566,4 +566,85 @@ impl_runtime_apis! {
SessionKeys::decode_into_raw_public_keys(&encoded)
}
}

impl frame_benchmarking::Benchmark<Block> for Runtime {
fn dispatch_benchmark(
module: Vec<u8>,
extrinsic: Vec<u8>,
steps: Vec<u32>,
repeat: u32,
) -> Result<Vec<frame_benchmarking::BenchmarkResults>, RuntimeString> {
use frame_benchmarking::Benchmarking;

let result = match module.as_slice() {
b"pallet-balances" | b"balances" => Balances::run_benchmark(extrinsic, steps, repeat),
b"pallet-identity" | b"identity" => Identity::run_benchmark(extrinsic, steps, repeat),
b"pallet-timestamp" | b"timestamp" => Timestamp::run_benchmark(extrinsic, steps, repeat),
_ => Err("Benchmark not found for this pallet."),
};

result.map_err(|e| e.into())
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use frame_system::offchain::{SignAndSubmitTransaction, SubmitSignedTransaction};

#[test]
fn validate_transaction_submitter_bounds() {
fn is_submit_signed_transaction<T>() where
T: SubmitSignedTransaction<
Runtime,
Call,
>,
{}

fn is_sign_and_submit_transaction<T>() where
T: SignAndSubmitTransaction<
Runtime,
Call,
Extrinsic=UncheckedExtrinsic,
CreateTransaction=Runtime,
Signer=ImOnlineId,
>,
{}

is_submit_signed_transaction::<SubmitTransaction>();
is_sign_and_submit_transaction::<SubmitTransaction>();
}

#[test]
fn block_hooks_weight_should_not_exceed_limits() {
use frame_support::weights::WeighBlock;
let check_for_block = |b| {
let block_hooks_weight =
<AllModules as WeighBlock<BlockNumber>>::on_initialize(b) +
<AllModules as WeighBlock<BlockNumber>>::on_finalize(b);

assert_eq!(
block_hooks_weight,
0,
"This test might fail simply because the value being compared to has increased to a \
module declaring a new weight for a hook or call. In this case update the test and \
happily move on.",
);

// Invariant. Always must be like this to have a sane chain.
assert!(block_hooks_weight < MaximumBlockWeight::get());

// Warning.
if block_hooks_weight > MaximumBlockWeight::get() / 2 {
println!(
"block hooks weight is consuming more than a block's capacity. You probably want \
to re-think this. This test will fail now."
);
assert!(false);
}
};

let _ = (0..100_000).for_each(check_for_block);
}
}
2 changes: 2 additions & 0 deletions bin/node/robonomics-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pallet-staking = { git = "https://github.com/paritytech/substrate", default-feat
pallet-staking-reward-curve = { git = "https://github.com/paritytech/substrate" }
pallet-sudo = { git = "https://github.com/paritytech/substrate", default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", default-features = false }
frame-system-rpc-runtime-api = { git = "https://github.com/paritytech/substrate", default-features = false }
pallet-utility = { git = "https://github.com/paritytech/substrate", default-features = false }
Expand Down Expand Up @@ -91,6 +92,7 @@ std = [
"sp-session/std",
"pallet-sudo/std",
"frame-support/std",
"frame-benchmarking/std",
"frame-system/std",
"frame-system-rpc-runtime-api/std",
"pallet-utility/std",
Expand Down
83 changes: 82 additions & 1 deletion bin/node/robonomics-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use frame_support::{
traits::Randomness, weights::Weight,
};
use sp_runtime::{
ApplyExtrinsicResult, Perbill,
ApplyExtrinsicResult, Perbill, RuntimeString,
generic, create_runtime_str, impl_opaque_keys,
};
use sp_runtime::transaction_validity::TransactionValidity;
Expand Down Expand Up @@ -590,4 +590,85 @@ impl_runtime_apis! {
SessionKeys::decode_into_raw_public_keys(&encoded)
}
}

impl frame_benchmarking::Benchmark<Block> for Runtime {
fn dispatch_benchmark(
module: Vec<u8>,
extrinsic: Vec<u8>,
steps: Vec<u32>,
repeat: u32,
) -> Result<Vec<frame_benchmarking::BenchmarkResults>, RuntimeString> {
use frame_benchmarking::Benchmarking;

let result = match module.as_slice() {
b"pallet-balances" | b"balances" => Balances::run_benchmark(extrinsic, steps, repeat),
b"pallet-identity" | b"identity" => Identity::run_benchmark(extrinsic, steps, repeat),
b"pallet-timestamp" | b"timestamp" => Timestamp::run_benchmark(extrinsic, steps, repeat),
_ => Err("Benchmark not found for this pallet."),
};

result.map_err(|e| e.into())
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use frame_system::offchain::{SignAndSubmitTransaction, SubmitSignedTransaction};

#[test]
fn validate_transaction_submitter_bounds() {
fn is_submit_signed_transaction<T>() where
T: SubmitSignedTransaction<
Runtime,
Call,
>,
{}

fn is_sign_and_submit_transaction<T>() where
T: SignAndSubmitTransaction<
Runtime,
Call,
Extrinsic=UncheckedExtrinsic,
CreateTransaction=Runtime,
Signer=ImOnlineId,
>,
{}

is_submit_signed_transaction::<SubmitTransaction>();
is_sign_and_submit_transaction::<SubmitTransaction>();
}

#[test]
fn block_hooks_weight_should_not_exceed_limits() {
use frame_support::weights::WeighBlock;
let check_for_block = |b| {
let block_hooks_weight =
<AllModules as WeighBlock<BlockNumber>>::on_initialize(b) +
<AllModules as WeighBlock<BlockNumber>>::on_finalize(b);

assert_eq!(
block_hooks_weight,
0,
"This test might fail simply because the value being compared to has increased to a \
module declaring a new weight for a hook or call. In this case update the test and \
happily move on.",
);

// Invariant. Always must be like this to have a sane chain.
assert!(block_hooks_weight < MaximumBlockWeight::get());

// Warning.
if block_hooks_weight > MaximumBlockWeight::get() / 2 {
println!(
"block hooks weight is consuming more than a block's capacity. You probably want \
to re-think this. This test will fail now."
);
assert!(false);
}
};

let _ = (0..100_000).for_each(check_for_block);
}
}

0 comments on commit d2f5919

Please sign in to comment.