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-im-online benchmark to v2 #6295

Merged
merged 8 commits into from
Nov 5, 2024
73 changes: 46 additions & 27 deletions substrate/frame/im-online/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@

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

use super::*;

use frame_benchmarking::v1::benchmarks;
use frame_benchmarking::{v2::*, BenchmarkError};
aurexav marked this conversation as resolved.
Show resolved Hide resolved
use frame_support::{traits::UnfilteredDispatchable, WeakBoundedVec};
use frame_system::RawOrigin;
use sp_runtime::{
traits::{ValidateUnsigned, Zero},
transaction_validity::TransactionSource,
};

use crate::Pallet as ImOnline;
use crate::*;

const MAX_KEYS: u32 = 1000;

Expand Down Expand Up @@ -64,34 +62,55 @@ pub fn create_heartbeat<T: Config>(
Ok((input_heartbeat, signature))
}

benchmarks! {
#[extra]
heartbeat {
let k in 1 .. MAX_KEYS;
let (input_heartbeat, signature) = create_heartbeat::<T>(k)?;
}: _(RawOrigin::None, input_heartbeat, signature)

#[extra]
validate_unsigned {
let k in 1 .. MAX_KEYS;
let (input_heartbeat, signature) = create_heartbeat::<T>(k)?;
#[benchmarks]
mod benchmarks {
use super::*;

#[benchmark(extra)]
fn heartbeat(k: Linear<1, MAX_KEYS>) -> Result<(), BenchmarkError> {
let (input_heartbeat, signature) = create_heartbeat::<T>(k.into())?;
aurexav marked this conversation as resolved.
Show resolved Hide resolved

#[extrinsic_call]
_(RawOrigin::None, input_heartbeat, signature);

Ok(())
}

#[benchmark(extra)]
fn validate_unsigned(k: Linear<1, MAX_KEYS>) -> Result<(), BenchmarkError> {
let (input_heartbeat, signature) = create_heartbeat::<T>(k.into())?;
let call = Call::heartbeat { heartbeat: input_heartbeat, signature };
}: {
ImOnline::<T>::validate_unsigned(TransactionSource::InBlock, &call)
.map_err(<&str>::from)?;

#[block]
{
Pallet::<T>::validate_unsigned(TransactionSource::InBlock, &call)
.map_err(<&str>::from)?;
}

Ok(())
}

validate_unsigned_and_then_heartbeat {
let k in 1 .. MAX_KEYS;
let (input_heartbeat, signature) = create_heartbeat::<T>(k)?;
#[benchmark]
fn validate_unsigned_and_then_heartbeat(k: Linear<1, MAX_KEYS>) -> Result<(), BenchmarkError> {
let (input_heartbeat, signature) = create_heartbeat::<T>(k.into())?;
re-gius marked this conversation as resolved.
Show resolved Hide resolved
let call = Call::heartbeat { heartbeat: input_heartbeat, signature };
let call_enc = call.encode();
}: {
ImOnline::<T>::validate_unsigned(TransactionSource::InBlock, &call).map_err(<&str>::from)?;
<Call<T> as Decode>::decode(&mut &*call_enc)
.expect("call is encoded above, encoding must be correct")
.dispatch_bypass_filter(RawOrigin::None.into())?;

#[block]
{
Pallet::<T>::validate_unsigned(TransactionSource::InBlock, &call)
.map_err(<&str>::from)?;
<Call<T> as Decode>::decode(&mut &*call_enc)
.expect("call is encoded above, encoding must be correct")
.dispatch_bypass_filter(RawOrigin::None.into())?;
}

Ok(())
}

impl_benchmark_test_suite!(ImOnline, crate::mock::new_test_ext(), crate::mock::Runtime);
impl_benchmark_test_suite! {
Pallet,
mock::new_test_ext(),
mock::Runtime
}
}
Loading