Skip to content

Commit

Permalink
Add pallet-motion benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
weezy20 committed Aug 9, 2023
1 parent 54d85db commit 222eb3e
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 29 deletions.
56 changes: 30 additions & 26 deletions pallets/motion/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/substrate-developer-hub>"]
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"]
50 changes: 50 additions & 0 deletions pallets/motion/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -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<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
frame_system::Pallet::<T>::assert_last_event(generic_event.into());
}

#[benchmarks(
where
<T as Config>::RuntimeCall: From<frame_system::Call<T>>,
)]
mod benchmarks {
use super::*;

#[benchmark]
fn simple_majority() {
let call: <T as Config>::RuntimeCall = frame_system::Call::remark { remark: vec![] }.into();
let origin = <T as Config>::SimpleMajorityOrigin::try_successful_origin().unwrap();
#[extrinsic_call]
_(origin as T::RuntimeOrigin, Box::new(call));

assert_last_event::<T>(Event::DispatchSimpleMajority { motion_result: Ok(()) }.into())
}

#[benchmark]
fn super_majority() {
let call: <T as Config>::RuntimeCall = frame_system::Call::remark { remark: vec![] }.into();
let origin = <T as Config>::SuperMajorityOrigin::try_successful_origin().unwrap();
#[extrinsic_call]
_(origin as T::RuntimeOrigin, Box::new(call));

assert_last_event::<T>(Event::DispatchSuperMajority { motion_result: Ok(()) }.into())
}

#[benchmark]
fn unanimous() {
let call: <T as Config>::RuntimeCall = frame_system::Call::remark { remark: vec![] }.into();
let origin = <T as Config>::UnanimousOrigin::try_successful_origin().unwrap();
#[extrinsic_call]
_(origin as T::RuntimeOrigin, Box::new(call));

assert_last_event::<T>(Event::DispatchUnanimous { motion_result: Ok(()) }.into())
}
impl_benchmark_test_suite!(Motion, crate::mock::new_test_ext(), crate::mock::Test);
}
13 changes: 10 additions & 3 deletions pallets/motion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down Expand Up @@ -32,6 +38,7 @@ pub mod pallet {
type SimpleMajorityOrigin: EnsureOrigin<Self::RuntimeOrigin>;
type SuperMajorityOrigin: EnsureOrigin<Self::RuntimeOrigin>;
type UnanimousOrigin: EnsureOrigin<Self::RuntimeOrigin>;
type WeightInfo: WeightInfo;
}

#[pallet::event]
Expand Down Expand Up @@ -60,7 +67,7 @@ pub mod pallet {
/// # </weight>
#[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(
Expand All @@ -85,7 +92,7 @@ pub mod pallet {
/// # </weight>
#[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(
Expand All @@ -110,7 +117,7 @@ pub mod pallet {
/// # </weight>
#[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(
Expand Down
1 change: 1 addition & 0 deletions pallets/motion/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ impl pallet_motion::Config for Test {
type SuperMajorityOrigin =
pallet_collective::EnsureProportionAtLeast<u64, CouncilCollective, 2, 3>;
type UnanimousOrigin = pallet_collective::EnsureProportionAtLeast<u64, CouncilCollective, 1, 1>;
type WeightInfo = ();
}

// Build genesis storage according to the mock runtime.
Expand Down
90 changes: 90 additions & 0 deletions pallets/motion/src/weights.rs
Original file line number Diff line number Diff line change
@@ -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: `<UNKNOWN>`
//! 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<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
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)
}
}
2 changes: 2 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ impl pallet_motion::Config for Runtime {
pallet_collective::EnsureProportionAtLeast<AccountId, CouncilCollective, 2, 3>;
type UnanimousOrigin =
pallet_collective::EnsureProportionAtLeast<AccountId, CouncilCollective, 1, 1>;
type WeightInfo = pallet_motion::weights::SubstrateWeight<Runtime>;
}

// Frontier
Expand Down Expand Up @@ -894,6 +895,7 @@ mod benches {
[pallet_collator_selection, CollatorSelection]
[cumulus_pallet_xcmp_queue, XcmpQueue]
[pallet_evm, EVM]
[pallet_motion, Motion]
);
}

Expand Down

0 comments on commit 222eb3e

Please sign in to comment.