Files
pezkuwi-subxt/substrate/frame/proxy/src/benchmarking.rs
T
Peter Goodspeed-Niklaus 44d5aba80d Create a macro which automates creation of benchmark test suites. (#8104)
* Create a macro which automates creation of benchmark test suites.

* bump impl_version

* allow unused on test_bench_by_name

* use proper doctest ignore attribute

* Explicitly hand the Module to the test suite

Much better practice than depending on it showing up implicitly in
the namespace.

* explicitly import what we need into `mod tests`

* bench_module is `ident` not `tt`

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* allow end users to specify arguments for new_test_ext

This turned out to be surprisingly easy. On reflection, it turns out
that of course the compiler can't eagerly evaluate the function call,
but needs to paste it in everywhere desired.

* enable explicitly specifying the path to the benchmarks invocation

also enable optional trailing commas

* Revert "bump impl_version"

This reverts commit 0209e4de33fd43873f8cfc6875815d0fd6151e63.

* list failing benchmark tests and the errors which caused the failure

* harden benchmark tests against internal panics

* suppress warning about ignored profiles

unfortunately, setting the profile here doesn't do anything; we'd
need to set it in every leaf package anyway. However, as this was
just making the default explicit anyway, I think it's safe enough
to remove entirely.

* impl_benchmark_test_suite for assets

* impl_benchmark_test_suite for balances

* impl_benchmark_test_suite for bounties

* impl_benchmark_test_suite for Collective

* impl_benchmark_test_suite for Contracts

* impl_benchmark_test_suite for Democracy

* don't impl_benchmark_test_suite for Elections-Phragmen

* impl_benchmark_test_suite for Identity

Note that Identity tests currently fail. They failed in an identical
way before this change, so as far as I'm concerned, the status quo is
good enough for now.

* impl_benchmark_test_suite for ImOnline

* impl_benchmark_test_suite for indices

For this crate also, the test suite fails identically with and without
this change, so we can say that this change is not the cause of the
tests' failure to compile.

* impl_benchmark_test_suite for lottery

* impl_benchmark_test_suite for merkle-mountain-range

* impl_benchmark_test_suite for Multisig

These tests fail identically with and without the change, so the change
seems unlikely to be the origin of the failures.

* impl_benchmark_test_suite for offences

* impl_benchmark_test_suite for Proxy

Fails identically with and without this change.

* impl_benchmark_test_suite for scheduler

* impl_benchmark_test_suite for session

It turns out to be important to be able to exclude items marked
`#[extra]` sometimes. Who knew?

* impl_benchmark_test_suite for staking

* impl_benchmark_test_suite for system

* impl_benchmark_test_suite for timestamp

* impl_benchmark_test_suite for tips

* impl_benchmark_test_suite for treasury

* impl_benchmark_test_suite for utility

Note that benchmark tests fail identically before and after this change.

* impl_benchmark_test_suite for vesting

* fix wrong module name in impl_benchmark_test_suite in Offences

* address line length nits

* enable optional keyword argument: exec_name

Took a _lot_ of macro-wrangling to get the functionality that I want,
but now you have the option to pass in

```rust
impl_benchmark_test_suite!(
	Elections,
	crate::tests::ExtBuilder::default().desired_members(13).desired_runners_up(7),
	crate::tests::Test,
	exec_name = build_and_execute,
);
```

and have it expand out properly. A selected fragment of the expansion:

```rust
        fn test_benchmarks() {
            crate::tests::ExtBuilder::default()
                .desired_members(13)
                .desired_runners_up(7)
                .build_and_execute(|| {
```

* get rid of dead code

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
2021-02-16 10:01:20 +01:00

259 lines
9.0 KiB
Rust

// This file is part of Substrate.
// Copyright (C) 2019-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Benchmarks for Proxy Pallet
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_system::{RawOrigin, EventRecord};
use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite};
use sp_runtime::traits::Bounded;
use crate::Module as Proxy;
const SEED: u32 = 0;
fn assert_last_event<T: Config>(generic_event: <T as Config>::Event) {
let events = frame_system::Module::<T>::events();
let system_event: <T as frame_system::Config>::Event = generic_event.into();
// compare to the last event record
let EventRecord { event, .. } = &events[events.len() - 1];
assert_eq!(event, &system_event);
}
fn add_proxies<T: Config>(n: u32, maybe_who: Option<T::AccountId>) -> Result<(), &'static str> {
let caller = maybe_who.unwrap_or_else(|| whitelisted_caller());
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
for i in 0..n {
Proxy::<T>::add_proxy(
RawOrigin::Signed(caller.clone()).into(),
account("target", i, SEED),
T::ProxyType::default(),
T::BlockNumber::zero(),
)?;
}
Ok(())
}
fn add_announcements<T: Config>(
n: u32,
maybe_who: Option<T::AccountId>,
maybe_real: Option<T::AccountId>
) -> Result<(), &'static str> {
let caller = maybe_who.unwrap_or_else(|| account("caller", 0, SEED));
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
let real = if let Some(real) = maybe_real {
real
} else {
let real = account("real", 0, SEED);
T::Currency::make_free_balance_be(&real, BalanceOf::<T>::max_value());
Proxy::<T>::add_proxy(
RawOrigin::Signed(real.clone()).into(),
caller.clone(),
T::ProxyType::default(),
T::BlockNumber::zero(),
)?;
real
};
for _ in 0..n {
Proxy::<T>::announce(
RawOrigin::Signed(caller.clone()).into(),
real.clone(),
T::CallHasher::hash_of(&("add_announcement", n)),
)?;
}
Ok(())
}
benchmarks! {
proxy {
let p in 1 .. (T::MaxProxies::get() - 1).into() => add_proxies::<T>(p, None)?;
// In this case the caller is the "target" proxy
let caller: T::AccountId = account("target", p - 1, SEED);
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
// ... and "real" is the traditional caller. This is not a typo.
let real: T::AccountId = whitelisted_caller();
let call: <T as Config>::Call = frame_system::Call::<T>::remark(vec![]).into();
}: _(RawOrigin::Signed(caller), real, Some(T::ProxyType::default()), Box::new(call))
verify {
assert_last_event::<T>(RawEvent::ProxyExecuted(Ok(())).into())
}
proxy_announced {
let a in 0 .. T::MaxPending::get() - 1;
let p in 1 .. (T::MaxProxies::get() - 1).into() => add_proxies::<T>(p, None)?;
// In this case the caller is the "target" proxy
let caller: T::AccountId = account("anonymous", 0, SEED);
let delegate: T::AccountId = account("target", p - 1, SEED);
T::Currency::make_free_balance_be(&delegate, BalanceOf::<T>::max_value());
// ... and "real" is the traditional caller. This is not a typo.
let real: T::AccountId = whitelisted_caller();
let call: <T as Config>::Call = frame_system::Call::<T>::remark(vec![]).into();
Proxy::<T>::announce(
RawOrigin::Signed(delegate.clone()).into(),
real.clone(),
T::CallHasher::hash_of(&call),
)?;
add_announcements::<T>(a, Some(delegate.clone()), None)?;
}: _(RawOrigin::Signed(caller), delegate, real, Some(T::ProxyType::default()), Box::new(call))
verify {
assert_last_event::<T>(RawEvent::ProxyExecuted(Ok(())).into())
}
remove_announcement {
let a in 0 .. T::MaxPending::get() - 1;
let p in 1 .. (T::MaxProxies::get() - 1).into() => add_proxies::<T>(p, None)?;
// In this case the caller is the "target" proxy
let caller: T::AccountId = account("target", p - 1, SEED);
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
// ... and "real" is the traditional caller. This is not a typo.
let real: T::AccountId = whitelisted_caller();
let call: <T as Config>::Call = frame_system::Call::<T>::remark(vec![]).into();
Proxy::<T>::announce(
RawOrigin::Signed(caller.clone()).into(),
real.clone(),
T::CallHasher::hash_of(&call),
)?;
add_announcements::<T>(a, Some(caller.clone()), None)?;
}: _(RawOrigin::Signed(caller.clone()), real, T::CallHasher::hash_of(&call))
verify {
let (announcements, _) = Announcements::<T>::get(&caller);
assert_eq!(announcements.len() as u32, a);
}
reject_announcement {
let a in 0 .. T::MaxPending::get() - 1;
let p in 1 .. (T::MaxProxies::get() - 1).into() => add_proxies::<T>(p, None)?;
// In this case the caller is the "target" proxy
let caller: T::AccountId = account("target", p - 1, SEED);
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
// ... and "real" is the traditional caller. This is not a typo.
let real: T::AccountId = whitelisted_caller();
let call: <T as Config>::Call = frame_system::Call::<T>::remark(vec![]).into();
Proxy::<T>::announce(
RawOrigin::Signed(caller.clone()).into(),
real.clone(),
T::CallHasher::hash_of(&call),
)?;
add_announcements::<T>(a, Some(caller.clone()), None)?;
}: _(RawOrigin::Signed(real), caller.clone(), T::CallHasher::hash_of(&call))
verify {
let (announcements, _) = Announcements::<T>::get(&caller);
assert_eq!(announcements.len() as u32, a);
}
announce {
let a in 0 .. T::MaxPending::get() - 1;
let p in 1 .. (T::MaxProxies::get() - 1).into() => add_proxies::<T>(p, None)?;
// In this case the caller is the "target" proxy
let caller: T::AccountId = account("target", p - 1, SEED);
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
// ... and "real" is the traditional caller. This is not a typo.
let real: T::AccountId = whitelisted_caller();
add_announcements::<T>(a, Some(caller.clone()), None)?;
let call: <T as Config>::Call = frame_system::Call::<T>::remark(vec![]).into();
let call_hash = T::CallHasher::hash_of(&call);
}: _(RawOrigin::Signed(caller.clone()), real.clone(), call_hash)
verify {
assert_last_event::<T>(RawEvent::Announced(real, caller, call_hash).into());
}
add_proxy {
let p in 1 .. (T::MaxProxies::get() - 1).into() => add_proxies::<T>(p, None)?;
let caller: T::AccountId = whitelisted_caller();
}: _(
RawOrigin::Signed(caller.clone()),
account("target", T::MaxProxies::get().into(), SEED),
T::ProxyType::default(),
T::BlockNumber::zero()
)
verify {
let (proxies, _) = Proxies::<T>::get(caller);
assert_eq!(proxies.len() as u32, p + 1);
}
remove_proxy {
let p in 1 .. (T::MaxProxies::get() - 1).into() => add_proxies::<T>(p, None)?;
let caller: T::AccountId = whitelisted_caller();
}: _(
RawOrigin::Signed(caller.clone()),
account("target", 0, SEED),
T::ProxyType::default(),
T::BlockNumber::zero()
)
verify {
let (proxies, _) = Proxies::<T>::get(caller);
assert_eq!(proxies.len() as u32, p - 1);
}
remove_proxies {
let p in 1 .. (T::MaxProxies::get() - 1).into() => add_proxies::<T>(p, None)?;
let caller: T::AccountId = whitelisted_caller();
}: _(RawOrigin::Signed(caller.clone()))
verify {
let (proxies, _) = Proxies::<T>::get(caller);
assert_eq!(proxies.len() as u32, 0);
}
anonymous {
let p in 1 .. (T::MaxProxies::get() - 1).into() => add_proxies::<T>(p, None)?;
let caller: T::AccountId = whitelisted_caller();
}: _(
RawOrigin::Signed(caller.clone()),
T::ProxyType::default(),
T::BlockNumber::zero(),
0
)
verify {
let anon_account = Module::<T>::anonymous_account(&caller, &T::ProxyType::default(), 0, None);
assert_last_event::<T>(RawEvent::AnonymousCreated(
anon_account,
caller,
T::ProxyType::default(),
0,
).into());
}
kill_anonymous {
let p in 0 .. (T::MaxProxies::get() - 2).into();
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
Module::<T>::anonymous(
RawOrigin::Signed(whitelisted_caller()).into(),
T::ProxyType::default(),
T::BlockNumber::zero(),
0
)?;
let height = system::Module::<T>::block_number();
let ext_index = system::Module::<T>::extrinsic_index().unwrap_or(0);
let anon = Module::<T>::anonymous_account(&caller, &T::ProxyType::default(), 0, None);
add_proxies::<T>(p, Some(anon.clone()))?;
ensure!(Proxies::<T>::contains_key(&anon), "anon proxy not created");
}: _(RawOrigin::Signed(anon.clone()), caller.clone(), T::ProxyType::default(), 0, height, ext_index)
verify {
assert!(!Proxies::<T>::contains_key(&anon));
}
}
impl_benchmark_test_suite!(
Proxy,
crate::tests::new_test_ext(),
crate::tests::Test,
);