referrerpolicy=no-referrer-when-downgrade

pallet_utility/
benchmarking.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18// Benchmarks for Utility Pallet
19
20#![cfg(feature = "runtime-benchmarks")]
21
22use alloc::vec;
23use frame_benchmarking::{benchmarking::add_to_whitelist, v2::*};
24use frame_system::RawOrigin;
25
26use crate::*;
27
28const SEED: u32 = 0;
29
30fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
31	frame_system::Pallet::<T>::assert_last_event(generic_event.into());
32}
33
34#[benchmarks]
35mod benchmark {
36	use super::*;
37
38	#[benchmark]
39	fn batch(c: Linear<0, 1000>) {
40		let calls = vec![frame_system::Call::remark { remark: vec![] }.into(); c as usize];
41		let caller = whitelisted_caller();
42
43		#[extrinsic_call]
44		_(RawOrigin::Signed(caller), calls);
45
46		assert_last_event::<T>(Event::BatchCompleted.into());
47	}
48
49	#[benchmark]
50	fn as_derivative() {
51		let caller = account("caller", SEED, SEED);
52		let call = Box::new(frame_system::Call::remark { remark: vec![] }.into());
53		// Whitelist caller account from further DB operations.
54		let caller_key = frame_system::Account::<T>::hashed_key_for(&caller);
55		add_to_whitelist(caller_key.into());
56
57		#[extrinsic_call]
58		_(RawOrigin::Signed(caller), SEED as u16, call);
59	}
60
61	#[benchmark]
62	fn batch_all(c: Linear<0, 1000>) {
63		let calls = vec![frame_system::Call::remark { remark: vec![] }.into(); c as usize];
64		let caller = whitelisted_caller();
65
66		#[extrinsic_call]
67		_(RawOrigin::Signed(caller), calls);
68
69		assert_last_event::<T>(Event::BatchCompleted.into());
70	}
71
72	#[benchmark]
73	fn dispatch_as() {
74		let caller = account("caller", SEED, SEED);
75		let call = Box::new(frame_system::Call::remark { remark: vec![] }.into());
76		let origin = T::RuntimeOrigin::from(RawOrigin::Signed(caller));
77		let pallets_origin = origin.caller().clone();
78		let pallets_origin = T::PalletsOrigin::from(pallets_origin);
79
80		#[extrinsic_call]
81		_(RawOrigin::Root, Box::new(pallets_origin), call);
82	}
83
84	#[benchmark]
85	fn force_batch(c: Linear<0, 1000>) {
86		let calls = vec![frame_system::Call::remark { remark: vec![] }.into(); c as usize];
87		let caller = whitelisted_caller();
88
89		#[extrinsic_call]
90		_(RawOrigin::Signed(caller), calls);
91
92		assert_last_event::<T>(Event::BatchCompleted.into());
93	}
94
95	#[benchmark]
96	fn dispatch_as_fallible() {
97		let caller = account("caller", SEED, SEED);
98		let call = Box::new(frame_system::Call::remark { remark: vec![] }.into());
99		let origin: T::RuntimeOrigin = RawOrigin::Signed(caller).into();
100		let pallets_origin = origin.caller().clone();
101		let pallets_origin = T::PalletsOrigin::from(pallets_origin);
102
103		#[extrinsic_call]
104		_(RawOrigin::Root, Box::new(pallets_origin), call);
105	}
106
107	#[benchmark]
108	fn if_else() {
109		// Failing main call.
110		let main_call = Box::new(frame_system::Call::set_code { code: vec![1] }.into());
111		let fallback_call = Box::new(frame_system::Call::remark { remark: vec![1] }.into());
112		let caller = whitelisted_caller();
113
114		#[extrinsic_call]
115		_(RawOrigin::Signed(caller), main_call, fallback_call);
116	}
117
118	impl_benchmark_test_suite! {
119		Pallet,
120		tests::new_test_ext(),
121		tests::Test
122	}
123}