git.delta.rocks / unique-network / refs/commits / 03a2300a9d25

difftreelog

source

pallets/scheduler/src/benchmarking.rs4.8 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617// Original license18// This file is part of Substrate.1920// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.21// SPDX-License-Identifier: Apache-2.02223// Licensed under the Apache License, Version 2.0 (the "License");24// you may not use this file except in compliance with the License.25// You may obtain a copy of the License at26//27// 	http://www.apache.org/licenses/LICENSE-2.028//29// Unless required by applicable law or agreed to in writing, software30// distributed under the License is distributed on an "AS IS" BASIS,31// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.32// See the License for the specific language governing permissions and33// limitations under the License.3435//! Scheduler pallet benchmarking.3637#![cfg(feature = "runtime-benchmarks")]3839use super::*;40use sp_std::{vec, prelude::*};41use frame_system::RawOrigin;42use frame_support::{ensure, traits::OnInitialize};43use frame_benchmarking::{benchmarks, impl_benchmark_test_suite};4445use crate::Module as Scheduler;46use frame_system::Pallet as System;4748const BLOCK_NUMBER: u32 = 2;4950// Add `n` named items to the schedule51fn fill_schedule<T: Config>(when: T::BlockNumber, n: u32) -> Result<(), &'static str> {52	// Essentially a no-op call.53	let call = frame_system::Call::set_storage { items: vec![] };54	for i in 0..n {55		// Named schedule is strictly heavier than anonymous56		Scheduler::<T>::do_schedule_named(57			i.encode(),58			DispatchTime::At(when),59			// Add periodicity60			Some((T::BlockNumber::one(), 100)),61			// HARD_DEADLINE priority means it gets executed no matter what62			0,63			frame_system::RawOrigin::Root.into(),64			call.clone().into(),65		)?;66	}67	ensure!(68		Agenda::<T>::get(when).len() == n as usize,69		"didn't fill schedule"70	);71	Ok(())72}7374benchmarks! {75	schedule {76		let s in 0 .. T::MaxScheduledPerBlock::get();77		let when = BLOCK_NUMBER.into();78		let periodic = Some((T::BlockNumber::one(), 100));79		let priority = 0;80		// Essentially a no-op call.81		let call = Box::new(frame_system::Call::set_storage { items: vec![] }.into());8283		fill_schedule::<T>(when, s)?;84	}: _(RawOrigin::Root, when, periodic, priority, call)85	verify {86		ensure!(87			Agenda::<T>::get(when).len() == (s + 1) as usize,88			"didn't add to schedule"89		);90	}9192	cancel {93		let s in 1 .. T::MaxScheduledPerBlock::get();94		let when = BLOCK_NUMBER.into();9596		fill_schedule::<T>(when, s)?;97		assert_eq!(Agenda::<T>::get(when).len(), s as usize);98	}: _(RawOrigin::Root, when, 0)99	verify {100		ensure!(101			Lookup::<T>::get(0.encode()).is_none(),102			"didn't remove from lookup"103		);104		// Removed schedule is NONE105		ensure!(106			Agenda::<T>::get(when)[0].is_none(),107			"didn't remove from schedule"108		);109	}110111	schedule_named {112		let s in 0 .. T::MaxScheduledPerBlock::get();113		let id = s.encode();114		let when = BLOCK_NUMBER.into();115		let periodic = Some((T::BlockNumber::one(), 100));116		let priority = 0;117		// Essentially a no-op call.118		let call = Box::new(frame_system::Call::set_storage { items: vec![] }.into());119120		fill_schedule::<T>(when, s)?;121	}: _(RawOrigin::Root, id, when, periodic, priority, call)122	verify {123		ensure!(124			Agenda::<T>::get(when).len() == (s + 1) as usize,125			"didn't add to schedule"126		);127	}128129	cancel_named {130		let s in 1 .. T::MaxScheduledPerBlock::get();131		let when = BLOCK_NUMBER.into();132133		fill_schedule::<T>(when, s)?;134	}: _(RawOrigin::Root, 0.encode())135	verify {136		ensure!(137			Lookup::<T>::get(0.encode()).is_none(),138			"didn't remove from lookup"139		);140		// Removed schedule is NONE141		ensure!(142			Agenda::<T>::get(when)[0].is_none(),143			"didn't remove from schedule"144		);145	}146147	// TODO [#7141]: Make this more complex and flexible so it can be used in automation.148	#[extra]149	on_initialize {150		let s in 0 .. T::MaxScheduledPerBlock::get();151		let when = BLOCK_NUMBER.into();152		fill_schedule::<T>(when, s)?;153	}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }154	verify {155		assert_eq!(System::<T>::event_count(), s);156		// Next block should have all the schedules again157		ensure!(158			Agenda::<T>::get(when + T::BlockNumber::one()).len() == s as usize,159			"didn't append schedule"160		);161	}162}163164impl_benchmark_test_suite!(Scheduler, crate::tests::new_test_ext(), crate::tests::Test,);