From 222eb3e0d17aad2a5f5e827003c26aa775e627b9 Mon Sep 17 00:00:00 2001 From: Abhishek Shah Date: Wed, 9 Aug 2023 17:38:30 +0530 Subject: [PATCH] Add pallet-motion benchmarks --- pallets/motion/Cargo.toml | 56 ++++++++++--------- pallets/motion/src/benchmarking.rs | 50 +++++++++++++++++ pallets/motion/src/lib.rs | 13 ++++- pallets/motion/src/mock.rs | 1 + pallets/motion/src/weights.rs | 90 ++++++++++++++++++++++++++++++ runtime/src/lib.rs | 2 + 6 files changed, 183 insertions(+), 29 deletions(-) create mode 100644 pallets/motion/src/benchmarking.rs create mode 100644 pallets/motion/src/weights.rs diff --git a/pallets/motion/Cargo.toml b/pallets/motion/Cargo.toml index fb49354..e4477f9 100644 --- a/pallets/motion/Cargo.toml +++ b/pallets/motion/Cargo.toml @@ -1,44 +1,48 @@ [package] -name = "pallet-motion" -version = "4.0.0-dev" -description = "A pallet for dispatching as sudo from collective origins" authors = ["Substrate DevHub "] -homepage = "https://substrate.io" +description = "A pallet for dispatching as sudo from collective origins" edition = "2021" +homepage = "https://substrate.io" license = "Unlicense" +name = "pallet-motion" publish = false +version = "4.0.0-dev" [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [ - "derive", -] } -log = { version = "0.4.17", default-features = false } -scale-info = { version = "2.5.0", default-features = false, features = ["derive"] } -frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42" } -frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" } -frame-system = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" } -sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" } -sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" } +codec = {package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [ + "derive", +]} +frame-benchmarking = {version = "4.0.0-dev", default-features = false, optional = true, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42"} +frame-support = {git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42"} +frame-system = {git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42"} +log = {version = "0.4.17", default-features = false} +pallet-collective = {git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42"} +scale-info = {version = "2.5.0", default-features = false, features = ["derive"]} +sp-runtime = {git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42"} +sp-std = {git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42"} [dev-dependencies] -sp-core = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" } -sp-io = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" } -pallet-balances = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" } -pallet-collective = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" } -pallet-membership = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" } +pallet-balances = {git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42"} +pallet-membership = {git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42"} +sp-core = {git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42"} +sp-io = {git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42"} [features] default = ["std"] +runtime-benchmarks = [ + "frame-benchmarking/runtime-benchmarks", + "pallet-collective/runtime-benchmarks", +] std = [ - "codec/std", - "frame-benchmarking?/std", - "frame-support/std", - "frame-system/std", - "scale-info/std", - "sp-std/std", + "codec/std", + "frame-benchmarking?/std", + "frame-support/std", + "frame-system/std", + "scale-info/std", + "sp-std/std", + "pallet-collective/std", ] -runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"] try-runtime = ["frame-support/try-runtime"] diff --git a/pallets/motion/src/benchmarking.rs b/pallets/motion/src/benchmarking.rs new file mode 100644 index 0000000..b79a29e --- /dev/null +++ b/pallets/motion/src/benchmarking.rs @@ -0,0 +1,50 @@ +#![cfg(feature = "runtime-benchmarks")] + +use super::*; +#[allow(unused)] +use crate::Pallet as Motion; +use frame_benchmarking::v2::*; +use frame_support::traits::EnsureOrigin; + +fn assert_last_event(generic_event: ::RuntimeEvent) { + frame_system::Pallet::::assert_last_event(generic_event.into()); +} + +#[benchmarks( +where + ::RuntimeCall: From>, +)] +mod benchmarks { + use super::*; + + #[benchmark] + fn simple_majority() { + let call: ::RuntimeCall = frame_system::Call::remark { remark: vec![] }.into(); + let origin = ::SimpleMajorityOrigin::try_successful_origin().unwrap(); + #[extrinsic_call] + _(origin as T::RuntimeOrigin, Box::new(call)); + + assert_last_event::(Event::DispatchSimpleMajority { motion_result: Ok(()) }.into()) + } + + #[benchmark] + fn super_majority() { + let call: ::RuntimeCall = frame_system::Call::remark { remark: vec![] }.into(); + let origin = ::SuperMajorityOrigin::try_successful_origin().unwrap(); + #[extrinsic_call] + _(origin as T::RuntimeOrigin, Box::new(call)); + + assert_last_event::(Event::DispatchSuperMajority { motion_result: Ok(()) }.into()) + } + + #[benchmark] + fn unanimous() { + let call: ::RuntimeCall = frame_system::Call::remark { remark: vec![] }.into(); + let origin = ::UnanimousOrigin::try_successful_origin().unwrap(); + #[extrinsic_call] + _(origin as T::RuntimeOrigin, Box::new(call)); + + assert_last_event::(Event::DispatchUnanimous { motion_result: Ok(()) }.into()) + } + impl_benchmark_test_suite!(Motion, crate::mock::new_test_ext(), crate::mock::Test); +} diff --git a/pallets/motion/src/lib.rs b/pallets/motion/src/lib.rs index ff3df54..a1be6c0 100644 --- a/pallets/motion/src/lib.rs +++ b/pallets/motion/src/lib.rs @@ -5,6 +5,12 @@ mod mock; #[cfg(test)] mod tests; +#[cfg(feature = "runtime-benchmarks")] +mod benchmarking; + +pub mod weights; +pub use weights::WeightInfo; + use sp_runtime::DispatchResult; use sp_std::prelude::*; @@ -32,6 +38,7 @@ pub mod pallet { type SimpleMajorityOrigin: EnsureOrigin; type SuperMajorityOrigin: EnsureOrigin; type UnanimousOrigin: EnsureOrigin; + type WeightInfo: WeightInfo; } #[pallet::event] @@ -60,7 +67,7 @@ pub mod pallet { /// # #[pallet::weight({ let dispatch_info = call.get_dispatch_info(); - (dispatch_info.weight, dispatch_info.class) + (T::WeightInfo::simple_majority().saturating_add(dispatch_info.weight), dispatch_info.class) })] #[pallet::call_index(0)] pub fn simple_majority( @@ -85,7 +92,7 @@ pub mod pallet { /// # #[pallet::weight({ let dispatch_info = call.get_dispatch_info(); - (dispatch_info.weight, dispatch_info.class) + (T::WeightInfo::super_majority().saturating_add(dispatch_info.weight), dispatch_info.class) })] #[pallet::call_index(1)] pub fn super_majority( @@ -110,7 +117,7 @@ pub mod pallet { /// # #[pallet::weight({ let dispatch_info = call.get_dispatch_info(); - (dispatch_info.weight, dispatch_info.class) + (T::WeightInfo::unanimous().saturating_add(dispatch_info.weight), dispatch_info.class) })] #[pallet::call_index(2)] pub fn unanimous( diff --git a/pallets/motion/src/mock.rs b/pallets/motion/src/mock.rs index 9bc5eba..43dc66a 100644 --- a/pallets/motion/src/mock.rs +++ b/pallets/motion/src/mock.rs @@ -107,6 +107,7 @@ impl pallet_motion::Config for Test { type SuperMajorityOrigin = pallet_collective::EnsureProportionAtLeast; type UnanimousOrigin = pallet_collective::EnsureProportionAtLeast; + type WeightInfo = (); } // Build genesis storage according to the mock runtime. diff --git a/pallets/motion/src/weights.rs b/pallets/motion/src/weights.rs new file mode 100644 index 0000000..3854d37 --- /dev/null +++ b/pallets/motion/src/weights.rs @@ -0,0 +1,90 @@ + +//! Autogenerated weights for pallet_motion +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2023-07-24, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! WORST CASE MAP SIZE: `1000000` +//! HOSTNAME: `PAR03043`, CPU: `` +//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 + +// Executed Command: +// target/release/parachain-template-node +// benchmark +// pallet +// --chain=dev +// --execution=wasm +// --wasm-execution=compiled +// --pallet=pallet_motion +// --extrinsic=* +// --steps=50 +// --repeat=20 +// --json +// --template +// scripts/frame-weight-template.hbs +// --output=./pallets/motion/src/weights.rs + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] +#![allow(missing_docs)] + +use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; +use core::marker::PhantomData; + +/// Weight functions needed for pallet_motion. +pub trait WeightInfo { + fn simple_majority() -> Weight; + fn super_majority() -> Weight; + fn unanimous() -> Weight; +} + +/// Weights for pallet_motion using the Substrate node and recommended hardware. +pub struct SubstrateWeight(PhantomData); +impl WeightInfo for SubstrateWeight { + fn simple_majority() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 7_000_000 picoseconds. + Weight::from_parts(8_000_000, 0) + } + fn super_majority() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 7_000_000 picoseconds. + Weight::from_parts(8_000_000, 0) + } + fn unanimous() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 7_000_000 picoseconds. + Weight::from_parts(8_000_000, 0) + } +} + +// For backwards compatibility and tests +impl WeightInfo for () { + fn simple_majority() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 7_000_000 picoseconds. + Weight::from_parts(8_000_000, 0) + } + fn super_majority() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 7_000_000 picoseconds. + Weight::from_parts(8_000_000, 0) + } + fn unanimous() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 7_000_000 picoseconds. + Weight::from_parts(8_000_000, 0) + } +} \ No newline at end of file diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 7aacc1c..c83557c 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -578,6 +578,7 @@ impl pallet_motion::Config for Runtime { pallet_collective::EnsureProportionAtLeast; type UnanimousOrigin = pallet_collective::EnsureProportionAtLeast; + type WeightInfo = pallet_motion::weights::SubstrateWeight; } // Frontier @@ -894,6 +895,7 @@ mod benches { [pallet_collator_selection, CollatorSelection] [cumulus_pallet_xcmp_queue, XcmpQueue] [pallet_evm, EVM] + [pallet_motion, Motion] ); }