From 3abd9174b42bdda849d62e868d9ed9c63fbe4869 Mon Sep 17 00:00:00 2001 From: AlexD10S Date: Mon, 3 Apr 2023 19:51:18 +0100 Subject: [PATCH 1/6] benchmarking to generate weights file --- .../pallets/template/src/benchmarking.rs | 7 ++ .../pallets/template/src/weights.rs | 95 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 bin/node-template/pallets/template/src/weights.rs diff --git a/bin/node-template/pallets/template/src/benchmarking.rs b/bin/node-template/pallets/template/src/benchmarking.rs index 1790849970440..451279f25f659 100644 --- a/bin/node-template/pallets/template/src/benchmarking.rs +++ b/bin/node-template/pallets/template/src/benchmarking.rs @@ -16,5 +16,12 @@ benchmarks! { assert_eq!(Something::::get(), Some(s)); } + cause_error { + Something::::put(100u32); + }: _(RawOrigin::Signed(whitelisted_caller())) + verify { + assert_eq!(Something::::get(), Some(101u32)); + } + impl_benchmark_test_suite!(Template, crate::mock::new_test_ext(), crate::mock::Test); } diff --git a/bin/node-template/pallets/template/src/weights.rs b/bin/node-template/pallets/template/src/weights.rs new file mode 100644 index 0000000000000..94cfa131ea43c --- /dev/null +++ b/bin/node-template/pallets/template/src/weights.rs @@ -0,0 +1,95 @@ + +//! Autogenerated weights for pallet_template +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2023-04-03, STEPS: `20`, REPEAT: `10`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! WORST CASE MAP SIZE: `1000000` +//! HOSTNAME: `Alexs-MacBook-Pro-2.local`, CPU: `` +//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 + +// Executed Command: +// ../../target/release/node-template +// benchmark +// pallet +// --chain +// dev +// --pallet +// pallet_template +// --extrinsic +// * +// --steps +// 20 +// --repeat +// 10 +// --execution=wasm +// --wasm-execution=compiled +// --output +// pallets/template/src/weights.rs +// --template +// ../../.maintain/frame-weight-template.hbs + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] + +use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; +use sp_std::marker::PhantomData; + +/// Weight functions needed for pallet_template. +pub trait WeightInfo { + fn do_something(s: u32, ) -> Weight; + fn cause_error() -> Weight; +} + +/// Weights for pallet_template using the Substrate node and recommended hardware. +pub struct SubstrateWeight(PhantomData); +impl WeightInfo for SubstrateWeight { + /// Storage: TemplateModule Something (r:0 w:1) + /// Proof: TemplateModule Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// The range of component `s` is `[0, 100]`. + fn do_something(_s: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 8_000_000 picoseconds. + Weight::from_parts(9_287_978, 0) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + /// Storage: TemplateModule Something (r:1 w:1) + /// Proof: TemplateModule Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + fn cause_error() -> Weight { + // Proof Size summary in bytes: + // Measured: `32` + // Estimated: `1489` + // Minimum execution time: 7_000_000 picoseconds. + Weight::from_parts(7_000_000, 1489) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } +} + +// For backwards compatibility and tests +impl WeightInfo for () { + /// Storage: TemplateModule Something (r:0 w:1) + /// Proof: TemplateModule Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// The range of component `s` is `[0, 100]`. + fn do_something(_s: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 8_000_000 picoseconds. + Weight::from_parts(9_287_978, 0) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } + /// Storage: TemplateModule Something (r:1 w:1) + /// Proof: TemplateModule Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + fn cause_error() -> Weight { + // Proof Size summary in bytes: + // Measured: `32` + // Estimated: `1489` + // Minimum execution time: 7_000_000 picoseconds. + Weight::from_parts(7_000_000, 1489) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } +} From 1802ffe7b43fbce68b7d10c56b81fa02abe14ee3 Mon Sep 17 00:00:00 2001 From: AlexD10S Date: Tue, 4 Apr 2023 09:18:45 +0100 Subject: [PATCH 2/6] add the calculated weights in the extrinsics --- Cargo.lock | 1 + bin/node-template/pallets/template/Cargo.toml | 1 + bin/node-template/pallets/template/src/lib.rs | 9 +++++++-- bin/node-template/runtime/src/lib.rs | 1 + 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1e9a87e818641..7d41160f73a23 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6838,6 +6838,7 @@ dependencies = [ "sp-core", "sp-io", "sp-runtime", + "sp-std", ] [[package]] diff --git a/bin/node-template/pallets/template/Cargo.toml b/bin/node-template/pallets/template/Cargo.toml index ddf7c6e52fb2f..818dc0bbc8c3c 100644 --- a/bin/node-template/pallets/template/Cargo.toml +++ b/bin/node-template/pallets/template/Cargo.toml @@ -20,6 +20,7 @@ scale-info = { version = "2.1.1", default-features = false, features = ["derive" frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../../../../frame/benchmarking" } frame-support = { version = "4.0.0-dev", default-features = false, path = "../../../../frame/support" } frame-system = { version = "4.0.0-dev", default-features = false, path = "../../../../frame/system" } +sp-std = { version = "5.0.0", default-features = false, path = "../../../../primitives/std" } [dev-dependencies] sp-core = { version = "7.0.0", path = "../../../../primitives/core" } diff --git a/bin/node-template/pallets/template/src/lib.rs b/bin/node-template/pallets/template/src/lib.rs index 9f17623db328f..86bc26f6af553 100644 --- a/bin/node-template/pallets/template/src/lib.rs +++ b/bin/node-template/pallets/template/src/lib.rs @@ -13,9 +13,12 @@ mod tests; #[cfg(feature = "runtime-benchmarks")] mod benchmarking; +pub mod weights; +pub use weights::*; #[frame_support::pallet] pub mod pallet { + use super::*; use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; @@ -27,6 +30,8 @@ pub mod pallet { pub trait Config: frame_system::Config { /// Because this pallet emits events, it depends on the runtime's definition of an event. type RuntimeEvent: From> + IsType<::RuntimeEvent>; + /// Type representing the weight of this pallet + type WeightInfo: WeightInfo; } // The pallet's runtime storage items. @@ -64,7 +69,7 @@ pub mod pallet { /// An example dispatchable that takes a singles value as a parameter, writes the value to /// storage and emits an event. This function must be dispatched by a signed extrinsic. #[pallet::call_index(0)] - #[pallet::weight(10_000 + T::DbWeight::get().writes(1).ref_time())] + #[pallet::weight(T::WeightInfo::do_something(*something))] pub fn do_something(origin: OriginFor, something: u32) -> DispatchResult { // Check that the extrinsic was signed and get the signer. // This function will return an error if the extrinsic is not signed. @@ -82,7 +87,7 @@ pub mod pallet { /// An example dispatchable that may throw a custom error. #[pallet::call_index(1)] - #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1).ref_time())] + #[pallet::weight(T::WeightInfo::cause_error())] pub fn cause_error(origin: OriginFor) -> DispatchResult { let _who = ensure_signed(origin)?; diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index 50bcb67cb4790..3dcf1b5564dec 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -270,6 +270,7 @@ impl pallet_sudo::Config for Runtime { /// Configure the pallet-template in pallets/template. impl pallet_template::Config for Runtime { type RuntimeEvent = RuntimeEvent; + type WeightInfo = pallet_template::weights::SubstrateWeight; } // Create the runtime by composing the FRAME pallets that were previously configured. From ee95acf39c4b47be07f317bcc13905ff7e637e19 Mon Sep 17 00:00:00 2001 From: AlexD10S Date: Tue, 4 Apr 2023 17:25:16 +0100 Subject: [PATCH 3/6] use benchmarking v2 syntax to generate the weights --- .../pallets/template/src/benchmarking.rs | 32 ++++++++++++------- bin/node-template/pallets/template/src/lib.rs | 2 +- .../pallets/template/src/weights.rs | 22 ++++++------- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/bin/node-template/pallets/template/src/benchmarking.rs b/bin/node-template/pallets/template/src/benchmarking.rs index 451279f25f659..6dbbe09a21a25 100644 --- a/bin/node-template/pallets/template/src/benchmarking.rs +++ b/bin/node-template/pallets/template/src/benchmarking.rs @@ -1,27 +1,37 @@ //! Benchmarking setup for pallet-template - +#![cfg(feature = "runtime-benchmarks")] use super::*; #[allow(unused)] use crate::Pallet as Template; -use frame_benchmarking::v1::{benchmarks, whitelisted_caller}; +use frame_benchmarking::v2::*; use frame_system::RawOrigin; -benchmarks! { - do_something { - let s in 0 .. 100; +#[benchmarks] +mod benchmarks { + use super::*; + + #[benchmark] + fn do_something() { + let value = 100u32.into(); let caller: T::AccountId = whitelisted_caller(); - }: _(RawOrigin::Signed(caller), s) - verify { - assert_eq!(Something::::get(), Some(s)); + #[extrinsic_call] + do_something(RawOrigin::Signed(caller), value); + + assert_eq!(Something::::get(), Some(value)); } - cause_error { + #[benchmark] + fn cause_error() { Something::::put(100u32); - }: _(RawOrigin::Signed(whitelisted_caller())) - verify { + let caller: T::AccountId = whitelisted_caller(); + #[extrinsic_call] + cause_error(RawOrigin::Signed(caller)); + assert_eq!(Something::::get(), Some(101u32)); } impl_benchmark_test_suite!(Template, crate::mock::new_test_ext(), crate::mock::Test); + } + diff --git a/bin/node-template/pallets/template/src/lib.rs b/bin/node-template/pallets/template/src/lib.rs index 86bc26f6af553..9550d3d546cca 100644 --- a/bin/node-template/pallets/template/src/lib.rs +++ b/bin/node-template/pallets/template/src/lib.rs @@ -69,7 +69,7 @@ pub mod pallet { /// An example dispatchable that takes a singles value as a parameter, writes the value to /// storage and emits an event. This function must be dispatched by a signed extrinsic. #[pallet::call_index(0)] - #[pallet::weight(T::WeightInfo::do_something(*something))] + #[pallet::weight(T::WeightInfo::do_something())] pub fn do_something(origin: OriginFor, something: u32) -> DispatchResult { // Check that the extrinsic was signed and get the signer. // This function will return an error if the extrinsic is not signed. diff --git a/bin/node-template/pallets/template/src/weights.rs b/bin/node-template/pallets/template/src/weights.rs index 94cfa131ea43c..9a0c6f6fcfe34 100644 --- a/bin/node-template/pallets/template/src/weights.rs +++ b/bin/node-template/pallets/template/src/weights.rs @@ -2,7 +2,7 @@ //! Autogenerated weights for pallet_template //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2023-04-03, STEPS: `20`, REPEAT: `10`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2023-04-04, STEPS: `20`, REPEAT: `10`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! WORST CASE MAP SIZE: `1000000` //! HOSTNAME: `Alexs-MacBook-Pro-2.local`, CPU: `` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 @@ -37,7 +37,7 @@ use sp_std::marker::PhantomData; /// Weight functions needed for pallet_template. pub trait WeightInfo { - fn do_something(s: u32, ) -> Weight; + fn do_something() -> Weight; fn cause_error() -> Weight; } @@ -46,13 +46,12 @@ pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { /// Storage: TemplateModule Something (r:0 w:1) /// Proof: TemplateModule Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) - /// The range of component `s` is `[0, 100]`. - fn do_something(_s: u32, ) -> Weight { + fn do_something() -> Weight { // Proof Size summary in bytes: // Measured: `0` // Estimated: `0` // Minimum execution time: 8_000_000 picoseconds. - Weight::from_parts(9_287_978, 0) + Weight::from_parts(9_000_000, 0) .saturating_add(T::DbWeight::get().writes(1_u64)) } /// Storage: TemplateModule Something (r:1 w:1) @@ -61,8 +60,8 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `32` // Estimated: `1489` - // Minimum execution time: 7_000_000 picoseconds. - Weight::from_parts(7_000_000, 1489) + // Minimum execution time: 6_000_000 picoseconds. + Weight::from_parts(6_000_000, 1489) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } @@ -72,13 +71,12 @@ impl WeightInfo for SubstrateWeight { impl WeightInfo for () { /// Storage: TemplateModule Something (r:0 w:1) /// Proof: TemplateModule Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) - /// The range of component `s` is `[0, 100]`. - fn do_something(_s: u32, ) -> Weight { + fn do_something() -> Weight { // Proof Size summary in bytes: // Measured: `0` // Estimated: `0` // Minimum execution time: 8_000_000 picoseconds. - Weight::from_parts(9_287_978, 0) + Weight::from_parts(9_000_000, 0) .saturating_add(RocksDbWeight::get().writes(1_u64)) } /// Storage: TemplateModule Something (r:1 w:1) @@ -87,8 +85,8 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `32` // Estimated: `1489` - // Minimum execution time: 7_000_000 picoseconds. - Weight::from_parts(7_000_000, 1489) + // Minimum execution time: 6_000_000 picoseconds. + Weight::from_parts(6_000_000, 1489) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } From 5c24d3aca0f6b84a7e33a740ebd9ee2ab92a7d42 Mon Sep 17 00:00:00 2001 From: AlexD10S Date: Wed, 5 Apr 2023 10:07:02 +0100 Subject: [PATCH 4/6] minor syntax change when benchmarking --- .../pallets/template/src/weights.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/bin/node-template/pallets/template/src/weights.rs b/bin/node-template/pallets/template/src/weights.rs index 9a0c6f6fcfe34..952aba49bb3d4 100644 --- a/bin/node-template/pallets/template/src/weights.rs +++ b/bin/node-template/pallets/template/src/weights.rs @@ -2,7 +2,7 @@ //! Autogenerated weights for pallet_template //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2023-04-04, STEPS: `20`, REPEAT: `10`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2023-04-05, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! WORST CASE MAP SIZE: `1000000` //! HOSTNAME: `Alexs-MacBook-Pro-2.local`, CPU: `` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 @@ -17,10 +17,8 @@ // pallet_template // --extrinsic // * -// --steps -// 20 -// --repeat -// 10 +// --steps=50 +// --repeat=20 // --execution=wasm // --wasm-execution=compiled // --output @@ -51,7 +49,7 @@ impl WeightInfo for SubstrateWeight { // Measured: `0` // Estimated: `0` // Minimum execution time: 8_000_000 picoseconds. - Weight::from_parts(9_000_000, 0) + Weight::from_parts(8_000_000, 0) .saturating_add(T::DbWeight::get().writes(1_u64)) } /// Storage: TemplateModule Something (r:1 w:1) @@ -60,7 +58,7 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `32` // Estimated: `1489` - // Minimum execution time: 6_000_000 picoseconds. + // Minimum execution time: 5_000_000 picoseconds. Weight::from_parts(6_000_000, 1489) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) @@ -76,7 +74,7 @@ impl WeightInfo for () { // Measured: `0` // Estimated: `0` // Minimum execution time: 8_000_000 picoseconds. - Weight::from_parts(9_000_000, 0) + Weight::from_parts(8_000_000, 0) .saturating_add(RocksDbWeight::get().writes(1_u64)) } /// Storage: TemplateModule Something (r:1 w:1) @@ -85,7 +83,7 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `32` // Estimated: `1489` - // Minimum execution time: 6_000_000 picoseconds. + // Minimum execution time: 5_000_000 picoseconds. Weight::from_parts(6_000_000, 1489) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) From 11757c357d784e4d4de10cdd4cb51d620aac5737 Mon Sep 17 00:00:00 2001 From: AlexD10S Date: Wed, 5 Apr 2023 14:55:44 +0100 Subject: [PATCH 5/6] added WeightInfo in the mock to pass tests --- bin/node-template/pallets/template/src/mock.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/node-template/pallets/template/src/mock.rs b/bin/node-template/pallets/template/src/mock.rs index 91a1bf6ed5fc8..b4d6905378a5d 100644 --- a/bin/node-template/pallets/template/src/mock.rs +++ b/bin/node-template/pallets/template/src/mock.rs @@ -50,6 +50,7 @@ impl frame_system::Config for Test { impl pallet_template::Config for Test { type RuntimeEvent = RuntimeEvent; + type WeightInfo = (); } // Build genesis storage according to the mock runtime. From 2e158975c6deb0c9a882ea123a0e495a53eced86 Mon Sep 17 00:00:00 2001 From: AlexD10S Date: Wed, 5 Apr 2023 15:23:51 +0100 Subject: [PATCH 6/6] minor cargo fmt format changes --- bin/node-template/pallets/template/src/benchmarking.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/bin/node-template/pallets/template/src/benchmarking.rs b/bin/node-template/pallets/template/src/benchmarking.rs index 6dbbe09a21a25..5a262417629c5 100644 --- a/bin/node-template/pallets/template/src/benchmarking.rs +++ b/bin/node-template/pallets/template/src/benchmarking.rs @@ -16,7 +16,7 @@ mod benchmarks { let value = 100u32.into(); let caller: T::AccountId = whitelisted_caller(); #[extrinsic_call] - do_something(RawOrigin::Signed(caller), value); + do_something(RawOrigin::Signed(caller), value); assert_eq!(Something::::get(), Some(value)); } @@ -26,12 +26,10 @@ mod benchmarks { Something::::put(100u32); let caller: T::AccountId = whitelisted_caller(); #[extrinsic_call] - cause_error(RawOrigin::Signed(caller)); + cause_error(RawOrigin::Signed(caller)); assert_eq!(Something::::get(), Some(101u32)); } impl_benchmark_test_suite!(Template, crate::mock::new_test_ext(), crate::mock::Test); - } -