referrerpolicy=no-referrer-when-downgrade

cumulus_pallet_weight_reclaim/
benchmarks.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: Apache-2.0
4
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// 	http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#![cfg(feature = "runtime-benchmarks")]
18
19use super::*;
20use frame_support::pallet_prelude::{DispatchClass, Pays};
21use frame_system::RawOrigin;
22use sp_runtime::traits::{AsTransactionAuthorizedOrigin, DispatchTransaction};
23
24#[frame_benchmarking::v2::benchmarks(
25	where T: Send + Sync,
26		<T as frame_system::Config>::RuntimeCall:
27			Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
28		<T as frame_system::Config>::RuntimeOrigin: AsTransactionAuthorizedOrigin,
29)]
30mod bench {
31	use super::*;
32	use frame_benchmarking::impl_test_function;
33
34	#[benchmark]
35	fn storage_weight_reclaim() {
36		let ext = StorageWeightReclaim::<T, ()>::new(());
37
38		let origin = RawOrigin::Root.into();
39		let call = T::RuntimeCall::from(frame_system::Call::remark { remark: alloc::vec![] });
40
41		let overestimate = 10_000;
42		let info = DispatchInfo {
43			call_weight: Weight::zero().add_proof_size(overestimate),
44			extension_weight: Weight::zero(),
45			class: DispatchClass::Normal,
46			pays_fee: Pays::No,
47		};
48
49		let post_info = PostDispatchInfo { actual_weight: None, pays_fee: Pays::No };
50
51		let mut block_weight = frame_system::ConsumedWeight::default();
52		block_weight.accrue(Weight::from_parts(0, overestimate), info.class);
53
54		frame_system::BlockWeight::<T>::put(block_weight);
55
56		#[block]
57		{
58			assert!(ext.test_run(origin, &call, &info, 0, 0, |_| Ok(post_info)).unwrap().is_ok());
59		}
60
61		let final_block_proof_size =
62			frame_system::BlockWeight::<T>::get().get(info.class).proof_size();
63
64		assert!(
65			final_block_proof_size < overestimate,
66			"The proof size measured should be less than {overestimate}"
67		);
68	}
69
70	impl_benchmark_test_suite!(Pallet, crate::tests::setup_test_ext_default(), crate::tests::Test);
71}