Skip to content

Commit d024eb8

Browse files
shawntabriziark0f
authored andcommitted
Dont use benchmark range on constant functions (paritytech#12456)
* dont use benchmark range on constant function * update weights * fix * new weights * Update frame/examples/basic/src/benchmarking.rs Co-authored-by: parity-processbot <>
1 parent 201f38f commit d024eb8

File tree

3 files changed

+46
-51
lines changed

3 files changed

+46
-51
lines changed

frame/examples/basic/src/benchmarking.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,26 @@ use frame_system::RawOrigin;
3434
// Details on using the benchmarks macro can be seen at:
3535
// https://paritytech.github.io/substrate/master/frame_benchmarking/trait.Benchmarking.html#tymethod.benchmarks
3636
benchmarks! {
37-
// This will measure the execution time of `set_dummy` for b in [0..1000] range.
37+
// This will measure the execution time of `set_dummy`.
3838
set_dummy_benchmark {
39-
// This is the benchmark setup phase
40-
let b in 0 .. 1000;
41-
}: set_dummy(RawOrigin::Root, b.into()) // The execution phase is just running `set_dummy` extrinsic call
39+
// This is the benchmark setup phase.
40+
// `set_dummy` is a constant time function, hence we hard-code some random value here.
41+
let value = 1000u32.into();
42+
}: set_dummy(RawOrigin::Root, value) // The execution phase is just running `set_dummy` extrinsic call
4243
verify {
4344
// This is the optional benchmark verification phase, asserting certain states.
44-
assert_eq!(Pallet::<T>::dummy(), Some(b.into()))
45+
assert_eq!(Pallet::<T>::dummy(), Some(value))
4546
}
4647

47-
// This will measure the execution time of `accumulate_dummy` for b in [0..1000] range.
48+
// This will measure the execution time of `accumulate_dummy`.
4849
// The benchmark execution phase is shorthanded. When the name of the benchmark case is the same
4950
// as the extrinsic call. `_(...)` is used to represent the extrinsic name.
5051
// The benchmark verification phase is omitted.
5152
accumulate_dummy {
52-
let b in 0 .. 1000;
53+
let value = 1000u32.into();
5354
// The caller account is whitelisted for DB reads/write by the benchmarking macro.
5455
let caller: T::AccountId = whitelisted_caller();
55-
}: _(RawOrigin::Signed(caller), b.into())
56+
}: _(RawOrigin::Signed(caller), value)
5657

