Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 9e1fa3d

Browse files
authored
Use proper weights in the pallet-template (#13817)
* benchmarking to generate weights file * add the calculated weights in the extrinsics * use benchmarking v2 syntax to generate the weights * minor syntax change when benchmarking * added WeightInfo in the mock to pass tests * minor cargo fmt format changes
1 parent a1d3d50 commit 9e1fa3d

File tree

7 files changed

+125
-10
lines changed

7 files changed

+125
-10
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/node-template/pallets/template/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ scale-info = { version = "2.1.1", default-features = false, features = ["derive"
2020
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../../../../frame/benchmarking" }
2121
frame-support = { version = "4.0.0-dev", default-features = false, path = "../../../../frame/support" }
2222
frame-system = { version = "4.0.0-dev", default-features = false, path = "../../../../frame/system" }
23+
sp-std = { version = "5.0.0", default-features = false, path = "../../../../primitives/std" }
2324

2425
[dev-dependencies]
2526
sp-core = { version = "7.0.0", path = "../../../../primitives/core" }

bin/node-template/pallets/template/src/benchmarking.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
//! Benchmarking setup for pallet-template
2-
2+
#![cfg(feature = "runtime-benchmarks")]
33
use super::*;
44

55
#[allow(unused)]
66
use crate::Pallet as Template;
7-
use frame_benchmarking::v1::{benchmarks, whitelisted_caller};
7+
use frame_benchmarking::v2::*;
88
use frame_system::RawOrigin;
99

10-
benchmarks! {
11-
do_something {
12-
let s in 0 .. 100;
10+
#[benchmarks]
11+
mod benchmarks {
12+
use super::*;
13+
14+
#[benchmark]
15+
fn do_something() {
16+
let value = 100u32.into();
1317
let caller: T::AccountId = whitelisted_caller();
14-
}: _(RawOrigin::Signed(caller), s)
15-
verify {
16-
assert_eq!(Something::<T>::get(), Some(s));
18+
#[extrinsic_call]
19+
do_something(RawOrigin::Signed(caller), value);
20+
21+
assert_eq!(Something::<T>::get(), Some(value));
22+
}
23+
24+
#[benchmark]
25+
fn cause_error() {
26+
Something::<T>::put(100u32);
27+
let caller: T::AccountId = whitelisted_caller();
28+
#[extrinsic_call]
29+
cause_error(RawOrigin::Signed(caller));
30+
31+
assert_eq!(Something::<T>::get(), Some(101u32));
1732
}
1833

1934
impl_benchmark_test_suite!(Template, crate::mock::new_test_ext(), crate::mock::Test);

bin/node-template/pallets/template/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ mod tests;
1313

1414
#[cfg(feature = "runtime-benchmarks")]
1515
mod benchmarking;
16+
pub mod weights;
17+
pub use weights::*;
1618

1719
#[frame_support::pallet]
1820
pub mod pallet {
21+
use super::*;
1922
use frame_support::pallet_prelude::*;
2023
use frame_system::pallet_prelude::*;
2124

@@ -27,6 +30,8 @@ pub mod pallet {
2730
pub trait Config: frame_system::Config {
2831
/// Because this pallet emits events, it depends on the runtime's definition of an event.
2932
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
33+
/// Type representing the weight of this pallet
34+
type WeightInfo: WeightInfo;
3035
}
3136

3237
// The pallet's runtime storage items.
@@ -64,7 +69,7 @@ pub mod pallet {
6469
/// An example dispatchable that takes a singles value as a parameter, writes the value to
6570
/// storage and emits an event. This function must be dispatched by a signed extrinsic.
6671
#[pallet::call_index(0)]
67-
#[pallet::weight(10_000 + T::DbWeight::get().writes(1).ref_time())]
72+
#[pallet::weight(T::WeightInfo::do_something())]
6873
pub fn do_something(origin: OriginFor<T>, something: u32) -> DispatchResult {
6974
// Check that the extrinsic was signed and get the signer.
7075
// This function will return an error if the extrinsic is not signed.
@@ -82,7 +87,7 @@ pub mod pallet {
8287

8388
/// An example dispatchable that may throw a custom error.
8489
#[pallet::call_index(1)]
85-
#[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1).ref_time())]
90+
#[pallet::weight(T::WeightInfo::cause_error())]
8691
pub fn cause_error(origin: OriginFor<T>) -> DispatchResult {
8792
let _who = ensure_signed(origin)?;
8893

bin/node-template/pallets/template/src/mock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ impl frame_system::Config for Test {
5050

5151
impl pallet_template::Config for Test {
5252
type RuntimeEvent = RuntimeEvent;
53+
type WeightInfo = ();
5354
}
5455

5556
// Build genesis storage according to the mock runtime.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
2+
//! Autogenerated weights for pallet_template
3+
//!
4+
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
5+
//! DATE: 2023-04-05, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
6+
//! WORST CASE MAP SIZE: `1000000`
7+
//! HOSTNAME: `Alexs-MacBook-Pro-2.local`, CPU: `<UNKNOWN>`
8+
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024
9+
10+
// Executed Command:
11+
// ../../target/release/node-template
12+
// benchmark
13+
// pallet
14+
// --chain
15+
// dev
16+
// --pallet
17+
// pallet_template
18+
// --extrinsic
19+
// *
20+
// --steps=50
21+
// --repeat=20
22+
// --execution=wasm
23+
// --wasm-execution=compiled
24+
// --output
25+
// pallets/template/src/weights.rs
26+
// --template
27+
// ../../.maintain/frame-weight-template.hbs
28+
29+
#![cfg_attr(rustfmt, rustfmt_skip)]
30+
#![allow(unused_parens)]
31+
#![allow(unused_imports)]
32+
33+
use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
34+
use sp_std::marker::PhantomData;
35+
36+
/// Weight functions needed for pallet_template.
37+
pub trait WeightInfo {
38+
fn do_something() -> Weight;
39+
fn cause_error() -> Weight;
40+
}
41+
42+
/// Weights for pallet_template using the Substrate node and recommended hardware.
43+
pub struct SubstrateWeight<T>(PhantomData<T>);
44+
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
45+
/// Storage: TemplateModule Something (r:0 w:1)
46+
/// Proof: TemplateModule Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
47+
fn do_something() -> Weight {
48+
// Proof Size summary in bytes:
49+
// Measured: `0`
50+
// Estimated: `0`
51+
// Minimum execution time: 8_000_000 picoseconds.
52+
Weight::from_parts(8_000_000, 0)
53+
.saturating_add(T::DbWeight::get().writes(1_u64))
54+
}
55+
/// Storage: TemplateModule Something (r:1 w:1)
56+
/// Proof: TemplateModule Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
57+
fn cause_error() -> Weight {
58+
// Proof Size summary in bytes:
59+
// Measured: `32`
60+
// Estimated: `1489`
61+
// Minimum execution time: 5_000_000 picoseconds.
62+
Weight::from_parts(6_000_000, 1489)
63+
.saturating_add(T::DbWeight::get().reads(1_u64))
64+
.saturating_add(T::DbWeight::get().writes(1_u64))
65+
}
66+
}
67+
68+
// For backwards compatibility and tests
69+
impl WeightInfo for () {
70+
/// Storage: TemplateModule Something (r:0 w:1)
71+
/// Proof: TemplateModule Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
72+
fn do_something() -> Weight {
73+
// Proof Size summary in bytes:
74+
// Measured: `0`
75+
// Estimated: `0`
76+
// Minimum execution time: 8_000_000 picoseconds.
77+
Weight::from_parts(8_000_000, 0)
78+
.saturating_add(RocksDbWeight::get().writes(1_u64))
79+
}
80+
/// Storage: TemplateModule Something (r:1 w:1)
81+
/// Proof: TemplateModule Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
82+
fn cause_error() -> Weight {
83+
// Proof Size summary in bytes:
84+
// Measured: `32`
85+
// Estimated: `1489`
86+
// Minimum execution time: 5_000_000 picoseconds.
87+
Weight::from_parts(6_000_000, 1489)
88+
.saturating_add(RocksDbWeight::get().reads(1_u64))
89+
.saturating_add(RocksDbWeight::get().writes(1_u64))
90+
}
91+
}

bin/node-template/runtime/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ impl pallet_sudo::Config for Runtime {
270270
/// Configure the pallet-template in pallets/template.
271271
impl pallet_template::Config for Runtime {
272272
type RuntimeEvent = RuntimeEvent;
273+
type WeightInfo = pallet_template::weights::SubstrateWeight<Runtime>;
273274
}
274275

275276
// Create the runtime by composing the FRAME pallets that were previously configured.

0 commit comments

Comments
 (0)