Consolidate frame benchmarking into a frame crate (#4977)

This prs cleans up some of the frame benchmarking stuff:
- Move CLI into `frame-benchmarking-cli`. No frame related CLI should
exists in the default Substrate CLI.
- Move all traits and types related to frame benchmarking into the
`frame-benchmarking` trait. Frame types should be isolated in Frame.
This commit is contained in:
Bastian Köcher
2020-02-19 10:22:36 +01:00
committed by GitHub
parent e50f610907
commit b4ebd41c21
25 changed files with 484 additions and 329 deletions
+25 -95
View File
@@ -47,28 +47,34 @@ impl Into<sc_client_api::ExecutionStrategy> for ExecutionStrategy {
}
}
arg_enum! {
/// How to execute Wasm runtime code
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy)]
pub enum WasmExecutionMethod {
// Uses an interpreter.
Interpreted,
// Uses a compiled runtime.
Compiled,
#[allow(missing_docs)]
mod wasm_execution_method {
use super::*;
arg_enum! {
/// How to execute Wasm runtime code
#[derive(Debug, Clone, Copy)]
pub enum WasmExecutionMethod {
// Uses an interpreter.
Interpreted,
// Uses a compiled runtime.
Compiled,
}
}
impl WasmExecutionMethod {
/// Returns list of variants that are not disabled by feature flags.
pub fn enabled_variants() -> Vec<&'static str> {
Self::variants()
.iter()
.cloned()
.filter(|&name| cfg!(feature = "wasmtime") || name != "Compiled")
.collect()
}
}
}
impl WasmExecutionMethod {
/// Returns list of variants that are not disabled by feature flags.
fn enabled_variants() -> Vec<&'static str> {
Self::variants()
.iter()
.cloned()
.filter(|&name| cfg!(feature = "wasmtime") || name != "Compiled")
.collect()
}
}
pub use wasm_execution_method::WasmExecutionMethod;
impl Into<sc_service::config::WasmExecutionMethod> for WasmExecutionMethod {
fn into(self) -> sc_service::config::WasmExecutionMethod {
@@ -849,49 +855,6 @@ pub struct PurgeChainCmd {
pub shared_params: SharedParams,
}
/// The `benchmark` command used to benchmark FRAME Pallets.
#[derive(Debug, StructOpt, Clone)]
pub struct BenchmarkCmd {
/// Select a FRAME Pallet to benchmark.
#[structopt(short, long)]
pub pallet: String,
/// Select an extrinsic to benchmark.
#[structopt(short, long)]
pub extrinsic: String,
/// Select how many samples we should take across the variable components.
#[structopt(short, long, default_value = "1")]
pub steps: u32,
/// Select how many repetitions of this benchmark should run.
#[structopt(short, long, default_value = "1")]
pub repeat: u32,
#[allow(missing_docs)]
#[structopt(flatten)]
pub shared_params: SharedParams,
/// The execution strategy that should be used for benchmarks
#[structopt(
long = "execution",
value_name = "STRATEGY",
possible_values = &ExecutionStrategy::variants(),
case_insensitive = true,
)]
pub execution: Option<ExecutionStrategy>,
/// Method for executing Wasm runtime code.
#[structopt(
long = "wasm-execution",
value_name = "METHOD",
possible_values = &WasmExecutionMethod::enabled_variants(),
case_insensitive = true,
default_value = "Interpreted"
)]
pub wasm_method: WasmExecutionMethod,
}
/// All core commands that are provided by default.
///
/// The core commands are split into multiple subcommands and `Run` is the default subcommand. From
@@ -916,9 +879,6 @@ pub enum Subcommand {
/// Remove the whole chain data.
PurgeChain(PurgeChainCmd),
/// Run runtime benchmarks.
Benchmark(BenchmarkCmd),
}
impl Subcommand {
@@ -933,7 +893,6 @@ impl Subcommand {
CheckBlock(params) => &params.shared_params,
Revert(params) => &params.shared_params,
PurgeChain(params) => &params.shared_params,
Benchmark(params) => &params.shared_params,
}
}
@@ -960,7 +919,6 @@ impl Subcommand {
Subcommand::ImportBlocks(cmd) => cmd.run(config, builder),
Subcommand::CheckBlock(cmd) => cmd.run(config, builder),
Subcommand::PurgeChain(cmd) => cmd.run(config),
Subcommand::Benchmark(cmd) => cmd.run(config, builder),
Subcommand::Revert(cmd) => cmd.run(config, builder),
}
}
@@ -1238,31 +1196,3 @@ impl RevertCmd {
Ok(())
}
}
impl BenchmarkCmd {
/// Runs the command and benchmarks the chain.
pub fn run<G, E, B, BC, BB>(
self,
config: Configuration<G, E>,
_builder: B,
) -> error::Result<()>
where
B: FnOnce(Configuration<G, E>) -> Result<BC, sc_service::error::Error>,
G: RuntimeGenesis,
E: ChainSpecExtension,
BC: ServiceBuilderCommand<Block = BB> + Unpin,
BB: sp_runtime::traits::Block + Debug,
<<<BB as BlockT>::Header as HeaderT>::Number as std::str::FromStr>::Err: std::fmt::Debug,
<BB as BlockT>::Hash: std::str::FromStr,
{
let spec = config.chain_spec.expect("chain_spec is always Some");
let execution_strategy = self.execution.unwrap_or(ExecutionStrategy::Native).into();
let wasm_method = self.wasm_method.into();
let pallet = self.pallet;
let extrinsic = self.extrinsic;
let steps = self.steps;
let repeat = self.repeat;
sc_service::chain_ops::benchmark_runtime::<BB, BC::NativeDispatch, _, _>(spec, execution_strategy, wasm_method, pallet, extrinsic, steps, repeat)?;
Ok(())
}
}