diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index 382d1fd102..c0b1fffa75 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -5293,6 +5293,7 @@ dependencies = [ name = "pallet-template" version = "2.0.0" dependencies = [ + "frame-benchmarking", "frame-support", "frame-system", "parity-scale-codec", diff --git a/substrate/bin/node-template/pallets/template/Cargo.toml b/substrate/bin/node-template/pallets/template/Cargo.toml index e6c0c5ac06..9f0c6ee182 100644 --- a/substrate/bin/node-template/pallets/template/Cargo.toml +++ b/substrate/bin/node-template/pallets/template/Cargo.toml @@ -25,6 +25,12 @@ default-features = false version = "3.0.0" path = "../../../../frame/system" +[dependencies.frame-benchmarking] +default-features = false +version = "3.1.0" +path = "../../../../frame/benchmarking" +optional = true + [dev-dependencies] serde = { version = "1.0.101" } @@ -43,12 +49,13 @@ default-features = false version = "3.0.0" path = "../../../../primitives/runtime" - [features] default = ['std'] std = [ 'codec/std', 'frame-support/std', - 'frame-system/std' + 'frame-system/std', + 'frame-benchmarking/std', ] +runtime-benchmarks = ["frame-benchmarking"] try-runtime = ["frame-support/try-runtime"] diff --git a/substrate/bin/node-template/pallets/template/src/benchmarking.rs b/substrate/bin/node-template/pallets/template/src/benchmarking.rs new file mode 100644 index 0000000000..5296ed7261 --- /dev/null +++ b/substrate/bin/node-template/pallets/template/src/benchmarking.rs @@ -0,0 +1,24 @@ +//! Benchmarking setup for pallet-template + +use super::*; + +use frame_system::RawOrigin; +use frame_benchmarking::{benchmarks, whitelisted_caller, impl_benchmark_test_suite}; +#[allow(unused)] +use crate::Module as Template; + +benchmarks! { + do_something { + let s in 0 .. 100; + let caller: T::AccountId = whitelisted_caller(); + }: _(RawOrigin::Signed(caller), s) + verify { + assert_eq!(Something::::get(), Some(s)); + } +} + +impl_benchmark_test_suite!( + Template, + crate::mock::new_test_ext(), + crate::mock::Test, +); diff --git a/substrate/bin/node-template/pallets/template/src/lib.rs b/substrate/bin/node-template/pallets/template/src/lib.rs index 52d9e8111d..5f4e4253f8 100644 --- a/substrate/bin/node-template/pallets/template/src/lib.rs +++ b/substrate/bin/node-template/pallets/template/src/lib.rs @@ -12,6 +12,9 @@ mod mock; #[cfg(test)] mod tests; +#[cfg(feature = "runtime-benchmarks")] +mod benchmarking; + #[frame_support::pallet] pub mod pallet { use frame_support::{dispatch::DispatchResultWithPostInfo, pallet_prelude::*}; @@ -46,7 +49,7 @@ pub mod pallet { /// parameters. [something, who] SomethingStored(u32, T::AccountId), } - + // Errors inform users that something went wrong. #[pallet::error] pub enum Error { diff --git a/substrate/bin/node-template/runtime/Cargo.toml b/substrate/bin/node-template/runtime/Cargo.toml index de69419b92..d4e202d688 100644 --- a/substrate/bin/node-template/runtime/Cargo.toml +++ b/substrate/bin/node-template/runtime/Cargo.toml @@ -89,4 +89,5 @@ runtime-benchmarks = [ "frame-system/runtime-benchmarks", "pallet-balances/runtime-benchmarks", "pallet-timestamp/runtime-benchmarks", + "template/runtime-benchmarks", ] diff --git a/substrate/bin/node-template/runtime/src/lib.rs b/substrate/bin/node-template/runtime/src/lib.rs index 8d68dbdc96..274e46d24d 100644 --- a/substrate/bin/node-template/runtime/src/lib.rs +++ b/substrate/bin/node-template/runtime/src/lib.rs @@ -475,6 +475,7 @@ impl_runtime_apis! { add_benchmark!(params, batches, frame_system, SystemBench::); add_benchmark!(params, batches, pallet_balances, Balances); add_benchmark!(params, batches, pallet_timestamp, Timestamp); + add_benchmark!(params, batches, template, TemplateModule); if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) } Ok(batches) diff --git a/substrate/frame/benchmarking/src/lib.rs b/substrate/frame/benchmarking/src/lib.rs index 266e2c7882..c2f60a5e13 100644 --- a/substrate/frame/benchmarking/src/lib.rs +++ b/substrate/frame/benchmarking/src/lib.rs @@ -34,6 +34,8 @@ pub use sp_runtime::traits::Zero; #[doc(hidden)] pub use frame_support; #[doc(hidden)] +pub use sp_std::{self, vec, prelude::Vec, boxed::Box}; +#[doc(hidden)] pub use paste; #[doc(hidden)] pub use sp_storage::TrackedStorageKey; @@ -566,8 +568,8 @@ macro_rules! benchmark_backend { $crate::BenchmarkingSetup for $name where $( $where_clause )* { - fn components(&self) -> Vec<($crate::BenchmarkParameter, u32, u32)> { - vec! [ + fn components(&self) -> $crate::Vec<($crate::BenchmarkParameter, u32, u32)> { + $crate::vec! [ $( ($crate::BenchmarkParameter::$param, $param_from, $param_to) ),* @@ -578,7 +580,7 @@ macro_rules! benchmark_backend { &self, components: &[($crate::BenchmarkParameter, u32)], verify: bool - ) -> Result Result<(), &'static str>>, &'static str> { + ) -> Result<$crate::Box Result<(), &'static str>>, &'static str> { $( // Prepare instance let $param = components.iter() @@ -592,7 +594,7 @@ macro_rules! benchmark_backend { $( $param_instancer ; )* $( $post )* - Ok(Box::new(move || -> Result<(), &'static str> { + Ok($crate::Box::new(move || -> Result<(), &'static str> { $eval; if verify { $postcode; @@ -637,7 +639,7 @@ macro_rules! selected_benchmark { $crate::BenchmarkingSetup for SelectedBenchmark where $( $where_clause )* { - fn components(&self) -> Vec<($crate::BenchmarkParameter, u32, u32)> { + fn components(&self) -> $crate::Vec<($crate::BenchmarkParameter, u32, u32)> { match self { $( Self::$bench => < @@ -651,7 +653,7 @@ macro_rules! selected_benchmark { &self, components: &[($crate::BenchmarkParameter, u32)], verify: bool - ) -> Result Result<(), &'static str>>, &'static str> { + ) -> Result<$crate::Box Result<(), &'static str>>, &'static str> { match self { $( Self::$bench => < @@ -677,8 +679,8 @@ macro_rules! impl_benchmark { $crate::Benchmarking<$crate::BenchmarkResults> for Module where T: frame_system::Config, $( $where_clause )* { - fn benchmarks(extra: bool) -> Vec<&'static [u8]> { - let mut all = vec![ $( stringify!($name).as_ref() ),* ]; + fn benchmarks(extra: bool) -> $crate::Vec<&'static [u8]> { + let mut all = $crate::vec![ $( stringify!($name).as_ref() ),* ]; if !extra { let extra = [ $( stringify!($name_extra).as_ref() ),* ]; all.retain(|x| !extra.contains(x)); @@ -694,15 +696,15 @@ macro_rules! impl_benchmark { repeat: u32, whitelist: &[$crate::TrackedStorageKey], verify: bool, - ) -> Result, &'static str> { + ) -> Result<$crate::Vec<$crate::BenchmarkResults>, &'static str> { // Map the input to the selected benchmark. - let extrinsic = sp_std::str::from_utf8(extrinsic) + let extrinsic = $crate::sp_std::str::from_utf8(extrinsic) .map_err(|_| "`extrinsic` is not a valid utf8 string!")?; let selected_benchmark = match extrinsic { $( stringify!($name) => SelectedBenchmark::$name, )* _ => return Err("Could not find extrinsic."), }; - let mut results: Vec<$crate::BenchmarkResults> = Vec::new(); + let mut results: $crate::Vec<$crate::BenchmarkResults> = $crate::Vec::new(); if repeat == 0 { return Ok(results); } @@ -710,7 +712,7 @@ macro_rules! impl_benchmark { // Add whitelist to DB including whitelisted caller let mut whitelist = whitelist.to_vec(); let whitelisted_caller_key = - as frame_support::storage::StorageMap<_,_>>::hashed_key_for( + as $crate::frame_support::storage::StorageMap<_,_>>::hashed_key_for( $crate::whitelisted_caller::() ); whitelist.push(whitelisted_caller_key.into()); @@ -730,7 +732,7 @@ macro_rules! impl_benchmark { let repeat_benchmark = | repeat: u32, c: &[($crate::BenchmarkParameter, u32)], - results: &mut Vec<$crate::BenchmarkResults>, + results: &mut $crate::Vec<$crate::BenchmarkResults>, verify: bool, | -> Result<(), &'static str> { // Run the benchmark `repeat` times. @@ -807,7 +809,7 @@ macro_rules! impl_benchmark { if components.is_empty() { if verify { // If `--verify` is used, run the benchmark once to verify it would complete. - repeat_benchmark(1, Default::default(), &mut Vec::new(), true)?; + repeat_benchmark(1, Default::default(), &mut $crate::Vec::new(), true)?; } repeat_benchmark(repeat, Default::default(), &mut results, false)?; } else { @@ -834,7 +836,7 @@ macro_rules! impl_benchmark { let component_value = lowest + step_size * s; // Select the max value for all the other components. - let c: Vec<($crate::BenchmarkParameter, u32)> = components.iter() + let c: $crate::Vec<($crate::BenchmarkParameter, u32)> = components.iter() .enumerate() .map(|(idx, (n, _, h))| if n == name { @@ -847,7 +849,7 @@ macro_rules! impl_benchmark { if verify { // If `--verify` is used, run the benchmark once to verify it would complete. - repeat_benchmark(1, &c, &mut Vec::new(), true)?; + repeat_benchmark(1, &c, &mut $crate::Vec::new(), true)?; } repeat_benchmark(repeat, &c, &mut results, false)?; } @@ -872,7 +874,7 @@ macro_rules! impl_benchmark { where T: Config + frame_system::Config, $( $where_clause )* { - let name = sp_std::str::from_utf8(name) + let name = $crate::sp_std::str::from_utf8(name) .map_err(|_| "`name` is not a valid utf8 string!")?; match name { $( stringify!($name) => { @@ -905,7 +907,7 @@ macro_rules! impl_benchmark_test { >::components(&selected_benchmark); let execute_benchmark = | - c: Vec<($crate::BenchmarkParameter, u32)> + c: $crate::Vec<($crate::BenchmarkParameter, u32)> | -> Result<(), &'static str> { // Set up the benchmark, return execution + verification function. let closure_to_verify = < @@ -931,9 +933,9 @@ macro_rules! impl_benchmark_test { } else { for (_, (name, low, high)) in components.iter().enumerate() { // Test only the low and high value, assuming values in the middle won't break - for component_value in vec![low, high] { + for component_value in $crate::vec![low, high] { // Select the max value for all the other components. - let c: Vec<($crate::BenchmarkParameter, u32)> = components.iter() + let c: $crate::Vec<($crate::BenchmarkParameter, u32)> = components.iter() .enumerate() .map(|(_, (n, _, h))| if n == name { diff --git a/substrate/frame/merkle-mountain-range/src/benchmarking.rs b/substrate/frame/merkle-mountain-range/src/benchmarking.rs index 750a140382..f64e2e39aa 100644 --- a/substrate/frame/merkle-mountain-range/src/benchmarking.rs +++ b/substrate/frame/merkle-mountain-range/src/benchmarking.rs @@ -22,7 +22,6 @@ use crate::*; use frame_support::traits::OnInitialize; use frame_benchmarking::{benchmarks, impl_benchmark_test_suite}; -use sp_std::prelude::*; benchmarks! { on_initialize { diff --git a/substrate/frame/timestamp/src/benchmarking.rs b/substrate/frame/timestamp/src/benchmarking.rs index 57b8ce2d1b..b3e8eca889 100644 --- a/substrate/frame/timestamp/src/benchmarking.rs +++ b/substrate/frame/timestamp/src/benchmarking.rs @@ -20,7 +20,6 @@ #![cfg(feature = "runtime-benchmarks")] use super::*; -use sp_std::prelude::*; use frame_system::RawOrigin; use frame_support::{ensure, traits::OnFinalize}; use frame_benchmarking::{benchmarks, TrackedStorageKey, impl_benchmark_test_suite};