referrerpolicy=no-referrer-when-downgrade

pallet_example_basic/
benchmarking.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: MIT-0
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy of
7// this software and associated documentation files (the "Software"), to deal in
8// the Software without restriction, including without limitation the rights to
9// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10// of the Software, and to permit persons to whom the Software is furnished to do
11// so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in all
14// copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22// SOFTWARE.
23
24//! Benchmarking for `pallet-example-basic`.
25
26// Only enable this module for benchmarking.
27#![cfg(feature = "runtime-benchmarks")]
28
29use crate::*;
30use frame_benchmarking::v2::*;
31use frame_system::RawOrigin;
32
33// To actually run this benchmark on pallet-example-basic, we need to put this pallet into the
34//   runtime and compile it with `runtime-benchmarks` feature. The detail procedures are
35//   documented at:
36//   https://docs.substrate.io/reference/how-to-guides/weights/add-benchmarks/
37//
38// The auto-generated weight estimate of this pallet is copied over to the `weights.rs` file.
39// The exact command of how the estimate generated is printed at the top of the file.
40
41// Details on using the benchmarks macro can be seen at:
42//   https://paritytech.github.io/substrate/master/frame_benchmarking/trait.Benchmarking.html#tymethod.benchmarks
43#[benchmarks]
44mod benchmarks {
45	use super::*;
46
47	// This will measure the execution time of `set_dummy`.
48	#[benchmark]
49	fn set_dummy_benchmark() {
50		// This is the benchmark setup phase.
51		// `set_dummy` is a constant time function, hence we hard-code some random value here.
52		let value = 1000u32.into();
53		#[extrinsic_call]
54		set_dummy(RawOrigin::Root, value); // The execution phase is just running `set_dummy` extrinsic call
55
56		// This is the optional benchmark verification phase, asserting certain states.
57		assert_eq!(Dummy::<T>::get(), Some(value))
58	}
59
60	// An example method that returns a Result that can be called within a benchmark
61	fn example_result_method() -> Result<(), BenchmarkError> {
62		Ok(())
63	}
64
65	// This will measure the execution time of `accumulate_dummy`.
66	// The benchmark execution phase is shorthanded. When the name of the benchmark case is the same
67	// as the extrinsic call. `_(...)` is used to represent the extrinsic name.
68	// The benchmark verification phase is omitted.
69	#[benchmark]
70	fn accumulate_dummy() -> Result<(), BenchmarkError> {
71		let value = 1000u32.into();
72		// The caller account is whitelisted for DB reads/write by the benchmarking macro.
73		let caller: T::AccountId = whitelisted_caller();
74
75		// an example of calling something result-based within a benchmark using the ? operator
76		// this necessitates specifying the `Result<(), BenchmarkError>` return type
77		example_result_method()?;
78
79		// You can use `_` if the name of the Call matches the benchmark name.
80		#[extrinsic_call]
81		_(RawOrigin::Signed(caller), value);
82
83		// need this to be compatible with the return type
84		Ok(())
85	}
86
87	/// You can write helper functions in here since its a normal Rust module.
88	fn setup_vector(len: u32) -> Vec<u32> {
89		let mut vector = Vec::<u32>::new();
90		for i in (0..len).rev() {
91			vector.push(i);
92		}
93		vector
94	}
95
96	// This will measure the execution time of sorting a vector.
97	//
98	// Define `x` as a linear component with range `[0, =10_000]`. This means that the benchmarking
99	// will assume that the weight grows at a linear rate depending on `x`.
100	#[benchmark]
101	fn sort_vector(x: Linear<0, 10_000>) {
102		let mut vector = setup_vector(x);
103
104		// The benchmark execution phase could also be a closure with custom code:
105		#[block]
106		{
107			vector.sort();
108		}
109
110		// Check that it was sorted correctly. This will not be benchmarked and is just for
111		// verification.
112		vector.windows(2).for_each(|w| assert!(w[0] <= w[1]));
113	}
114
115	// This line generates test cases for benchmarking, and could be run by:
116	//   `cargo test -p pallet-example-basic --all-features`, you will see one line per case:
117	//   `test benchmarking::bench_sort_vector ... ok`
118	//   `test benchmarking::bench_accumulate_dummy ... ok`
119	//   `test benchmarking::bench_set_dummy_benchmark ... ok` in the result.
120	//
121	// The line generates three steps per benchmark, with repeat=1 and the three steps are
122	//   [low, mid, high] of the range.
123	impl_benchmark_test_suite!(Pallet, crate::tests::new_test_ext(), crate::tests::Test);
124}