pallet_example_kitchensink/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-kitchensink`.
25
26// Only enable this module for benchmarking.
27#![cfg(feature = "runtime-benchmarks")]
28use super::*;
29
30#[allow(unused)]
31use crate::Pallet as Kitchensink;
32
33use frame_benchmarking::v2::*;
34use frame_support::pallet_prelude::TransactionSource;
35use frame_system::RawOrigin;
36
37// To actually run this benchmark on pallet-example-kitchensink, we need to put this pallet into the
38// runtime and compile it with `runtime-benchmarks` feature. The detail procedures are
39// documented at:
40// https://docs.substrate.io/reference/how-to-guides/weights/add-benchmarks/
41//
42// The auto-generated weight estimate of this pallet is copied over to the `weights.rs` file.
43// The exact command of how the estimate generated is printed at the top of the file.
44
45// Details on using the benchmarks macro can be seen at:
46// https://paritytech.github.io/substrate/master/frame_benchmarking/trait.Benchmarking.html#tymethod.benchmarks
47#[benchmarks]
48mod benchmarks {
49 use super::*;
50
51 // This will measure the execution time of `set_foo`.
52 #[benchmark]
53 fn set_foo_benchmark() {
54 // This is the benchmark setup phase.
55 // `set_foo` is a constant time function, hence we hard-code some random value here.
56 let value = 1000u32.into();
57 #[extrinsic_call]
58 set_foo(RawOrigin::Root, value, 10u128); // The execution phase is just running `set_foo` extrinsic call
59
60 // This is the optional benchmark verification phase, asserting certain states.
61 assert_eq!(Foo::<T>::get(), Some(value))
62 }
63
64 // This will measure the execution time of `set_foo_using_authorize`.
65 #[benchmark]
66 fn set_foo_using_authorize() {
67 // This is the benchmark setup phase.
68
69 // `set_foo_using_authorize` is only authorized when value is 42 so we will use it.
70 let value = 42u32;
71 // We dispatch with authorized origin, it is the origin resulting from authorization.
72 let origin = RawOrigin::Authorized;
73
74 #[extrinsic_call]
75 _(origin, value); // The execution phase is just running `set_foo_using_authorize` extrinsic call
76
77 // This is the optional benchmark verification phase, asserting certain states.
78 assert_eq!(Foo::<T>::get(), Some(42))
79 }
80
81 // This will measure the weight for the closure in `[pallet::authorize(...)]`.
82 #[benchmark]
83 fn authorize_set_foo_using_authorize() {
84 // This is the benchmark setup phase.
85
86 let call = Call::<T>::set_foo_using_authorize { new_foo: 42 };
87 let source = TransactionSource::External;
88 Foo::<T>::kill();
89
90 // We use a block with specific code to benchmark the closure.
91 #[block]
92 {
93 use frame_support::traits::Authorize;
94 call.authorize(source)
95 .expect("Call give some authorization")
96 .expect("Authorization is successful");
97 }
98 }
99
100 // This line generates test cases for benchmarking, and could be run by:
101 // `cargo test -p pallet-example-kitchensink --all-features`, you will see one line per case:
102 // `test benchmarking::bench_set_foo_benchmark ... ok`
103 // `test benchmarking::bench_set_foo_using_authorize_benchmark ... ok` in the result.
104 // `test benchmarking::bench_authorize_set_foo_using_authorize_benchmark ... ok` in the
105 // result.
106 //
107 // The line generates three steps per benchmark, with repeat=1 and the three steps are
108 // [low, mid, high] of the range.
109 impl_benchmark_test_suite!(Kitchensink, crate::tests::new_test_ext(), crate::tests::Test);
110}