referrerpolicy=no-referrer-when-downgrade

polkadot_runtime_parachains/on_demand/
benchmarking.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17//! On demand assigner pallet benchmarking.
18
19#![cfg(feature = "runtime-benchmarks")]
20
21use super::{Pallet, *};
22use crate::{
23	configuration::{HostConfiguration, Pallet as ConfigurationPallet},
24	paras::{Pallet as ParasPallet, ParaGenesisArgs, ParaKind, ParachainsCache},
25	shared::Pallet as ParasShared,
26};
27
28use alloc::vec;
29use frame_benchmarking::v2::*;
30use frame_system::RawOrigin;
31use sp_runtime::traits::Bounded;
32
33use polkadot_primitives::{
34	HeadData, Id as ParaId, SessionIndex, ValidationCode, ON_DEMAND_DEFAULT_QUEUE_MAX_SIZE,
35};
36
37// Constants for the benchmarking
38const SESSION_INDEX: SessionIndex = 1;
39
40// Initialize a parathread for benchmarking.
41pub fn init_parathread<T>(para_id: ParaId)
42where
43	T: Config + crate::paras::Config + crate::shared::Config,
44{
45	ParasShared::<T>::set_session_index(SESSION_INDEX);
46	let mut config = HostConfiguration::default();
47	config.scheduler_params.num_cores = 1;
48	ConfigurationPallet::<T>::force_set_active_config(config);
49	let mut parachains = ParachainsCache::new();
50	ParasPallet::<T>::initialize_para_now(
51		&mut parachains,
52		para_id,
53		&ParaGenesisArgs {
54			para_kind: ParaKind::Parathread,
55			genesis_head: HeadData(vec![1, 2, 3, 4]),
56			validation_code: ValidationCode(vec![1, 2, 3, 4]),
57		},
58	);
59}
60
61#[benchmarks]
62mod benchmarks {
63	/// We want to fill the queue to the maximum, so exactly one more item fits.
64	const MAX_FILL_BENCH: u32 = ON_DEMAND_DEFAULT_QUEUE_MAX_SIZE.saturating_sub(1);
65
66	use super::*;
67	#[benchmark]
68	fn place_order_keep_alive(s: Linear<1, MAX_FILL_BENCH>) {
69		// Setup
70		let caller = whitelisted_caller();
71		let para_id = ParaId::from(111u32);
72		init_parathread::<T>(para_id);
73		T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
74		Pallet::<T>::populate_queue(para_id, s);
75
76		#[extrinsic_call]
77		_(RawOrigin::Signed(caller.into()), BalanceOf::<T>::max_value(), para_id)
78	}
79
80	#[benchmark]
81	fn place_order_allow_death(s: Linear<1, MAX_FILL_BENCH>) {
82		// Setup
83		let caller = whitelisted_caller();
84		let para_id = ParaId::from(111u32);
85		init_parathread::<T>(para_id);
86		T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
87
88		Pallet::<T>::populate_queue(para_id, s);
89
90		#[extrinsic_call]
91		_(RawOrigin::Signed(caller.into()), BalanceOf::<T>::max_value(), para_id)
92	}
93
94	#[benchmark]
95	fn place_order_with_credits(s: Linear<1, MAX_FILL_BENCH>) {
96		// Setup
97		let caller: T::AccountId = whitelisted_caller();
98		let para_id = ParaId::from(111u32);
99		init_parathread::<T>(para_id);
100		Credits::<T>::insert(&caller, BalanceOf::<T>::max_value());
101
102		Pallet::<T>::populate_queue(para_id, s);
103
104		#[extrinsic_call]
105		_(RawOrigin::Signed(caller.into()), BalanceOf::<T>::max_value(), para_id)
106	}
107
108	impl_benchmark_test_suite!(
109		Pallet,
110		crate::mock::new_test_ext(
111			crate::on_demand::mock_helpers::GenesisConfigBuilder::default().build()
112		),
113		crate::mock::Test
114	);
115}