diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index a76dd934ff92c..799fc5397ced6 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -865,58 +865,25 @@ impl_runtime_apis! { steps: Vec, repeat: u32, ) -> Result, sp_runtime::RuntimeString> { - use frame_benchmarking::Benchmarking; + use frame_benchmarking::{Benchmarking, BenchmarkBatch, add_benchmark}; // Trying to add benchmarks directly to the Session Pallet caused cyclic dependency issues. // To get around that, we separated the Session benchmarks into its own crate, which is why // we need these two lines below. use pallet_session_benchmarking::Module as SessionBench; impl pallet_session_benchmarking::Trait for Runtime {} - let mut batches = Vec::::new(); - macro_rules! pallet { - ( $name:literal, $( $location:tt )* ) => ( - if &pallet[..] == &$name[..] || &pallet[..] == &b"*"[..] { - if &pallet[..] == &b"*"[..] || &benchmark[..] == &b"*"[..] { - for benchmark in $( $location )*::benchmarks().into_iter() { - batches.push(frame_benchmarking::BenchmarkBatch { - results: $( $location )*::run_benchmark( - benchmark, - &lowest_range_values[..], - &highest_range_values[..], - &steps[..], - repeat, - )?, - pallet: pallet.to_vec(), - benchmark: benchmark.to_vec(), - }); - } - } else { - batches.push(frame_benchmarking::BenchmarkBatch { - results: $( $location )*::run_benchmark( - &benchmark[..], - &lowest_range_values[..], - &highest_range_values[..], - &steps[..], - repeat, - )?, - pallet: pallet.to_vec(), - benchmark: benchmark.clone(), - }); - } - } - ) - } - - pallet!(b"balances", Balances); - pallet!(b"im-online", ImOnline); - pallet!(b"identity", Identity); - pallet!(b"session", SessionBench::); - pallet!(b"staking", Staking); - pallet!(b"timestamp", Timestamp); - pallet!(b"treasury", Treasury); - pallet!(b"vesting", Vesting); - pallet!(b"democracy", Democracy); - pallet!(b"collective", Council); + let mut batches = Vec::::new(); + let params = (&pallet, &benchmark, &lowest_range_values, &highest_range_values, &steps, repeat); + add_benchmark!(params, batches, b"balances", Balances); + add_benchmark!(params, batches, b"im-online", ImOnline); + add_benchmark!(params, batches, b"identity", Identity); + add_benchmark!(params, batches, b"session", SessionBench::); + add_benchmark!(params, batches, b"staking", Staking); + add_benchmark!(params, batches, b"timestamp", Timestamp); + add_benchmark!(params, batches, b"treasury", Treasury); + add_benchmark!(params, batches, b"vesting", Vesting); + add_benchmark!(params, batches, b"democracy", Democracy); + add_benchmark!(params, batches, b"collective", Council); if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) } Ok(batches) } diff --git a/frame/benchmarking/src/lib.rs b/frame/benchmarking/src/lib.rs index ebb8de70dc948..4cd6072ce4555 100644 --- a/frame/benchmarking/src/lib.rs +++ b/frame/benchmarking/src/lib.rs @@ -694,3 +694,65 @@ macro_rules! impl_benchmark { } } } + + +/// This macro adds pallet benchmarks to a `Vec` object. +/// +/// First create an object that holds in the input parameters for the benchmark: +/// +/// ```ignore +/// let params = (&pallet, &benchmark, &lowest_range_values, &highest_range_values, &steps, repeat); +/// ``` +/// +/// Then define a mutable local variable to hold your `BenchmarkBatch` object: +/// +/// ```ignore +/// let mut batches = Vec::::new(); +/// ```` +/// +/// Then add the pallets you want to benchmark to this object, including the string +/// you want to use target a particular pallet: +/// +/// ```ignore +/// add_benchmark!(params, batches, b"balances", Balances); +/// add_benchmark!(params, batches, b"identity", Identity); +/// add_benchmark!(params, batches, b"session", SessionBench::); +/// ... +/// ``` +/// +/// At the end of `dispatch_benchmark`, you should return this batches object. +#[macro_export] +macro_rules! add_benchmark { + ( $params:ident, $batches:ident, $name:literal, $( $location:tt )* ) => ( + let (pallet, benchmark, lowest_range_values, highest_range_values, steps, repeat) = $params; + if &pallet[..] == &$name[..] || &pallet[..] == &b"*"[..] { + if &pallet[..] == &b"*"[..] || &benchmark[..] == &b"*"[..] { + for benchmark in $( $location )*::benchmarks().into_iter() { + $batches.push($crate::BenchmarkBatch { + results: $( $location )*::run_benchmark( + benchmark, + &lowest_range_values[..], + &highest_range_values[..], + &steps[..], + repeat, + )?, + pallet: pallet.to_vec(), + benchmark: benchmark.to_vec(), + }); + } + } else { + $batches.push($crate::BenchmarkBatch { + results: $( $location )*::run_benchmark( + &benchmark[..], + &lowest_range_values[..], + &highest_range_values[..], + &steps[..], + repeat, + )?, + pallet: pallet.to_vec(), + benchmark: benchmark.clone(), + }); + } + } + ) +}