referrerpolicy=no-referrer-when-downgrade

pallet_glutton/
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//! Glutton pallet benchmarking.
19//!
20//! Has to be compiled and run twice to calibrate on new hardware.
21
22#[cfg(feature = "runtime-benchmarks")]
23use frame_benchmarking::v2::*;
24use frame_support::{pallet_prelude::*, weights::constants::*};
25use frame_system::RawOrigin;
26use sp_runtime::{traits::One, Perbill};
27
28use crate::*;
29
30#[benchmarks]
31mod benchmarks {
32	use super::*;
33
34	#[benchmark]
35	fn initialize_pallet_grow(n: Linear<0, 1_000>) -> Result<(), BenchmarkError> {
36		#[block]
37		{
38			Pallet::<T>::initialize_pallet(RawOrigin::Root.into(), n, None)?;
39		}
40
41		assert_eq!(TrashDataCount::<T>::get(), n);
42
43		Ok(())
44	}
45
46	#[benchmark]
47	fn initialize_pallet_shrink(n: Linear<0, 1_000>) -> Result<(), BenchmarkError> {
48		Pallet::<T>::initialize_pallet(RawOrigin::Root.into(), n, None)?;
49
50		#[block]
51		{
52			Pallet::<T>::initialize_pallet(RawOrigin::Root.into(), 0, Some(n))?;
53		}
54
55		assert_eq!(TrashDataCount::<T>::get(), 0);
56
57		Ok(())
58	}
59
60	#[benchmark]
61	fn waste_ref_time_iter(i: Linear<0, 100_000>) {
62		#[block]
63		{
64			Pallet::<T>::waste_ref_time_iter(vec![0u8; 64], i);
65		}
66	}
67
68	#[benchmark]
69	fn waste_proof_size_some(i: Linear<0, 5_000>) {
70		(0..5000).for_each(|i| TrashData::<T>::insert(i, [i as u8; 1024]));
71
72		#[block]
73		{
74			(0..i).for_each(|i| {
75				TrashData::<T>::get(i);
76			})
77		}
78	}
79
80	// For manual verification only.
81	#[benchmark]
82	fn on_idle_high_proof_waste() {
83		(0..5000).for_each(|i| TrashData::<T>::insert(i, [i as u8; 1024]));
84		let _ = Pallet::<T>::set_compute(RawOrigin::Root.into(), One::one());
85		let _ = Pallet::<T>::set_storage(RawOrigin::Root.into(), One::one());
86
87		#[block]
88		{
89			Pallet::<T>::on_idle(
90				frame_system::Pallet::<T>::block_number(),
91				Weight::from_parts(WEIGHT_REF_TIME_PER_MILLIS * 100, WEIGHT_PROOF_SIZE_PER_MB * 5),
92			);
93		}
94	}
95
96	// For manual verification only.
97	#[benchmark]
98	fn on_idle_low_proof_waste() {
99		(0..5000).for_each(|i| TrashData::<T>::insert(i, [i as u8; 1024]));
100		let _ = Pallet::<T>::set_compute(RawOrigin::Root.into(), One::one());
101		let _ = Pallet::<T>::set_storage(RawOrigin::Root.into(), One::one());
102
103		#[block]
104		{
105			Pallet::<T>::on_idle(
106				frame_system::Pallet::<T>::block_number(),
107				Weight::from_parts(WEIGHT_REF_TIME_PER_MILLIS * 100, WEIGHT_PROOF_SIZE_PER_KB * 20),
108			);
109		}
110	}
111
112	#[benchmark]
113	fn empty_on_idle() {
114		// Enough weight to do nothing.
115		#[block]
116		{
117			Pallet::<T>::on_idle(
118				frame_system::Pallet::<T>::block_number(),
119				T::WeightInfo::empty_on_idle(),
120			);
121		}
122	}
123
124	#[benchmark]
125	fn set_compute() {
126		#[extrinsic_call]
127		_(RawOrigin::Root, FixedU64::from_perbill(Perbill::from_percent(50)));
128	}
129
130	#[benchmark]
131	fn set_storage() {
132		#[extrinsic_call]
133		_(RawOrigin::Root, FixedU64::from_perbill(Perbill::from_percent(50)));
134	}
135
136	impl_benchmark_test_suite! {
137		Pallet,
138		mock::new_test_ext(),
139		mock::Test
140	}
141}