Skip to content

Commit

Permalink
feat(benchmark): benchmarking pallets [2/4] (#698)
Browse files Browse the repository at this point in the history
  • Loading branch information
renauter authored Jun 2, 2023
1 parent 67659b5 commit e96dd81
Show file tree
Hide file tree
Showing 33 changed files with 1,259 additions and 355 deletions.
7 changes: 7 additions & 0 deletions substrate-node/pallets/pallet-burning/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ sp-storage = { git = "https://github.com/paritytech/substrate", branch = "polkad
pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false }

# Benchmarking
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false, optional = true }

[dev-dependencies]
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false }

Expand All @@ -36,6 +39,7 @@ std = [
'pallet-balances/std',
'frame-support/std',
'frame-system/std',
'frame-benchmarking/std',
'sp-runtime/std',
'sp-std/std',
'sp-storage/std',
Expand All @@ -44,6 +48,9 @@ std = [
'sp-io/std',
'scale-info/std'
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
]
34 changes: 34 additions & 0 deletions substrate-node/pallets/pallet-burning/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#![cfg(feature = "runtime-benchmarks")]

use super::*;
use crate::Pallet as BurningModule;
use frame_benchmarking::{benchmarks, whitelisted_caller};
use frame_system::{EventRecord, Pallet as System, RawOrigin};
use sp_runtime::{traits::StaticLookup, SaturatedConversion};

benchmarks! {
// burn_tft()
burn_tft {
let target: T::AccountId = whitelisted_caller();
let target_lookup = T::Lookup::unlookup(target.clone());
let amount = BalanceOf::<T>::saturated_from(1000 as u128);
T::Currency::make_free_balance_be(&target, amount);
let message = b"some_message".to_vec();
}: _(RawOrigin::Signed(target.clone()), amount.clone(), message.clone())
verify {
let block = T::BlockNumber::from(1 as u32);
assert_eq!(T::Currency::free_balance(&target).saturated_into::<u128>(), 0 as u128);
assert_last_event::<T>(Event::BurnTransactionCreated(target, amount, block, message).into());
}

// Calling the `impl_benchmark_test_suite` macro inside the `benchmarks`
// block will generate one #[test] function per benchmark
impl_benchmark_test_suite!(BurningModule, crate::mock::new_test_ext(), crate::mock::TestRuntime)
}

fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
let events = System::<T>::events();
let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
let EventRecord { event, .. } = &events[events.len() - 1];
assert_eq!(event, &system_event);
}
9 changes: 8 additions & 1 deletion substrate-node/pallets/pallet-burning/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ mod tests;
#[cfg(test)]
mod mock;

#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;

pub mod weights;

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, Default, Debug, TypeInfo)]
pub struct Burn<AccountId, BalanceOf, BlockNumber> {
pub target: AccountId,
Expand All @@ -29,6 +34,7 @@ pub use pallet::*;

#[frame_support::pallet]
pub mod pallet {
use super::weights::WeightInfo;
use frame_support::{
pallet_prelude::*,
traits::{Currency, OnUnbalanced, ReservableCurrency},
Expand Down Expand Up @@ -57,6 +63,7 @@ pub mod pallet {
type Currency: Currency<Self::AccountId> + ReservableCurrency<Self::AccountId>;
/// Handler for the unbalanced decrement when slashing (burning collateral)
type Burn: OnUnbalanced<NegativeImbalanceOf<Self>>;
type WeightInfo: crate::weights::WeightInfo;
}

#[pallet::event]
Expand All @@ -78,7 +85,7 @@ pub mod pallet {
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(100_000_000)]
#[pallet::weight(<T as Config>::WeightInfo::burn_tft())]
pub fn burn_tft(
origin: OriginFor<T>,
amount: BalanceOf<T>,
Expand Down
2 changes: 2 additions & 0 deletions substrate-node/pallets/pallet-burning/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ impl pallet_balances::Config for TestRuntime {
type WeightInfo = pallet_balances::weights::SubstrateWeight<TestRuntime>;
}

use weights;
impl Config for TestRuntime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type Burn = ();
type WeightInfo = weights::SubstrateWeight<TestRuntime>;
}

type AccountPublic = <MultiSignature as Verify>::Signer;
Expand Down
4 changes: 2 additions & 2 deletions substrate-node/pallets/pallet-burning/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn test_burn() {
assert_ok!(BurningModule::burn_tft(
RuntimeOrigin::signed(alice()),
900000000000,
"some_message".as_bytes().to_vec()
b"some_message".to_vec()
));

let b = Balances::free_balance(alice());
Expand All @@ -24,7 +24,7 @@ fn test_burn_to_much_fails() {
BurningModule::burn_tft(
RuntimeOrigin::signed(alice()),
1200000000000,
"some_message".as_bytes().to_vec()
b"some_message".to_vec()
),
Error::<TestRuntime>::NotEnoughBalanceToBurn
);
Expand Down
58 changes: 58 additions & 0 deletions substrate-node/pallets/pallet-burning/src/weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

//! Autogenerated weights for pallet_burning
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-05-16, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! HOSTNAME: `R1-HP-ProBook-630-G8`, CPU: `11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024

// Executed Command:
// ./target/release/tfchain
// benchmark
// pallet
// --chain=dev
// --pallet=pallet_burning
// --extrinsic=*
// --steps=50
// --repeat=20
// --execution=wasm
// --heap-pages=4096
// --output
// pallets/pallet-burning/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_burning.
pub trait WeightInfo {
fn burn_tft() -> Weight;
}

/// Weights for pallet_burning using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Storage: BurningModule Burns (r:1 w:1)
fn burn_tft() -> Weight {
// Minimum execution time: 58_615 nanoseconds.
Weight::from_ref_time(63_829_000)
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(1))
}
}

// For backwards compatibility and tests
impl WeightInfo for () {
// Storage: BurningModule Burns (r:1 w:1)
fn burn_tft() -> Weight {
// Minimum execution time: 58_615 nanoseconds.
Weight::from_ref_time(63_829_000)
.saturating_add(RocksDbWeight::get().reads(1))
.saturating_add(RocksDbWeight::get().writes(1))
}
}
17 changes: 11 additions & 6 deletions substrate-node/pallets/pallet-dao/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features =
] }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }

frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false, optional = true }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false }
pallet-membership = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false }
Expand All @@ -33,11 +32,14 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkad
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false }

# Benchmarking
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false, optional = true }

tfchain-support = { path = "../../support", default-features = false }
pallet-tfgrid = { path = "../pallet-tfgrid", default-features = false }

[dev-dependencies]
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false }
env_logger = "*"

[features]
Expand All @@ -55,10 +57,13 @@ std = [
"pallet-timestamp/std",
"pallet-tfgrid/std",
"tfchain-support/std",
'scale-info/std',
'serde/std',
'log/std'
"scale-info/std",
"serde/std",
"log/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
]
]
Loading

0 comments on commit e96dd81

Please sign in to comment.