-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ismp-grandpa): add benchmarking support (#353)
- Loading branch information
1 parent
46da0fb
commit 0382d9f
Showing
10 changed files
with
188 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#![cfg(feature = "runtime-benchmarks")] | ||
use super::*; | ||
use frame_benchmarking::v2::*; | ||
use frame_support::pallet_prelude::Weight; | ||
use frame_system::RawOrigin; | ||
use sp_std::prelude::*; | ||
|
||
/// 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(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(); | ||
|
||
#[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(n: Linear<1, 100>) -> Result<(), BenchmarkError> { | ||
let caller: T::AccountId = whitelisted_caller(); | ||
|
||
// 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(()) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// This file is part of Hyperbridge. | ||
|
||
// Copyright (C) Polytope Labs Ltd. | ||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
#![cfg_attr(rustfmt, rustfmt_skip)] | ||
#![allow(unused_parens)] | ||
#![allow(unused_imports)] | ||
|
||
use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; | ||
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, scaled by the number of machines. | ||
/// Values based on measured benchmarks: | ||
/// - Base Weight: 5.525 µs | ||
/// - Additional Weight per item: 1.458 µs | ||
/// - DB Weight: n writes | ||
fn add_state_machines(n: u32) -> Weight { | ||
Weight::from_parts(5_525, 0) | ||
.saturating_add(Weight::from_parts(1_458, 0).saturating_mul(n as u64)) | ||
.saturating_add(T::DbWeight::get().writes(n as u64)) | ||
} | ||
|
||
/// Weight for removing state machines, scaled by the number of machines. | ||
/// Values based on measured benchmarks: | ||
/// - Base Weight: 4.914 µs | ||
/// - Additional Weight per item: 1.419 µs | ||
/// - DB Weight: n writes | ||
fn remove_state_machines(n: u32) -> Weight { | ||
Weight::from_parts(4_914, 0) | ||
.saturating_add(Weight::from_parts(1_419, 0).saturating_mul(n as u64)) | ||
.saturating_add(T::DbWeight::get().writes(n as u64)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters