Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate pallet-utility to benchmark v2 #6276

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 54 additions & 45 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};
AurevoirXavier marked this conversation as resolved.
Show resolved Hide resolved
use frame_benchmarking::v1::{account, benchmarks, whitelisted_caller};
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
}
}
Loading