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
+1 -1
View File
@@ -57,7 +57,7 @@ use params::{
pub use params::{
SharedParams, ImportParams, ExecutionStrategy, Subcommand, RunCmd, BuildSpecCmd,
ExportBlocksCmd, ImportBlocksCmd, CheckBlockCmd, PurgeChainCmd, RevertCmd,
BenchmarkCmd,
WasmExecutionMethod,
};
pub use traits::GetSharedParams;
use app_dirs::{AppInfo, AppDataType};
+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(())
}
}
+8 -64
View File
@@ -22,20 +22,17 @@ use crate::error::Error;
use sc_chain_spec::{ChainSpec, RuntimeGenesis, Extension};
use log::{warn, info};
use futures::{future, prelude::*};
use sp_runtime::{
BuildStorage, BenchmarkResults,
traits::{
Block as BlockT, NumberFor, One, Zero, Header, SaturatedConversion
}
use sp_runtime::traits::{
Block as BlockT, NumberFor, One, Zero, Header, SaturatedConversion
};
use sp_runtime::generic::{BlockId, SignedBlock};
use codec::{Decode, Encode, IoReader};
use sc_client::{Client, ExecutionStrategy, StateMachine, LocalCallExecutor};
#[cfg(feature = "rocksdb")]
use sc_client_db::BenchmarkingState;
use sp_consensus::import_queue::{IncomingBlock, Link, BlockImportError, BlockImportResult, ImportQueue};
use sp_consensus::BlockOrigin;
use sc_executor::{NativeExecutor, NativeExecutionDispatch, WasmExecutionMethod};
use sc_client::{Client, LocalCallExecutor};
use sp_consensus::{
BlockOrigin,
import_queue::{IncomingBlock, Link, BlockImportError, BlockImportResult, ImportQueue},
};
use sc_executor::{NativeExecutor, NativeExecutionDispatch};
use std::{io::{Read, Write, Seek}, pin::Pin};
@@ -49,59 +46,6 @@ pub fn build_spec<G, E>(spec: ChainSpec<G, E>, raw: bool) -> error::Result<Strin
Ok(spec.to_json(raw)?)
}
/// Run runtime benchmarks.
#[cfg(feature = "rocksdb")]
pub fn benchmark_runtime<TBl, TExecDisp, G, E> (
spec: ChainSpec<G, E>,
strategy: ExecutionStrategy,
wasm_method: WasmExecutionMethod,
pallet: String,
extrinsic: String,
steps: u32,
repeat: u32,
) -> error::Result<()> where
TBl: BlockT,
TExecDisp: NativeExecutionDispatch + 'static,
G: RuntimeGenesis,
E: Extension,
{
let genesis_storage = spec.build_storage()?;
let mut changes = Default::default();
let state = BenchmarkingState::<TBl>::new(genesis_storage)?;
let executor = NativeExecutor::<TExecDisp>::new(
wasm_method,
None, // heap pages
);
let result = StateMachine::<_, _, NumberFor<TBl>, _>::new(
&state,
None,
&mut changes,
&executor,
"Benchmark_dispatch_benchmark",
&(&pallet, &extrinsic, steps, repeat).encode(),
Default::default(),
).execute(strategy).map_err(|e| format!("Error executing runtime benchmark: {:?}", e))?;
let results = <Option<Vec<BenchmarkResults>> as Decode>::decode(&mut &result[..]).unwrap_or(None);
if let Some(results) = results {
// Print benchmark metadata
println!("Pallet: {:?}, Extrinsic: {:?}, Steps: {:?}, Repeat: {:?}", pallet, extrinsic, steps, repeat);
// Print the table header
results[0].0.iter().for_each(|param| print!("{:?},", param.0));
print!("time\n");
// Print the values
results.iter().for_each(|result| {
let parameters = &result.0;
parameters.iter().for_each(|param| print!("{:?},", param.1));
print!("{:?}\n", result.1);
});
info!("Done.");
} else {
info!("No Results.");
}
Ok(())
}
impl<
TBl, TRtApi, TGen, TCSExt, TBackend,
TExecDisp, TFchr, TSc, TImpQu, TFprb, TFpp, TNetP,