-
Notifications
You must be signed in to change notification settings - Fork 48
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
feat(ismp-grandpa): add benchmarking support #353
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#![cfg(feature = "runtime-benchmarks")] | ||
|
||
use super::*; | ||
use frame_benchmarking::v2::*; | ||
use frame_system::RawOrigin; | ||
use ismp::host::StateMachine; | ||
|
||
#[benchmarks(where T: Config)] | ||
mod benchmarks { | ||
use super::*; | ||
|
||
#[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, | ||
}) | ||
.collect(); | ||
|
||
#[block] | ||
{ | ||
Pallet::<T>::add_state_machines(RawOrigin::Root.into(), state_machines)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[benchmark] | ||
fn remove_state_machines() -> Result<(), BenchmarkError> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah this too |
||
let state_machines: Vec<StateMachine> = (0..10) | ||
.map(|i| StateMachine::Polkadot(i as u32)) | ||
.collect(); | ||
|
||
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)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,23 @@ | |
#![cfg_attr(not(feature = "std"), no_std)] | ||
extern crate alloc; | ||
|
||
|
||
#[cfg(feature = "runtime-benchmarks")] | ||
pub mod benchmarking; | ||
|
||
pub mod consensus; | ||
pub mod messages; | ||
|
||
use alloc::vec::Vec; | ||
use ismp::host::StateMachine; | ||
pub use pallet::*; | ||
use frame_support::pallet_prelude::Weight; | ||
pub trait WeightInfo { | ||
fn add_state_machines() -> Weight; | ||
fn remove_state_machines() -> Weight; | ||
Comment on lines
+30
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should take the length of state machines |
||
} | ||
|
||
pub mod weights; | ||
#[frame_support::pallet] | ||
pub mod pallet { | ||
use super::*; | ||
|
@@ -41,6 +51,7 @@ pub mod pallet { | |
|
||
/// IsmpHost implementation | ||
type IsmpHost: IsmpHost + Default; | ||
type WeightInfo: WeightInfo; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some doc comments will be nice |
||
} | ||
|
||
/// Events emitted by this pallet | ||
|
@@ -89,7 +100,7 @@ pub mod pallet { | |
|
||
/// Remove a state machine from the list of supported state machines | ||
#[pallet::call_index(1)] | ||
#[pallet::weight(T::DbWeight::get().writes(state_machines.len() as u64))] | ||
#[pallet::weight(T::WeightInfo::remove_state_machines())] | ||
pub fn remove_state_machines( | ||
origin: OriginFor<T>, | ||
state_machines: Vec<StateMachine>, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// 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>); | ||
|
||
impl<T: frame_system::Config> crate::WeightInfo for WeightInfo<T> { | ||
fn add_state_machines() -> Weight { | ||
Weight::from_parts(20_000, 0) | ||
.saturating_add(T::DbWeight::get().writes(10)) | ||
} | ||
|
||
fn remove_state_machines() -> Weight { | ||
Weight::from_parts(20_000, 0) | ||
.saturating_add(T::DbWeight::get().writes(10)) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't this benchmark be dynamic? so that the weight is calculated based on the number of items to be added?