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 near maximum for realistic benchmarking.
64	/// Leave room for exactly one more item.
65	const QUEUE_BENCH_SIZE: u32 = ON_DEMAND_DEFAULT_QUEUE_MAX_SIZE.saturating_sub(1);
66
67	use super::*;
68	#[benchmark]
69	fn place_order_keep_alive() {
70		// Setup
71		let caller = whitelisted_caller();
72		let para_id = ParaId::from(111u32);
73		init_parathread::<T>(para_id);
74		T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
75		// Populate queue to near-max to benchmark realistic conditions
76		Pallet::<T>::populate_queue(para_id, QUEUE_BENCH_SIZE);
77
78		#[extrinsic_call]
79		_(RawOrigin::Signed(caller.into()), BalanceOf::<T>::max_value(), para_id)
80	}
81
82	#[benchmark]
83	fn place_order_allow_death() {
84		// Setup
85		let caller = whitelisted_caller();
86		let para_id = ParaId::from(111u32);
87		init_parathread::<T>(para_id);
88		T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
89		// Populate queue to near-max to benchmark realistic conditions
90		Pallet::<T>::populate_queue(para_id, QUEUE_BENCH_SIZE);
91
92		#[extrinsic_call]
93		_(RawOrigin::Signed(caller.into()), BalanceOf::<T>::max_value(), para_id)
94	}
95
96	#[benchmark]
97	fn place_order_with_credits() {
98		// Setup
99		let caller: T::AccountId = whitelisted_caller();
100		let para_id = ParaId::from(111u32);
101		init_parathread::<T>(para_id);
102		Credits::<T>::insert(&caller, BalanceOf::<T>::max_value());
103		// Populate queue to near-max to benchmark realistic conditions
104		Pallet::<T>::populate_queue(para_id, QUEUE_BENCH_SIZE);
105
106		#[extrinsic_call]
107		_(RawOrigin::Signed(caller.into()), BalanceOf::<T>::max_value(), para_id)
108	}
109
110	impl_benchmark_test_suite!(
111		Pallet,
112		crate::mock::new_test_ext(
113			crate::on_demand::mock_helpers::GenesisConfigBuilder::default().build()
114		),
115		crate::mock::Test
116	);
117}