git.delta.rocks / unique-network / refs/commits / 90fcef986d5a

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!(Agenda::<T>::get(when).len() == n as usize, "didn't fill schedule");51	Ok(())52}5354benchmarks! {55	schedule {56		let s in 0 .. T::MaxScheduledPerBlock::get();57		let when = BLOCK_NUMBER.into();58		let periodic = Some((T::BlockNumber::one(), 100));59		let priority = 0;60		// Essentially a no-op call.61		let call = Box::new(frame_system::Call::set_storage(vec![]).into());6263		fill_schedule::<T>(when, s)?;64	}: _(RawOrigin::Root, when, periodic, priority, call)65	verify {66		ensure!(67			Agenda::<T>::get(when).len() == (s + 1) as usize,68			"didn't add to schedule"69		);70	}7172	cancel {73		let s in 1 .. T::MaxScheduledPerBlock::get();74		let when = BLOCK_NUMBER.into();7576		fill_schedule::<T>(when, s)?;77		assert_eq!(Agenda::<T>::get(when).len(), s as usize);78	}: _(RawOrigin::Root, when, 0)79	verify {80		ensure!(81			Lookup::<T>::get(0.encode()).is_none(),82			"didn't remove from lookup"83		);84		// Removed schedule is NONE85		ensure!(86			Agenda::<T>::get(when)[0].is_none(),87			"didn't remove from schedule"88		);89	}9091	schedule_named {92		let s in 0 .. T::MaxScheduledPerBlock::get();93		let id = s.encode();94		let when = BLOCK_NUMBER.into();95		let periodic = Some((T::BlockNumber::one(), 100));96		let priority = 0;97		// Essentially a no-op call.98		let call = Box::new(frame_system::Call::set_storage(vec![]).into());99100		fill_schedule::<T>(when, s)?;101	}: _(RawOrigin::Root, id, when, periodic, priority, call)102	verify {103		ensure!(104			Agenda::<T>::get(when).len() == (s + 1) as usize,105			"didn't add to schedule"106		);107	}108109	cancel_named {110		let s in 1 .. T::MaxScheduledPerBlock::get();111		let when = BLOCK_NUMBER.into();112113		fill_schedule::<T>(when, s)?;114	}: _(RawOrigin::Root, 0.encode())115	verify {116		ensure!(117			Lookup::<T>::get(0.encode()).is_none(),118			"didn't remove from lookup"119		);120		// Removed schedule is NONE121		ensure!(122			Agenda::<T>::get(when)[0].is_none(),123			"didn't remove from schedule"124		);125	}126127	// TODO [#7141]: Make this more complex and flexible so it can be used in automation.128	#[extra]129	on_initialize {130		let s in 0 .. T::MaxScheduledPerBlock::get();131		let when = BLOCK_NUMBER.into();132		fill_schedule::<T>(when, s)?;133	}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }134	verify {135		assert_eq!(System::<T>::event_count(), s);136		// Next block should have all the schedules again137		ensure!(138			Agenda::<T>::get(when + T::BlockNumber::one()).len() == s as usize,139			"didn't append schedule"140		);141	}142}143144impl_benchmark_test_suite!(145	Scheduler,146	crate::tests::new_test_ext(),147	crate::tests::Test,148);