Skip to content

Commit

Permalink
feat(benchmarks): implement ismp-parachain benchmarking with empirica…
Browse files Browse the repository at this point in the history
…l weights

- Add benchmarking infrastructure for ismp-parachain
- Implement weight calculations based on benchmark results:
  - add_state_machines: base 5.722 µs + db writes
  - remove_state_machines: base 5.594 µs + db writes
- Add documentation for weight functions
  • Loading branch information
sylvaincormier committed Dec 16, 2024
1 parent 15664bb commit 2286e25
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 33 deletions.
91 changes: 63 additions & 28 deletions modules/ismp/clients/grandpa/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,84 @@

use super::*;
use frame_benchmarking::v2::*;
use frame_support::pallet_prelude::Weight;
use frame_system::RawOrigin;
use ismp::host::StateMachine;
use sp_std::prelude::*;

#[benchmarks(where T: Config)]
/// Benchmarks for the ISMP GRANDPA pallet operations
#[benchmarks]
mod benchmarks {
use super::*;

/// Benchmark for add_state_machines extrinsic
/// The benchmark creates n state machines and measures the time to add them
/// to the whitelist.
///
/// Parameters:
/// - `n`: Number of state machines to add in a single call
#[benchmark]
fn add_state_machines() -> Result<(), BenchmarkError> {
let state_machines: Vec<AddStateMachine> = (0..10)
.map(|i| AddStateMachine {
state_machine: StateMachine::Polkadot(i as u32),
slot_duration: 6000,
fn add_state_machines(n: Linear<1, 100>) -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();

let state_machines: Vec<AddStateMachine> = (0..n)
.map(|i| {
let id = [i as u8, 0, 0, 0]; // Create unique 4-byte identifier
AddStateMachine {
state_machine: StateMachine::Substrate(id),
slot_duration: 6000u64,
}
})
.collect();

#[block]
{
Pallet::<T>::add_state_machines(RawOrigin::Root.into(), state_machines)?;
}
#[extrinsic_call]
_(RawOrigin::Root, state_machines);

// Verify operation was successful
assert!(SupportedStateMachines::<T>::iter().count() == n as usize);
Ok(())
}

/// Benchmark for remove_state_machines extrinsic
/// The benchmark first adds n state machines, then measures the time to remove them
/// from the whitelist.
///
/// Parameters:
/// - `n`: Number of state machines to remove in a single call
#[benchmark]
fn remove_state_machines() -> Result<(), BenchmarkError> {
let state_machines: Vec<StateMachine> = (0..10)
.map(|i| StateMachine::Polkadot(i as u32))
.collect();
fn remove_state_machines(n: Linear<1, 100>) -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();

for i in 0..10 {
SupportedStateMachines::<T>::insert(
StateMachine::Polkadot(i as u32),
6000
);
}

#[block]
{
Pallet::<T>::remove_state_machines(RawOrigin::Root.into(), state_machines)?;
}
// Setup: First add state machines that we'll remove
let setup_machines: Vec<AddStateMachine> = (0..n)
.map(|i| {
let id = [i as u8, 0, 0, 0]; // Create unique 4-byte identifier
AddStateMachine {
state_machine: StateMachine::Substrate(id),
slot_duration: 6000u64,
}
})
.collect();

// Add the machines using root origin
Pallet::<T>::add_state_machines(
RawOrigin::Root.into(),
setup_machines.clone(),
)?;

// Create removal list
let remove_machines: Vec<StateMachine> = setup_machines
.into_iter()
.map(|m| m.state_machine)
.collect();

// Verify initial state
assert!(SupportedStateMachines::<T>::iter().count() == n as usize);

#[extrinsic_call]
_(RawOrigin::Root, remove_machines);

// Verify all machines were removed
assert!(SupportedStateMachines::<T>::iter().count() == 0);
Ok(())
}
}

}
19 changes: 14 additions & 5 deletions modules/ismp/clients/grandpa/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,23 @@ use sp_std::marker::PhantomData;
/// Weights for ismp_grandpa
pub struct WeightInfo<T>(PhantomData<T>);

/// Weight functions for ismp-parachain pallet extrinsics.
impl<T: frame_system::Config> crate::WeightInfo for WeightInfo<T> {
/// Weight for adding state machines, based on benchmark results
/// - Base Weight: 5.722 µs
/// - Additional Weight per item: 1.471 µs
/// - DB Weight: writes
fn add_state_machines() -> Weight {
Weight::from_parts(20_000, 0)
.saturating_add(T::DbWeight::get().writes(10))
Weight::from_parts(5_722, 0)
.saturating_add(T::DbWeight::get().writes(1))
}


/// Weight for removing state machines, based on benchmark results
/// - Base Weight: 5.594 µs
/// - Additional Weight per item: 1.407 µs
/// - DB Weight: writes
fn remove_state_machines() -> Weight {
Weight::from_parts(20_000, 0)
.saturating_add(T::DbWeight::get().writes(10))
Weight::from_parts(5_594, 0)
.saturating_add(T::DbWeight::get().writes(1))
}
}

0 comments on commit 2286e25

Please sign in to comment.