diff --git a/utils/frame/benchmarking-cli/src/overhead/README.md b/utils/frame/benchmarking-cli/src/overhead/README.md index 6f41e881d0572..8605367ea4151 100644 --- a/utils/frame/benchmarking-cli/src/overhead/README.md +++ b/utils/frame/benchmarking-cli/src/overhead/README.md @@ -2,7 +2,7 @@ Each time an extrinsic or a block is executed, a fixed weight is charged as "execution overhead". This is necessary since the weight that is calculated by the pallet benchmarks does not include this overhead. -The exact overhead to can vary per Substrate chain and needs to be calculated per chain. +The exact overhead can vary per Substrate chain and needs to be calculated per chain. This command calculates the exact values of these overhead weights for any Substrate chain that supports it. ## How does it work? diff --git a/utils/frame/benchmarking-cli/src/overhead/cmd.rs b/utils/frame/benchmarking-cli/src/overhead/cmd.rs index 357c89d97a5ac..2a7ce4062ece8 100644 --- a/utils/frame/benchmarking-cli/src/overhead/cmd.rs +++ b/utils/frame/benchmarking-cli/src/overhead/cmd.rs @@ -23,12 +23,12 @@ use sc_cli::{CliConfiguration, ImportParams, Result, SharedParams}; use sc_client_api::Backend as ClientBackend; use sc_service::Configuration; use sp_api::{ApiExt, ProvideRuntimeApi}; -use sp_runtime::{traits::Block as BlockT, OpaqueExtrinsic}; +use sp_runtime::{traits::{ Block as BlockT, Header as HeaderT}, OpaqueExtrinsic}; use clap::{Args, Parser}; use log::info; use serde::Serialize; -use std::{fmt::Debug, sync::Arc}; +use std::{fmt::Debug, sync::Arc, path::PathBuf}; use crate::{ overhead::{ @@ -52,6 +52,10 @@ pub struct OverheadCmd { #[allow(missing_docs)] #[clap(flatten)] pub params: OverheadParams, + + /// Add a header file to your outputted benchmarks. + #[clap(long)] + pub header: Option, } /// Configures the benchmark, the post-processing and weight generation. @@ -96,21 +100,28 @@ impl OverheadCmd { BA: ClientBackend, C: BlockBuilderProvider + ProvideRuntimeApi, C::Api: ApiExt + BlockBuilderApi, + <<::Header as HeaderT>::Number as std::str::FromStr>::Err: std::fmt::Debug, { + if let Some(header_file) = &self.header { + if !header_file.is_file() { + return Err("Header file is invalid!".into()) + }; + } + let bench = Benchmark::new(client, self.params.bench.clone(), inherent_data, ext_builder); // per-block execution overhead { let stats = bench.bench(BenchmarkType::Block)?; info!("Per-block execution overhead [ns]:\n{:?}", stats); - let template = TemplateData::new(BenchmarkType::Block, &cfg, &self.params, &stats)?; + let template = TemplateData::new(BenchmarkType::Block, &cfg, &self, &stats)?; template.write(&self.params.weight.weight_path)?; } // per-extrinsic execution overhead { let stats = bench.bench(BenchmarkType::Extrinsic)?; info!("Per-extrinsic execution overhead [ns]:\n{:?}", stats); - let template = TemplateData::new(BenchmarkType::Extrinsic, &cfg, &self.params, &stats)?; + let template = TemplateData::new(BenchmarkType::Extrinsic, &cfg, &self, &stats)?; template.write(&self.params.weight.weight_path)?; } diff --git a/utils/frame/benchmarking-cli/src/overhead/template.rs b/utils/frame/benchmarking-cli/src/overhead/template.rs index 33c2c7999039a..ae22244fb445c 100644 --- a/utils/frame/benchmarking-cli/src/overhead/template.rs +++ b/utils/frame/benchmarking-cli/src/overhead/template.rs @@ -26,10 +26,7 @@ use log::info; use serde::Serialize; use std::{env, fs, path::PathBuf}; -use crate::{ - overhead::{bench::BenchmarkType, cmd::OverheadParams}, - shared::{Stats, UnderscoreHelper}, -}; +use crate::{overhead::{bench::BenchmarkType, cmd::OverheadParams}, OverheadCmd, shared::{Stats, UnderscoreHelper}}; static VERSION: &str = env!("CARGO_PKG_VERSION"); static TEMPLATE: &str = include_str!("./weights.hbs"); @@ -59,6 +56,10 @@ pub(crate) struct TemplateData { stats: Stats, /// The resulting weight in ns. weight: u64, + /// Contents of the header file provided in the params of the executed command + header: String, + /// The Execution Strategy used + execution_strategy: String, } impl TemplateData { @@ -66,11 +67,20 @@ impl TemplateData { pub(crate) fn new( t: BenchmarkType, cfg: &Configuration, - params: &OverheadParams, + cmd: &OverheadCmd, stats: &Stats, ) -> Result { + let params = &cmd.params; let weight = params.weight.calc_weight(stats)?; + let header_text = match &cmd.header { + Some(header_file) => { + let text = fs::read_to_string(header_file)?; + text + }, + None => String::new(), + }; + Ok(TemplateData { short_name: t.short_name().into(), long_name: t.long_name().into(), @@ -83,6 +93,8 @@ impl TemplateData { params: params.clone(), stats: stats.clone(), weight, + header: header_text.clone(), + execution_strategy: cmd.import_params.wasm_method.to_string() }) } diff --git a/utils/frame/benchmarking-cli/src/overhead/weights.hbs b/utils/frame/benchmarking-cli/src/overhead/weights.hbs index f8312b0052592..6983261c357e9 100644 --- a/utils/frame/benchmarking-cli/src/overhead/weights.hbs +++ b/utils/frame/benchmarking-cli/src/overhead/weights.hbs @@ -1,3 +1,4 @@ +{{header}} // This file is part of Substrate. // Copyright (C) 2022 Parity Technologies (UK) Ltd. @@ -23,6 +24,7 @@ //! WARMUPS: `{{params.bench.warmup}}`, REPEAT: `{{params.bench.repeat}}` //! WEIGHT-PATH: `{{params.weight.weight_path}}` //! WEIGHT-METRIC: `{{params.weight.weight_metric}}`, WEIGHT-MUL: `{{params.weight.weight_mul}}`, WEIGHT-ADD: `{{params.weight.weight_add}}` +//! WASM-EXECUTION-METHOD: `{{execution_strategy}}` // Executed Command: {{#each args as |arg|}} diff --git a/utils/frame/benchmarking-cli/src/shared/weight_params.rs b/utils/frame/benchmarking-cli/src/shared/weight_params.rs index 4dd80cd41ff3d..6413a238d772b 100644 --- a/utils/frame/benchmarking-cli/src/shared/weight_params.rs +++ b/utils/frame/benchmarking-cli/src/shared/weight_params.rs @@ -49,6 +49,10 @@ pub struct WeightParams { /// Is applied after `weight_mul`. #[clap(long = "add", default_value = "0")] pub weight_add: u64, + + /// The block weight key + #[clap(long = "key", default_value = "26aa394eea5630e07c48ae0c9558cef734abf5cb34d6244378cddbf18e849d96")] + pub block_weight_key: String, } /// Calculates the final weight by multiplying the selected metric with