Skip to content

Commit

Permalink
Migrate some pallets to benchmark v2 (#6311)
Browse files Browse the repository at this point in the history
Part of #6202

---------

Co-authored-by: Guillaume Thiolliere <[email protected]>
Co-authored-by: Giuseppe Re <[email protected]>
  • Loading branch information
3 people authored Nov 15, 2024
1 parent a77940b commit 5f270bc
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 68 deletions.
13 changes: 13 additions & 0 deletions prdoc/pr_6311.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: Migrate pallet-fast-unstake and pallet-babe benchmark to v2
doc:
- audience: Runtime Dev
description: |-
Migrate pallet-fast-unstake and pallet-babe benchmark to v2
crates:
- name: pallet-babe
bump: patch
- name: pallet-fast-unstake
bump: patch
27 changes: 14 additions & 13 deletions substrate/frame/babe/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
#![cfg(feature = "runtime-benchmarks")]

use super::*;
use frame_benchmarking::v1::benchmarks;
use frame_benchmarking::v2::*;

type Header = sp_runtime::generic::Header<u64, sp_runtime::traits::BlakeTwo256>;

benchmarks! {
check_equivocation_proof {
let x in 0 .. 1;
#[benchmarks]
mod benchmarks {
use super::*;

#[benchmark]
fn check_equivocation_proof(x: Linear<0, 1>) {
// NOTE: generated with the test below `test_generate_equivocation_report_blob`.
// the output is not deterministic since keys are generated randomly (and therefore
// signature content changes). it should not affect the benchmark.
Expand All @@ -53,22 +55,21 @@ benchmarks! {
124, 11, 167, 227, 103, 88, 78, 23, 228, 33, 96, 41, 207, 183, 227, 189, 114, 70, 254,
30, 128, 243, 233, 83, 214, 45, 74, 182, 120, 119, 64, 243, 219, 119, 63, 240, 205,
123, 231, 82, 205, 174, 143, 70, 2, 86, 182, 20, 16, 141, 145, 91, 116, 195, 58, 223,
175, 145, 255, 7, 121, 133
175, 145, 255, 7, 121, 133,
];

let equivocation_proof1: sp_consensus_babe::EquivocationProof<Header> =
Decode::decode(&mut &EQUIVOCATION_PROOF_BLOB[..]).unwrap();

let equivocation_proof2 = equivocation_proof1.clone();
}: {
sp_consensus_babe::check_equivocation_proof::<Header>(equivocation_proof1);
} verify {

#[block]
{
sp_consensus_babe::check_equivocation_proof::<Header>(equivocation_proof1);
}

assert!(sp_consensus_babe::check_equivocation_proof::<Header>(equivocation_proof2));
}

impl_benchmark_test_suite!(
Pallet,
crate::mock::new_test_ext(3),
crate::mock::Test,
)
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(3), crate::mock::Test,);
}
116 changes: 61 additions & 55 deletions substrate/frame/fast-unstake/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

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

use crate::{types::*, Pallet as FastUnstake, *};
use crate::{types::*, *};
use alloc::{vec, vec::Vec};
use frame_benchmarking::v1::{benchmarks, whitelist_account, BenchmarkError};
use frame_benchmarking::v2::*;
use frame_support::{
assert_ok,
traits::{Currency, EnsureOrigin, Get, Hooks},
Expand Down Expand Up @@ -89,22 +89,21 @@ fn setup_staking<T: Config>(v: u32, until: EraIndex) {

fn on_idle_full_block<T: Config>() {
let remaining_weight = <T as frame_system::Config>::BlockWeights::get().max_block;
FastUnstake::<T>::on_idle(Zero::zero(), remaining_weight);
Pallet::<T>::on_idle(Zero::zero(), remaining_weight);
}

benchmarks! {
#[benchmarks]
mod benchmarks {
use super::*;
// on_idle, we don't check anyone, but fully unbond them.
on_idle_unstake {
let b in 1 .. T::BatchSize::get();

#[benchmark]
fn on_idle_unstake(b: Linear<1, { T::BatchSize::get() }>) {
ErasToCheckPerBlock::<T>::put(1);
for who in create_unexposed_batch::<T>(b).into_iter() {
assert_ok!(FastUnstake::<T>::register_fast_unstake(
RawOrigin::Signed(who.clone()).into(),
));
assert_ok!(Pallet::<T>::register_fast_unstake(RawOrigin::Signed(who.clone()).into(),));
}

// run on_idle once. This will check era 0.
// Run on_idle once. This will check era 0.
assert_eq!(Head::<T>::get(), None);
on_idle_full_block::<T>();

Expand All @@ -116,21 +115,19 @@ benchmarks! {
..
}) if checked.len() == 1 && stashes.len() as u32 == b
));

#[block]
{
on_idle_full_block::<T>();
}

assert_eq!(fast_unstake_events::<T>().last(), Some(&Event::BatchFinished { size: b }));
}
: {
on_idle_full_block::<T>();
}
verify {
assert!(matches!(
fast_unstake_events::<T>().last(),
Some(Event::BatchFinished { size: b })
));
}

// on_idle, when we check some number of eras and the queue is already set.
on_idle_check {
let v in 1 .. 256;
let b in 1 .. T::BatchSize::get();
#[benchmark]
fn on_idle_check(v: Linear<1, 256>, b: Linear<1, { T::BatchSize::get() }>) {
// on_idle: When we check some number of eras and the queue is already set.

let u = T::MaxErasToCheckPerBlock::get().min(T::Staking::bonding_duration());

ErasToCheckPerBlock::<T>::put(u);
Expand All @@ -139,64 +136,73 @@ benchmarks! {
// setup staking with v validators and u eras of data (0..=u+1)
setup_staking::<T>(v, u);

let stashes = create_unexposed_batch::<T>(b).into_iter().map(|s| {
assert_ok!(FastUnstake::<T>::register_fast_unstake(
RawOrigin::Signed(s.clone()).into(),
));
(s, T::Deposit::get())
}).collect::<Vec<_>>();
let stashes = create_unexposed_batch::<T>(b)
.into_iter()
.map(|s| {
assert_ok!(
Pallet::<T>::register_fast_unstake(RawOrigin::Signed(s.clone()).into(),)
);
(s, T::Deposit::get())
})
.collect::<Vec<_>>();

// no one is queued thus far.
assert_eq!(Head::<T>::get(), None);

Head::<T>::put(UnstakeRequest { stashes: stashes.clone().try_into().unwrap(), checked: Default::default() });
}
: {
on_idle_full_block::<T>();
}
verify {
Head::<T>::put(UnstakeRequest {
stashes: stashes.clone().try_into().unwrap(),
checked: Default::default(),
});

#[block]
{
on_idle_full_block::<T>();
}

let checked = (1..=u).rev().collect::<Vec<EraIndex>>();
let request = Head::<T>::get().unwrap();
assert_eq!(checked, request.checked.into_inner());
assert!(matches!(
fast_unstake_events::<T>().last(),
Some(Event::BatchChecked { .. })
));
assert!(matches!(fast_unstake_events::<T>().last(), Some(Event::BatchChecked { .. })));
assert!(stashes.iter().all(|(s, _)| request.stashes.iter().any(|(ss, _)| ss == s)));
}

register_fast_unstake {
#[benchmark]
fn register_fast_unstake() {
ErasToCheckPerBlock::<T>::put(1);
let who = create_unexposed_batch::<T>(1).get(0).cloned().unwrap();
whitelist_account!(who);
assert_eq!(Queue::<T>::count(), 0);

}
:_(RawOrigin::Signed(who.clone()))
verify {
#[extrinsic_call]
_(RawOrigin::Signed(who.clone()));

assert_eq!(Queue::<T>::count(), 1);
}

deregister {
#[benchmark]
fn deregister() {
ErasToCheckPerBlock::<T>::put(1);
let who = create_unexposed_batch::<T>(1).get(0).cloned().unwrap();
assert_ok!(FastUnstake::<T>::register_fast_unstake(
RawOrigin::Signed(who.clone()).into(),
));
assert_ok!(Pallet::<T>::register_fast_unstake(RawOrigin::Signed(who.clone()).into(),));
assert_eq!(Queue::<T>::count(), 1);
whitelist_account!(who);
}
:_(RawOrigin::Signed(who.clone()))
verify {

#[extrinsic_call]
_(RawOrigin::Signed(who.clone()));

assert_eq!(Queue::<T>::count(), 0);
}

control {
#[benchmark]
fn control() -> Result<(), BenchmarkError> {
let origin = <T as Config>::ControlOrigin::try_successful_origin()
.map_err(|_| BenchmarkError::Weightless)?;

#[extrinsic_call]
_(origin as T::RuntimeOrigin, T::MaxErasToCheckPerBlock::get());

Ok(())
}
: _<T::RuntimeOrigin>(origin, T::MaxErasToCheckPerBlock::get())
verify {}

impl_benchmark_test_suite!(Pallet, crate::mock::ExtBuilder::default().build(), crate::mock::Runtime)
impl_benchmark_test_suite!(Pallet, mock::ExtBuilder::default().build(), mock::Runtime);
}

0 comments on commit 5f270bc

Please sign in to comment.