5758
// This will measure the execution time of sorting a vector.
5859
sort_vector {

frame/examples/basic/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ pub mod pallet {
498498
// The weight for this extrinsic we rely on the auto-generated `WeightInfo` from the
499499
// benchmark toolchain.
500500
#[pallet::weight(
501-
<T as pallet::Config>::WeightInfo::accumulate_dummy((*increase_by).saturated_into())
501+
<T as pallet::Config>::WeightInfo::accumulate_dummy()
502502
)]
503503
pub fn accumulate_dummy(origin: OriginFor<T>, increase_by: T::Balance) -> DispatchResult {
504504
// This is a public call, so we ensure that the origin is some signed account.

frame/examples/basic/src/weights.rs

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This file is part of Substrate.
22

3-
// Copyright (C) 2021-2022 Parity Technologies (UK) Ltd.
3+
// Copyright (C) 2022 Parity Technologies (UK) Ltd.
44
// SPDX-License-Identifier: Apache-2.0
55

66
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,34 +17,26 @@
1717

1818
//! Autogenerated weights for pallet_example_basic
1919
//!
20-
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0
21-
//! DATE: 2021-03-15, STEPS: `[100, ]`, REPEAT: 10, LOW RANGE: `[]`, HIGH RANGE: `[]`
22-
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128
20+
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
21+
//! DATE: 2022-10-09, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
22+
//! HOSTNAME: `Shawns-MacBook-Pro.local`, CPU: `<UNKNOWN>`
23+
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024
2324
2425
// Executed Command:
2526
// ./target/release/substrate
2627
// benchmark
27-
// --chain
28-
// dev
29-
// --execution
30-
// wasm
31-
// --wasm-execution
32-
// compiled
33-
// --pallet
34-
// pallet_example_basic
35-
// --extrinsic
36-
// *
37-
// --steps
38-
// 100
39-
// --repeat
40-
// 10
41-
// --raw
42-
// --output
43-
// ./
28+
// pallet
29+
// --chain=dev
30+
// --execution=wasm
31+
// --wasm-execution=compiled
32+
// --pallet=pallet_example_basic
33+
// --extrinsic=*
34+
// --steps=50
35+
// --repeat=20
36+
// --output=./
4437
// --template
4538
// ./.maintain/frame-weight-template.hbs
4639

47-
4840
#![cfg_attr(rustfmt, rustfmt_skip)]
4941
#![allow(unused_parens)]
5042
#![allow(unused_imports)]
@@ -54,48 +46,50 @@ use sp_std::marker::PhantomData;
5446

5547
/// Weight functions needed for pallet_example_basic.
5648
pub trait WeightInfo {
57-
fn set_dummy_benchmark(b: u32, ) -> Weight;
58-
fn accumulate_dummy(b: u32, ) -> Weight;
49+
fn set_dummy_benchmark() -> Weight;
50+
fn accumulate_dummy() -> Weight;
5951
fn sort_vector(x: u32, ) -> Weight;
6052
}
6153

6254
/// Weights for pallet_example_basic using the Substrate node and recommended hardware.
6355
pub struct SubstrateWeight<T>(PhantomData<T>);
6456
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
65-
fn set_dummy_benchmark(b: u32, ) -> Weight {
66-
Weight::from_ref_time(5_834_000 as u64)
67-
.saturating_add(Weight::from_ref_time(24_000 as u64).saturating_mul(b as u64))
57+
// Storage: BasicExample Dummy (r:0 w:1)
58+
fn set_dummy_benchmark() -> Weight {
59+
Weight::from_ref_time(19_000_000 as u64)
6860
.saturating_add(T::DbWeight::get().writes(1 as u64))
6961
}
70-
fn accumulate_dummy(b: u32, ) -> Weight {
71-
Weight::from_ref_time(51_353_000 as u64)
72-
.saturating_add(Weight::from_ref_time(14_000 as u64).saturating_mul(b as u64))
62+
// Storage: BasicExample Dummy (r:1 w:1)
63+
fn accumulate_dummy() -> Weight {
64+
Weight::from_ref_time(18_000_000 as u64)
7365
.saturating_add(T::DbWeight::get().reads(1 as u64))
7466
.saturating_add(T::DbWeight::get().writes(1 as u64))
7567
}
68+
/// The range of component `x` is `[0, 10000]`.
7669
fn sort_vector(x: u32, ) -> Weight {
77-
Weight::from_ref_time(2_569_000 as u64)
78-
// Standard Error: 0
79-
.saturating_add(Weight::from_ref_time(4_000 as u64).saturating_mul(x as u64))
70+
Weight::from_ref_time(0 as u64)
71+
// Standard Error: 2
72+
.saturating_add(Weight::from_ref_time(520 as u64).saturating_mul(x as u64))
8073
}
8174
}
8275

8376
// For backwards compatibility and tests
8477
impl WeightInfo for () {
85-
fn set_dummy_benchmark(b: u32, ) -> Weight {
86-
Weight::from_ref_time(5_834_000 as u64)
87-
.saturating_add(Weight::from_ref_time(24_000 as u64).saturating_mul(b as u64))
78+
// Storage: BasicExample Dummy (r:0 w:1)
79+
fn set_dummy_benchmark() -> Weight {
80+
Weight::from_ref_time(19_000_000 as u64)
8881
.saturating_add(RocksDbWeight::get().writes(1 as u64))
8982
}
90-
fn accumulate_dummy(b: u32, ) -> Weight {
91-
Weight::from_ref_time(51_353_000 as u64)
92-
.saturating_add(Weight::from_ref_time(14_000 as u64).saturating_mul(b as u64))
83+
// Storage: BasicExample Dummy (r:1 w:1)
84+
fn accumulate_dummy() -> Weight {
85+
Weight::from_ref_time(18_000_000 as u64)
9386
.saturating_add(RocksDbWeight::get().reads(1 as u64))
9487
.saturating_add(RocksDbWeight::get().writes(1 as u64))
9588
}
89+
/// The range of component `x` is `[0, 10000]`.
9690
fn sort_vector(x: u32, ) -> Weight {
97-
Weight::from_ref_time(2_569_000 as u64)
98-
// Standard Error: 0
99-
.saturating_add(Weight::from_ref_time(4_000 as u64).saturating_mul(x as u64))
91+
Weight::from_ref_time(0 as u64)
92+
// Standard Error: 2
93+
.saturating_add(Weight::from_ref_time(520 as u64).saturating_mul(x as u64))
10094
}
10195
}

0 commit comments

Comments
 (0)