This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
benchmarking.rs
128 lines (106 loc) · 4.61 KB
/
benchmarking.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Benchmarking for `pallet-example-basic`.
// Only enable this module for benchmarking.
#![cfg(feature = "runtime-benchmarks")]
use crate::*;
use frame_benchmarking::v2::*;
use frame_system::RawOrigin;
// To actually run this benchmark on pallet-example-basic, we need to put this pallet into the
// runtime and compile it with `runtime-benchmarks` feature. The detail procedures are
// documented at:
// https://docs.substrate.io/reference/how-to-guides/weights/add-benchmarks/
//
// The auto-generated weight estimate of this pallet is copied over to the `weights.rs` file.
// The exact command of how the estimate generated is printed at the top of the file.
// Details on using the benchmarks macro can be seen at:
// https://paritytech.github.io/substrate/master/frame_benchmarking/trait.Benchmarking.html#tymethod.benchmarks
#[benchmarks]
mod benchmarks {
use super::*;
const MY_CONST: u32 = 100;
const fn my_fn() -> u32 {
200
}
frame_support::parameter_types! {
const MyConst: u32 = MY_CONST;
}
// This will measure the execution time of `set_dummy`.
#[benchmark]
fn set_dummy_benchmark() {
// This is the benchmark setup phase.
// `set_dummy` is a constant time function, hence we hard-code some random value here.
let value = 1000u32.into();
#[extrinsic_call]
set_dummy(RawOrigin::Root, value); // The execution phase is just running `set_dummy` extrinsic call
// This is the optional benchmark verification phase, asserting certain states.
assert_eq!(Pallet::<T>::dummy(), Some(value))
}
// An example method that returns a Result that can be called within a benchmark
fn example_result_method() -> Result<(), BenchmarkError> {
Ok(())
}
// This will measure the execution time of `accumulate_dummy`.
// The benchmark execution phase is shorthanded. When the name of the benchmark case is the same
// as the extrinsic call. `_(...)` is used to represent the extrinsic name.
// The benchmark verification phase is omitted.
#[benchmark]
fn accumulate_dummy() -> Result<(), BenchmarkError> {
let value = 1000u32.into();
// The caller account is whitelisted for DB reads/write by the benchmarking macro.
let caller: T::AccountId = whitelisted_caller();
// an example of calling something result-based within a benchmark using the ? operator
// this necessitates specifying the `Result<(), BenchmarkError>` return type
example_result_method()?;
// You can use `_` if the name of the Call matches the benchmark name.
#[extrinsic_call]
_(RawOrigin::Signed(caller), value);
// need this to be compatible with the return type
Ok(())
}
/// You can write helper functions in here since its a normal Rust module.
fn setup_vector(len: u32) -> Vec<u32> {
let mut vector = Vec::<u32>::new();
for i in (0..len).rev() {
vector.push(i);
}
vector
}
// This will measure the execution time of sorting a vector.
//
// Define `x` as a linear component with range `[0, =10_000]`. This means that the benchmarking
// will assume that the weight grows at a linear rate depending on `x`.
#[benchmark]
fn sort_vector(x: Linear<{MY_CONST * 2}, {my_fn() + MyConst::get()}>) {
let mut vector = setup_vector(x);
// The benchmark execution phase could also be a closure with custom code:
#[block]
{
vector.sort();
}
// Check that it was sorted correctly. This will not be benchmarked and is just for
// verification.
vector.windows(2).for_each(|w| assert!(w[0] <= w[1]));
}
// This line generates test cases for benchmarking, and could be run by:
// `cargo test -p pallet-example-basic --all-features`, you will see one line per case:
// `test benchmarking::bench_sort_vector ... ok`
// `test benchmarking::bench_accumulate_dummy ... ok`
// `test benchmarking::bench_set_dummy_benchmark ... ok` in the result.
//
// The line generates three steps per benchmark, with repeat=1 and the three steps are
// [low, mid, high] of the range.
impl_benchmark_test_suite!(Pallet, crate::tests::new_test_ext(), crate::tests::Test);
}