|
| 1 | +// This file is part of Substrate. |
| 2 | + |
| 3 | +// Copyright (C) 2019-2020 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 Proxy Pallet |
| 19 | + |
| 20 | +#![cfg(feature = "runtime-benchmarks")] |
| 21 | + |
| 22 | +use super::*; |
| 23 | +use frame_system::RawOrigin; |
| 24 | +use frame_benchmarking::{benchmarks, account}; |
| 25 | +use sp_runtime::traits::Bounded; |
| 26 | +use crate::Module as Proxy; |
| 27 | + |
| 28 | +const SEED: u32 = 0; |
| 29 | + |
| 30 | +fn add_proxies<T: Trait>(n: u32) -> Result<(), &'static str> { |
| 31 | + let caller: T::AccountId = account("caller", 0, SEED); |
| 32 | + T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value()); |
| 33 | + for i in 0..n { |
| 34 | + Proxy::<T>::add_proxy( |
| 35 | + RawOrigin::Signed(caller.clone()).into(), |
| 36 | + account("target", i, SEED), |
| 37 | + T::ProxyType::default() |
| 38 | + )?; |
| 39 | + } |
| 40 | + Ok(()) |
| 41 | +} |
| 42 | + |
| 43 | +benchmarks! { |
| 44 | + _ { |
| 45 | + let p in 1 .. (T::MaxProxies::get() - 1).into() => add_proxies::<T>(p)?; |
| 46 | + } |
| 47 | + |
| 48 | + proxy { |
| 49 | + let p in ...; |
| 50 | + // In this case the caller is the "target" proxy |
| 51 | + let caller: T::AccountId = account("target", p - 1, SEED); |
| 52 | + // ... and "real" is the traditional caller. This is not a typo. |
| 53 | + let real: T::AccountId = account("caller", 0, SEED); |
| 54 | + let call: <T as Trait>::Call = frame_system::Call::<T>::remark(vec![]).into(); |
| 55 | + }: _(RawOrigin::Signed(caller), real, Some(T::ProxyType::default()), Box::new(call)) |
| 56 | + |
| 57 | + add_proxy { |
| 58 | + let p in ...; |
| 59 | + let caller: T::AccountId = account("caller", 0, SEED); |
| 60 | + }: _(RawOrigin::Signed(caller), account("target", T::MaxProxies::get().into(), SEED), T::ProxyType::default()) |
| 61 | + |
| 62 | + remove_proxy { |
| 63 | + let p in ...; |
| 64 | + let caller: T::AccountId = account("caller", 0, SEED); |
| 65 | + }: _(RawOrigin::Signed(caller), account("target", 0, SEED), T::ProxyType::default()) |
| 66 | + |
| 67 | + remove_proxies { |
| 68 | + let p in ...; |
| 69 | + let caller: T::AccountId = account("caller", 0, SEED); |
| 70 | + }: _(RawOrigin::Signed(caller)) |
| 71 | +} |
| 72 | + |
| 73 | +#[cfg(test)] |
| 74 | +mod tests { |
| 75 | + use super::*; |
| 76 | + use crate::tests::{new_test_ext, Test}; |
| 77 | + use frame_support::assert_ok; |
| 78 | + |
| 79 | + #[test] |
| 80 | + fn test_benchmarks() { |
| 81 | + new_test_ext().execute_with(|| { |
| 82 | + assert_ok!(test_benchmark_proxy::<Test>()); |
| 83 | + assert_ok!(test_benchmark_add_proxy::<Test>()); |
| 84 | + assert_ok!(test_benchmark_remove_proxy::<Test>()); |
| 85 | + assert_ok!(test_benchmark_remove_proxies::<Test>()); |
| 86 | + }); |
| 87 | + } |
| 88 | +} |
0 commit comments