referrerpolicy=no-referrer-when-downgrade

polkadot_runtime_parachains/coretime/
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//! Coretime pallet benchmarking.
18
19#![cfg(feature = "runtime-benchmarks")]
20
21use super::*;
22use frame_benchmarking::v2::*;
23use frame_support::traits::OriginTrait;
24use pallet_broker::CoreIndex as BrokerCoreIndex;
25use polkadot_primitives::Id as ParaId;
26
27#[benchmarks]
28mod benchmarks {
29	use super::*;
30	use scheduler::PartsOf57600;
31
32	#[benchmark]
33	fn request_revenue_at() {
34		// Ensure broker parachain is reachable for XCM delivery
35		Pallet::<T>::ensure_broker_parachain_reachable();
36
37		let root_origin = <T as frame_system::Config>::RuntimeOrigin::root();
38		let mhr = <T as on_demand::Config>::MaxHistoricalRevenue::get();
39		frame_system::Pallet::<T>::set_block_number((mhr + 2).into());
40		let minimum_balance = <T as on_demand::Config>::Currency::minimum_balance();
41		let rev: BoundedVec<
42			<<T as on_demand::Config>::Currency as frame_support::traits::Currency<
43				T::AccountId,
44			>>::Balance,
45			T::MaxHistoricalRevenue,
46		> = BoundedVec::try_from((1..=mhr).map(|v| minimum_balance * v.into()).collect::<Vec<_>>())
47			.unwrap();
48		on_demand::Pallet::<T>::set_revenue(rev);
49
50		crate::paras::Heads::<T>::insert(ParaId::from(T::BrokerId::get()), vec![1, 2, 3]);
51
52		<T as on_demand::Config>::Currency::make_free_balance_be(
53			&<on_demand::Pallet<T>>::account_id(),
54			minimum_balance * (mhr * (mhr + 1)).into(),
55		);
56
57		#[extrinsic_call]
58		_(root_origin as <T as frame_system::Config>::RuntimeOrigin, mhr + 1)
59	}
60
61	#[benchmark]
62	fn request_core_count() {
63		// Ensure broker parachain is reachable for XCM delivery
64		Pallet::<T>::ensure_broker_parachain_reachable();
65
66		// Setup
67		let root_origin = <T as frame_system::Config>::RuntimeOrigin::root();
68
69		#[extrinsic_call]
70		_(
71			root_origin as <T as frame_system::Config>::RuntimeOrigin,
72			// random core count
73			100,
74		)
75	}
76
77	#[benchmark]
78	fn assign_core(s: Linear<1, 100>) {
79		// Ensure broker parachain is reachable for XCM delivery
80		Pallet::<T>::ensure_broker_parachain_reachable();
81
82		// Setup
83		let root_origin = <T as frame_system::Config>::RuntimeOrigin::root();
84
85		// Use parameterized assignment count
86		let mut assignments: Vec<(CoreAssignment, PartsOf57600)> = vec![0u16; s as usize - 1]
87			.into_iter()
88			.enumerate()
89			.map(|(index, parts)| {
90				(CoreAssignment::Task(index as u32), PartsOf57600::new_saturating(parts))
91			})
92			.collect();
93		// Parts must add up to exactly 57600. Here we add all the parts in one assignment, as
94		// it won't effect the weight and splitting up the parts into even groupings may not
95		// work for every value `s`.
96		assignments.push((CoreAssignment::Task(s as u32), PartsOf57600::FULL));
97
98		let core_index: BrokerCoreIndex = 0;
99
100		#[extrinsic_call]
101		_(
102			root_origin as <T as frame_system::Config>::RuntimeOrigin,
103			core_index,
104			BlockNumberFor::<T>::from(5u32),
105			assignments,
106			Some(BlockNumberFor::<T>::from(20u32)),
107		)
108	}
109
110	#[benchmark]
111	fn queue_on_demand_batch(s: Linear<0, 100>) {
112		// Setup
113		let root_origin = <T as frame_system::Config>::RuntimeOrigin::root();
114
115		on_demand::Pallet::<T>::populate_queue(
116			ParaId::from(111u32),
117			polkadot_primitives::ON_DEMAND_MAX_QUEUE_MAX_SIZE - s,
118		);
119
120		// Use parameterized order count
121		let batch: Vec<(ParaId, BlockNumberFor<T>)> = vec![0u32; s as usize]
122			.into_iter()
123			.enumerate()
124			.map(|(index, block_number)| {
125				(ParaId::from(index as u32), BlockNumberFor::<T>::from(block_number))
126			})
127			.collect();
128
129		#[extrinsic_call]
130		_(root_origin as <T as frame_system::Config>::RuntimeOrigin, batch)
131	}
132
133	#[benchmark]
134	fn credit_account() {
135		// Ensure broker parachain is reachable for XCM delivery
136		Pallet::<T>::ensure_broker_parachain_reachable();
137
138		// Setup
139		let root_origin = <T as frame_system::Config>::RuntimeOrigin::root();
140		let who: T::AccountId = whitelisted_caller();
141
142		#[extrinsic_call]
143		_(root_origin as <T as frame_system::Config>::RuntimeOrigin, who, 1_000_000u32.into())
144	}
145}