git.delta.rocks / unique-network / refs/commits / 50b15345cfd5

difftreelog

source

pallets/scheduler/src/benchmarking.rs4.0 KiBsourcehistory
1// This file is part of Substrate.23// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.4// SPDX-License-Identifier: Apache-2.056// Licensed under the Apache License, Version 2.0 (the "License");7// you may not use this file except in compliance with the License.8// You may obtain a copy of the License at9//10// 	http://www.apache.org/licenses/LICENSE-2.011//12// Unless required by applicable law or agreed to in writing, software13// distributed under the License is distributed on an "AS IS" BASIS,14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15// See the License for the specific language governing permissions and16// limitations under the License.1718//! Scheduler pallet benchmarking.1920#![cfg(feature = "runtime-benchmarks")]2122use super::*;23use sp_std::{vec, prelude::*};24use frame_system::RawOrigin;25use frame_support::{ensure, traits::OnInitialize};26use frame_benchmarking::{benchmarks, impl_benchmark_test_suite};2728use crate::Module as Scheduler;29use frame_system::Pallet as System;3031const BLOCK_NUMBER: u32 = 2;3233// Add `n` named items to the schedule34fn fill_schedule<T: Config>(when: T::BlockNumber, n: u32) -> Result<(), &'static str> {35	// Essentially a no-op call.36	let call = frame_system::Call::set_storage(vec![]);37	for i in 0..n {38		// Named schedule is strictly heavier than anonymous39		Scheduler::<T>::do_schedule_named(40			i.encode(),41			DispatchTime::At(when),42			// Add periodicity43			Some((T::BlockNumber::one(), 100)),44			// HARD_DEADLINE priority means it gets executed no matter what45			0,46			frame_system::RawOrigin::Root.into(),47			call.clone().into(),48		)?;49	}50	ensure!(51		Agenda::<T>::get(when).len() == n as usize,52		"didn't fill schedule"53	);54	Ok(())55}5657benchmarks! {58	schedule {59		let s in 0 .. T::MaxScheduledPerBlock::get();60		let when = BLOCK_NUMBER.into();61		let periodic = Some((T::BlockNumber::one(), 100));62		let priority = 0;63		// Essentially a no-op call.64		let call = Box::new(frame_system::Call::set_storage(vec![]).into());6566		fill_schedule::<T>(when, s)?;67	}: _(RawOrigin::Root, when, periodic, priority, call)68	verify {69		ensure!(70			Agenda::<T>::get(when).len() == (s + 1) as usize,71			"didn't add to schedule"72		);73	}7475	cancel {76		let s in 1 .. T::MaxScheduledPerBlock::get();77		let when = BLOCK_NUMBER.into();7879		fill_schedule::<T>(when, s)?;80		assert_eq!(Agenda::<T>::get(when).len(), s as usize);81	}: _(RawOrigin::Root, when, 0)82	verify {83		ensure!(84			Lookup::<T>::get(0.encode()).is_none(),85			"didn't remove from lookup"86		);87		// Removed schedule is NONE88		ensure!(89			Agenda::<T>::get(when)[0].is_none(),90			"didn't remove from schedule"91		);92	}9394	schedule_named {95		let s in 0 .. T::MaxScheduledPerBlock::get();96		let id = s.encode();97		let when = BLOCK_NUMBER.into();98		let periodic = Some((T::BlockNumber::one(), 100));99		let priority = 0;100		// Essentially a no-op call.101		let call = Box::new(frame_system::Call::set_storage(vec![]).into());102103		fill_schedule::<T>(when, s)?;104	}: _(RawOrigin::Root, id, when, periodic, priority, call)105	verify {106		ensure!(107			Agenda::<T>::get(when).len() == (s + 1) as usize,108			"didn't add to schedule"109		);110	}111112	cancel_named {113		let s in 1 .. T::MaxScheduledPerBlock::get();114		let when = BLOCK_NUMBER.into();115116		fill_schedule::<T>(when, s)?;117	}: _(RawOrigin::Root, 0.encode())118	verify {119		ensure!(120			Lookup::<T>::get(0.encode()).is_none(),121			"didn't remove from lookup"122		);123		// Removed schedule is NONE124		ensure!(125			Agenda::<T>::get(when)[0].is_none(),126			"didn't remove from schedule"127		);128	}129130	// TODO [#7141]: Make this more complex and flexible so it can be used in automation.131	#[extra]132	on_initialize {133		let s in 0 .. T::MaxScheduledPerBlock::get();134		let when = BLOCK_NUMBER.into();135		fill_schedule::<T>(when, s)?;136	}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }137	verify {138		assert_eq!(System::<T>::event_count(), s);139		// Next block should have all the schedules again140		ensure!(141			Agenda::<T>::get(when + T::BlockNumber::one()).len() == s as usize,142			"didn't append schedule"143		);144	}145}146147impl_benchmark_test_suite!(Scheduler, crate::tests::new_test_ext(), crate::tests::Test,);