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;
25
26#[benchmarks]
27mod benchmarks {
28	use super::*;
29	use assigner_coretime::PartsOf57600;
30
31	#[benchmark]
32	fn request_revenue_at() {
33		let root_origin = <T as frame_system::Config>::RuntimeOrigin::root();
34		let mhr = <T as on_demand::Config>::MaxHistoricalRevenue::get();
35		frame_system::Pallet::<T>::set_block_number((mhr + 2).into());
36		let minimum_balance = <T as on_demand::Config>::Currency::minimum_balance();
37		let rev: BoundedVec<
38			<<T as on_demand::Config>::Currency as frame_support::traits::Currency<
39				T::AccountId,
40			>>::Balance,
41			T::MaxHistoricalRevenue,
42		> = BoundedVec::try_from((1..=mhr).map(|v| minimum_balance * v.into()).collect::<Vec<_>>())
43			.unwrap();
44		on_demand::Revenue::<T>::put(rev);
45
46		crate::paras::Heads::<T>::insert(ParaId::from(T::BrokerId::get()), vec![1, 2, 3]);
47
48		<T as on_demand::Config>::Currency::make_free_balance_be(
49			&<on_demand::Pallet<T>>::account_id(),
50			minimum_balance * (mhr * (mhr + 1)).into(),
51		);
52
53		#[extrinsic_call]
54		_(root_origin as <T as frame_system::Config>::RuntimeOrigin, mhr + 1)
55	}
56
57	#[benchmark]
58	fn request_core_count() {
59		// Setup
60		let root_origin = <T as frame_system::Config>::RuntimeOrigin::root();
61
62		#[extrinsic_call]
63		_(
64			root_origin as <T as frame_system::Config>::RuntimeOrigin,
65			// random core count
66			100,
67		)
68	}
69
70	#[benchmark]
71	fn assign_core(s: Linear<1, 100>) {
72		// Setup
73		let root_origin = <T as frame_system::Config>::RuntimeOrigin::root();
74
75		// Use parameterized assignment count
76		let mut assignments: Vec<(CoreAssignment, PartsOf57600)> = vec![0u16; s as usize - 1]
77			.into_iter()
78			.enumerate()
79			.map(|(index, parts)| {
80				(CoreAssignment::Task(index as u32), PartsOf57600::new_saturating(parts))
81			})
82			.collect();
83		// Parts must add up to exactly 57600. Here we add all the parts in one assignment, as
84		// it won't effect the weight and splitting up the parts into even groupings may not
85		// work for every value `s`.
86		assignments.push((CoreAssignment::Task(s as u32), PartsOf57600::FULL));
87
88		let core_index: BrokerCoreIndex = 0;
89
90		#[extrinsic_call]
91		_(
92			root_origin as <T as frame_system::Config>::RuntimeOrigin,
93			core_index,
94			BlockNumberFor::<T>::from(5u32),
95			assignments,
96			Some(BlockNumberFor::<T>::from(20u32)),
97		)
98	}
99
100	#[benchmark]
101	fn credit_account() {
102		// Setup
103		let root_origin = <T as frame_system::Config>::RuntimeOrigin::root();
104		let who: T::AccountId = whitelisted_caller();
105
106		#[extrinsic_call]
107		_(root_origin as <T as frame_system::Config>::RuntimeOrigin, who, 1_000_000u32.into())
108	}
109}