Skip to content

Commit

Permalink
Migrate pallet-utility to benchmark v2 (#6276)
Browse files Browse the repository at this point in the history
Part of:

- #6202.

---------

Co-authored-by: Dónal Murray <[email protected]>
  • Loading branch information
AurevoirXavier and seadanda authored Oct 30, 2024
1 parent 4e24733 commit 06debd0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 48 deletions.
101 changes: 55 additions & 46 deletions substrate/frame/utility/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,73 +19,82 @@

#![cfg(feature = "runtime-benchmarks")]

use super::*;
use alloc::{vec, vec::Vec};
use frame_benchmarking::v1::{account, benchmarks, whitelisted_caller};
use alloc::vec;
use frame_benchmarking::{benchmarking::add_to_whitelist, v2::*};
use frame_system::RawOrigin;

use crate::*;

const SEED: u32 = 0;

fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
frame_system::Pallet::<T>::assert_last_event(generic_event.into());
}

benchmarks! {
where_clause { where <T::RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin: Clone }
batch {
let c in 0 .. 1000;
let mut calls: Vec<<T as Config>::RuntimeCall> = Vec::new();
for i in 0 .. c {
let call = frame_system::Call::remark { remark: vec![] }.into();
calls.push(call);
}
#[benchmarks]
mod benchmark {
use super::*;

#[benchmark]
fn batch(c: Linear<0, 1000>) {
let calls = vec![frame_system::Call::remark { remark: vec![] }.into(); c as usize];
let caller = whitelisted_caller();
}: _(RawOrigin::Signed(caller), calls)
verify {
assert_last_event::<T>(Event::BatchCompleted.into())

#[extrinsic_call]
_(RawOrigin::Signed(caller), calls);

assert_last_event::<T>(Event::BatchCompleted.into());
}

as_derivative {
#[benchmark]
fn as_derivative() {
let caller = account("caller", SEED, SEED);
let call = Box::new(frame_system::Call::remark { remark: vec![] }.into());
// Whitelist caller account from further DB operations.
let caller_key = frame_system::Account::<T>::hashed_key_for(&caller);
frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into());
}: _(RawOrigin::Signed(caller), SEED as u16, call)

batch_all {
let c in 0 .. 1000;
let mut calls: Vec<<T as Config>::RuntimeCall> = Vec::new();
for i in 0 .. c {
let call = frame_system::Call::remark { remark: vec![] }.into();
calls.push(call);
}
add_to_whitelist(caller_key.into());

#[extrinsic_call]
_(RawOrigin::Signed(caller), SEED as u16, call);
}

#[benchmark]
fn batch_all(c: Linear<0, 1000>) {
let calls = vec![frame_system::Call::remark { remark: vec![] }.into(); c as usize];
let caller = whitelisted_caller();
}: _(RawOrigin::Signed(caller), calls)
verify {
assert_last_event::<T>(Event::BatchCompleted.into())

#[extrinsic_call]
_(RawOrigin::Signed(caller), calls);

assert_last_event::<T>(Event::BatchCompleted.into());
}

dispatch_as {
#[benchmark]
fn dispatch_as() {
let caller = account("caller", SEED, SEED);
let call = Box::new(frame_system::Call::remark { remark: vec![] }.into());
let origin: T::RuntimeOrigin = RawOrigin::Signed(caller).into();
let pallets_origin: <T::RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin = origin.caller().clone();
let pallets_origin = Into::<T::PalletsOrigin>::into(pallets_origin);
}: _(RawOrigin::Root, Box::new(pallets_origin), call)

force_batch {
let c in 0 .. 1000;
let mut calls: Vec<<T as Config>::RuntimeCall> = Vec::new();
for i in 0 .. c {
let call = frame_system::Call::remark { remark: vec![] }.into();
calls.push(call);
}
let origin = T::RuntimeOrigin::from(RawOrigin::Signed(caller));
let pallets_origin = origin.caller().clone();
let pallets_origin = T::PalletsOrigin::from(pallets_origin);

#[extrinsic_call]
_(RawOrigin::Root, Box::new(pallets_origin), call);
}

#[benchmark]
fn force_batch(c: Linear<0, 1000>) {
let calls = vec![frame_system::Call::remark { remark: vec![] }.into(); c as usize];
let caller = whitelisted_caller();
}: _(RawOrigin::Signed(caller), calls)
verify {
assert_last_event::<T>(Event::BatchCompleted.into())

#[extrinsic_call]
_(RawOrigin::Signed(caller), calls);

assert_last_event::<T>(Event::BatchCompleted.into());
}

impl_benchmark_test_suite!(Pallet, crate::tests::new_test_ext(), crate::tests::Test);
impl_benchmark_test_suite! {
Pallet,
tests::new_test_ext(),
tests::Test
}
}
4 changes: 2 additions & 2 deletions substrate/frame/vesting/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ mod benchmarks {

impl_benchmark_test_suite! {
Pallet,
crate::mock::ExtBuilder::default().existential_deposit(256).build(),
crate::mock::Test
mock::ExtBuilder::default().existential_deposit(256).build(),
mock::Test
}
}

0 comments on commit 06debd0

Please sign in to comment.