|
| 1 | +// This file is part of Substrate. |
| 2 | + |
| 3 | +// Copyright (C) Parity Technologies (UK) Ltd. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +// 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 at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// 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 and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +// Benchmarks for Utility Pallet |
| 19 | + |
| 20 | +#![cfg(feature = "runtime-benchmarks")] |
| 21 | + |
| 22 | +use super::*; |
| 23 | +use alloc::{vec, vec::Vec}; |
| 24 | +use frame_benchmarking::v1::{account, benchmarks, whitelisted_caller}; |
| 25 | +use frame_system::RawOrigin; |
| 26 | + |
| 27 | +const SEED: u32 = 0; |
| 28 | + |
| 29 | +fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) { |
| 30 | + frame_system::Pallet::<T>::assert_last_event(generic_event.into()); |
| 31 | +} |
| 32 | + |
| 33 | +benchmarks! { |
| 34 | + where_clause { where <T::RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin: Clone } |
| 35 | + batch { |
| 36 | + let c in 0 .. 1000; |
| 37 | + let mut calls: Vec<<T as Config>::RuntimeCall> = Vec::new(); |
| 38 | + for i in 0 .. c { |
| 39 | + let call = frame_system::Call::remark { remark: vec![] }.into(); |
| 40 | + calls.push(call); |
| 41 | + } |
| 42 | + let caller = whitelisted_caller(); |
| 43 | + }: _(RawOrigin::Signed(caller), calls) |
| 44 | + verify { |
| 45 | + assert_last_event::<T>(Event::BatchCompleted.into()) |
| 46 | + } |
| 47 | + |
| 48 | + as_derivative { |
| 49 | + let caller = account("caller", SEED, SEED); |
| 50 | + let call = Box::new(frame_system::Call::remark { remark: vec![] }.into()); |
| 51 | + // Whitelist caller account from further DB operations. |
| 52 | + let caller_key = frame_system::Account::<T>::hashed_key_for(&caller); |
| 53 | + frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into()); |
| 54 | + }: _(RawOrigin::Signed(caller), SEED as u16, call) |
| 55 | + |
| 56 | + batch_all { |
| 57 | + let c in 0 .. 1000; |
| 58 | + let mut calls: Vec<<T as Config>::RuntimeCall> = Vec::new(); |
| 59 | + for i in 0 .. c { |
| 60 | + let call = frame_system::Call::remark { remark: vec![] }.into(); |
| 61 | + calls.push(call); |
| 62 | + } |
| 63 | + let caller = whitelisted_caller(); |
| 64 | + }: _(RawOrigin::Signed(caller), calls) |
| 65 | + verify { |
| 66 | + assert_last_event::<T>(Event::BatchCompleted.into()) |
| 67 | + } |
| 68 | + |
| 69 | + dispatch_as { |
| 70 | + let caller = account("caller", SEED, SEED); |
| 71 | + let call = Box::new(frame_system::Call::remark { remark: vec![] }.into()); |
| 72 | + let origin: T::RuntimeOrigin = RawOrigin::Signed(caller).into(); |
| 73 | + let pallets_origin: <T::RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin = origin.caller().clone(); |
| 74 | + let pallets_origin = Into::<T::PalletsOrigin>::into(pallets_origin); |
| 75 | + }: _(RawOrigin::Root, Box::new(pallets_origin), call) |
| 76 | + |
| 77 | + force_batch { |
| 78 | + let c in 0 .. 1000; |
| 79 | + let mut calls: Vec<<T as Config>::RuntimeCall> = Vec::new(); |
| 80 | + for i in 0 .. c { |
| 81 | + let call = frame_system::Call::remark { remark: vec![] }.into(); |
| 82 | + calls.push(call); |
| 83 | + } |
| 84 | + let caller = whitelisted_caller(); |
| 85 | + }: _(RawOrigin::Signed(caller), calls) |
| 86 | + verify { |
| 87 | + assert_last_event::<T>(Event::BatchCompleted.into()) |
| 88 | + } |
| 89 | + |
| 90 | + impl_benchmark_test_suite!(Pallet, crate::tests::new_test_ext(), crate::tests::Test); |
| 91 | +} |
0 commit comments