Skip to content

Commit

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

- #6202.

---------

Co-authored-by: GitHub Action <[email protected]>
Co-authored-by: Giuseppe Re <[email protected]>
  • Loading branch information
3 people authored Nov 5, 2024
1 parent 6f078d1 commit 8b6c6eb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 23 deletions.
10 changes: 10 additions & 0 deletions prdoc/pr_6295.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
title: Migrate pallet-im-online benchmark to v2
doc:
- audience: Runtime Dev
description: |-
Part of:

- #6202.
crates:
- name: pallet-im-online
bump: patch
65 changes: 42 additions & 23 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::*;
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;
#[benchmarks]
mod benchmarks {
use super::*;

#[benchmark(extra)]
fn heartbeat(k: Linear<1, MAX_KEYS>) -> Result<(), BenchmarkError> {
let (input_heartbeat, signature) = create_heartbeat::<T>(k)?;
}: _(RawOrigin::None, input_heartbeat, signature)

#[extra]
validate_unsigned {
let k in 1 .. MAX_KEYS;
#[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)?;
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;
#[benchmark]
fn validate_unsigned_and_then_heartbeat(k: Linear<1, MAX_KEYS>) -> Result<(), BenchmarkError> {
let (input_heartbeat, signature) = create_heartbeat::<T>(k)?;
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
}
}

0 comments on commit 8b6c6eb

Please sign in to comment.