Skip to content

Commit

Permalink
Migrated benchmarking of system-staking from v1 to v2. (#1560)
Browse files Browse the repository at this point in the history
* fixed the benchmarking for system-staking.

* Migrated benchmarking of system-staking from v1 to v2.

* Format changed.
  • Loading branch information
MJLNSN authored Dec 18, 2024
1 parent 1b9a2e7 commit af3d472
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 89 deletions.
2 changes: 1 addition & 1 deletion pallets/system-staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ sp-arithmetic = { workspace = true }
orml-traits = { workspace = true }
sp-runtime = { workspace = true }
bifrost-primitives = { workspace = true }
bifrost-vtoken-minting = { workspace = true }

[dev-dependencies]
sp-core = { workspace = true }
Expand All @@ -34,7 +35,6 @@ orml-tokens = { workspace = true }
orml-xtokens = { workspace = true }
orml-traits = { workspace = true }
hex-literal = { workspace = true }
bifrost-vtoken-minting = { workspace = true }
bifrost-slp = { workspace = true }
bifrost-farming = { workspace = true }
bifrost-asset-registry = { workspace = true }
Expand Down
136 changes: 96 additions & 40 deletions pallets/system-staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![cfg(feature = "runtime-benchmarks")]
use crate::{Pallet as SystemStaking, *};
use bifrost_primitives::{CurrencyId, PoolId, TokenSymbol};
use frame_benchmarking::{benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use crate::{Config, Pallet as SystemStaking, *};
use bifrost_primitives::{CurrencyId, PoolId, TokenSymbol, *};
use frame_benchmarking::v2::*;
use frame_support::{
assert_ok,
sp_runtime::{traits::UniqueSaturatedFrom, Perbill, Permill},
Expand All @@ -28,10 +28,12 @@ use frame_support::{
use frame_system::{Pallet as System, RawOrigin};
use sp_std::vec;

benchmarks! {
on_initialize {
const KSM: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
const MOVR: CurrencyId = CurrencyId::Token(TokenSymbol::MOVR);
#[benchmarks(where T: Config + bifrost_vtoken_minting::Config)]
mod benchmarks {
use super::*;

#[benchmark]
fn on_initialize() -> Result<(), BenchmarkError> {
assert_ok!(SystemStaking::<T>::token_config(
RawOrigin::Root.into(),
KSM,
Expand All @@ -42,6 +44,7 @@ benchmarks! {
Some(vec![1 as PoolId]),
Some(vec![Perbill::from_percent(100)]),
));

assert_ok!(SystemStaking::<T>::token_config(
RawOrigin::Root.into(),
MOVR,
Expand All @@ -52,23 +55,41 @@ benchmarks! {
Some(vec![1 as PoolId]),
Some(vec![Perbill::from_percent(100)]),
));
System::<T>::set_block_number(
System::<T>::block_number() + 1u32.into()
);

System::<T>::set_block_number(System::<T>::block_number() + 1u32.into());
SystemStaking::<T>::on_initialize(System::<T>::block_number());
System::<T>::set_block_number(
System::<T>::block_number() + 1u32.into()
);
System::<T>::set_block_number(System::<T>::block_number() + 1u32.into());
SystemStaking::<T>::on_initialize(System::<T>::block_number());
}:{SystemStaking::<T>::on_initialize(System::<T>::block_number());}
#[block]
{
SystemStaking::<T>::on_initialize(System::<T>::block_number());
}

token_config {
Ok(())
}

#[benchmark]
fn token_config() -> Result<(), BenchmarkError> {
const KSM: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
let token_amount = BalanceOf::<T>::unique_saturated_from(1000u128);
let pool_id = PoolId::from(1u32);
}: _(RawOrigin::Root, KSM, Some(BlockNumberFor::<T>::from(1u32)), Some(Permill::from_percent(80)),Some(false),Some(token_amount),Some(vec![pool_id]),Some(vec![Perbill::from_percent(100)]))
#[extrinsic_call]
_(
RawOrigin::Root,
KSM,
Some(BlockNumberFor::<T>::from(1u32)),
Some(Permill::from_percent(80)),
Some(false),
Some(token_amount),
Some(vec![pool_id]),
Some(vec![Perbill::from_percent(100)]),
);

Ok(())
}

refresh_token_info {
#[benchmark]
fn refresh_token_info() -> Result<(), BenchmarkError> {
const KSM: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
assert_ok!(SystemStaking::<T>::token_config(
RawOrigin::Root.into(),
Expand All @@ -80,10 +101,14 @@ benchmarks! {
Some(vec![1 as PoolId]),
Some(vec![Perbill::from_percent(100)]),
));
}: _(RawOrigin::Root,KSM)
#[extrinsic_call]
_(RawOrigin::Root, KSM);

payout {
const KSM: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
Ok(())
}

#[benchmark]
fn payout() -> Result<(), BenchmarkError> {
assert_ok!(SystemStaking::<T>::token_config(
RawOrigin::Root.into(),
KSM,
Expand All @@ -94,13 +119,29 @@ benchmarks! {
Some(vec![1 as PoolId]),
Some(vec![Perbill::from_percent(100)]),
));

let caller: T::AccountId = whitelisted_caller();
assert_ok!(T::MultiCurrency::deposit(KSM, &caller, BalanceOf::<T>::unique_saturated_from(1000u128)));
assert_ok!(T::VtokenMintingInterface::mint(caller, KSM, BalanceOf::<T>::unique_saturated_from(1000u128), BoundedVec::default(),None));
}: _(RawOrigin::Root,KSM)
assert_ok!(<T as Config>::MultiCurrency::deposit(
KSM,
&caller,
BalanceOf::<T>::unique_saturated_from(1_000_000_000_000_000u128).into()
));
assert_ok!(T::VtokenMintingInterface::mint(
caller,
KSM,
BalanceOf::<T>::unique_saturated_from(10_000_000_000u128),
BoundedVec::default(),
None
));

on_redeem_success {
const KSM: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
#[extrinsic_call]
_(RawOrigin::Root, KSM);

Ok(())
}

#[benchmark]
fn on_redeem_success() -> Result<(), BenchmarkError> {
assert_ok!(SystemStaking::<T>::token_config(
RawOrigin::Root.into(),
KSM,
Expand All @@ -113,10 +154,16 @@ benchmarks! {
));
let caller: T::AccountId = whitelisted_caller();
let token_amount = BalanceOf::<T>::unique_saturated_from(1000u128);
}:{SystemStaking::<T>::on_redeem_success(KSM,caller,token_amount);}
#[block]
{
SystemStaking::<T>::on_redeem_success(KSM, caller, token_amount);
}

on_redeemed {
const KSM: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
Ok(())
}

#[benchmark]
fn on_redeemed() -> Result<(), BenchmarkError> {
assert_ok!(SystemStaking::<T>::token_config(
RawOrigin::Root.into(),
KSM,
Expand All @@ -130,10 +177,16 @@ benchmarks! {
let caller: T::AccountId = whitelisted_caller();
let token_amount = BalanceOf::<T>::unique_saturated_from(1000u128);
let fee_amount = BalanceOf::<T>::unique_saturated_from(1000u128);
}:{SystemStaking::<T>::on_redeemed(caller,KSM,token_amount,token_amount,fee_amount);}
#[block]
{
SystemStaking::<T>::on_redeemed(caller, KSM, token_amount, token_amount, fee_amount);
}

delete_token {
const KSM: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
Ok(())
}

#[benchmark]
fn delete_token() -> Result<(), BenchmarkError> {
assert_ok!(SystemStaking::<T>::token_config(
RawOrigin::Root.into(),
KSM,
Expand All @@ -144,13 +197,16 @@ benchmarks! {
Some(vec![1 as PoolId]),
Some(vec![Perbill::from_percent(100)]),
));
}: _(RawOrigin::Root,KSM)
}

impl_benchmark_test_suite!(
SystemStaking,
crate::mock::ExtBuilder::default()
.one_hundred_for_alice_n_bob()
.build(),
crate::mock::Runtime
);
#[extrinsic_call]
_(RawOrigin::Root, KSM);

Ok(())
}

impl_benchmark_test_suite!(
Pallet,
crate::mock::new_test_ext_benchmark(),
crate::mock::Runtime
);
}
5 changes: 5 additions & 0 deletions pallets/system-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,8 @@ pub(crate) fn roll_to(n: u64) -> u64 {
}
num_blocks
}

#[cfg(feature = "runtime-benchmarks")]
pub fn new_test_ext_benchmark() -> sp_io::TestExternalities {
ExtBuilder::default().build()
}
96 changes: 48 additions & 48 deletions pallets/system-staking/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

//! Autogenerated weights for bifrost_system_staking
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-09-14, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! HOSTNAME: `bifrost-jenkins`, CPU: `Intel(R) Xeon(R) CPU E5-26xx v4`
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 42.0.1
//! DATE: 2024-12-18, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! HOSTNAME: `mjl-legion`, CPU: `12th Gen Intel(R) Core(TM) i9-12900H`
//! WASM-EXECUTION: Compiled, CHAIN: Some("bifrost-kusama-local"), DB CACHE: 1024
// Executed Command:
Expand All @@ -36,7 +36,7 @@
// --chain=bifrost-kusama-local
// --steps=50
// --repeat=20
// --pallet=bifrost_system_staking
// --pallet=bifrost-system-staking
// --extrinsic=*
// --execution=wasm
// --wasm-execution=compiled
Expand Down Expand Up @@ -64,86 +64,86 @@ pub trait WeightInfo {

// For backwards compatibility and tests
impl WeightInfo for () {
/// Storage: SystemStaking TokenList (r:1 w:0)
/// Proof Skipped: SystemStaking TokenList (max_values: Some(1), max_size: None, mode: Measured)
/// Storage: SystemStaking Round (r:1 w:0)
/// Proof Skipped: SystemStaking Round (max_values: Some(1), max_size: None, mode: Measured)
/// Storage: SystemStaking TokenStatus (r:2 w:0)
/// Proof Skipped: SystemStaking TokenStatus (max_values: None, max_size: None, mode: Measured)
/// Storage: `SystemStaking::TokenList` (r:1 w:0)
/// Proof: `SystemStaking::TokenList` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `SystemStaking::Round` (r:1 w:0)
/// Proof: `SystemStaking::Round` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `SystemStaking::TokenStatus` (r:2 w:0)
/// Proof: `SystemStaking::TokenStatus` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn on_initialize() -> Weight {
// Proof Size summary in bytes:
// Measured: `445`
// Estimated: `6385`
// Minimum execution time: 30_023_000 picoseconds.
Weight::from_parts(30_834_000, 6385)
// Minimum execution time: 8_769_000 picoseconds.
Weight::from_parts(9_831_000, 6385)
.saturating_add(RocksDbWeight::get().reads(4_u64))
}
/// Storage: SystemStaking TokenStatus (r:1 w:1)
/// Proof Skipped: SystemStaking TokenStatus (max_values: None, max_size: None, mode: Measured)
/// Storage: SystemStaking TokenList (r:1 w:1)
/// Proof Skipped: SystemStaking TokenList (max_values: Some(1), max_size: None, mode: Measured)
/// Storage: `SystemStaking::TokenStatus` (r:1 w:1)
/// Proof: `SystemStaking::TokenStatus` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `SystemStaking::TokenList` (r:1 w:1)
/// Proof: `SystemStaking::TokenList` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
fn token_config() -> Weight {
// Proof Size summary in bytes:
// Measured: `109`
// Estimated: `3574`
// Minimum execution time: 44_695_000 picoseconds.
Weight::from_parts(45_801_000, 3574)
// Minimum execution time: 9_370_000 picoseconds.
Weight::from_parts(10_254_000, 3574)
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(2_u64))
}
/// Storage: SystemStaking TokenStatus (r:1 w:1)
/// Proof Skipped: SystemStaking TokenStatus (max_values: None, max_size: None, mode: Measured)
/// Storage: Farming PoolInfos (r:1 w:0)
/// Proof Skipped: Farming PoolInfos (max_values: None, max_size: None, mode: Measured)
/// Storage: `SystemStaking::TokenStatus` (r:1 w:1)
/// Proof: `SystemStaking::TokenStatus` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `Farming::PoolInfos` (r:1 w:0)
/// Proof: `Farming::PoolInfos` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn refresh_token_info() -> Weight {
// Proof Size summary in bytes:
// Measured: `403`
// Estimated: `3868`
// Minimum execution time: 56_032_000 picoseconds.
Weight::from_parts(57_981_000, 3868)
// Measured: `370`
// Estimated: `3835`
// Minimum execution time: 14_641_000 picoseconds.
Weight::from_parts(27_074_000, 3835)
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
/// Storage: SystemStaking TokenStatus (r:1 w:0)
/// Proof Skipped: SystemStaking TokenStatus (max_values: None, max_size: None, mode: Measured)
/// Storage: Tokens Accounts (r:1 w:0)
/// Proof: Tokens Accounts (max_values: None, max_size: Some(118), added: 2593, mode: MaxEncodedLen)
/// Storage: VtokenMinting TokenPool (r:1 w:0)
/// Proof: VtokenMinting TokenPool (max_values: None, max_size: Some(38), added: 2513, mode: MaxEncodedLen)
/// Storage: Tokens TotalIssuance (r:1 w:0)
/// Proof: Tokens TotalIssuance (max_values: None, max_size: Some(38), added: 2513, mode: MaxEncodedLen)
/// Storage: `SystemStaking::TokenStatus` (r:1 w:0)
/// Proof: `SystemStaking::TokenStatus` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `Tokens::Accounts` (r:1 w:0)
/// Proof: `Tokens::Accounts` (`max_values`: None, `max_size`: Some(118), added: 2593, mode: `MaxEncodedLen`)
/// Storage: `VtokenMinting::TokenPool` (r:1 w:0)
/// Proof: `VtokenMinting::TokenPool` (`max_values`: None, `max_size`: Some(38), added: 2513, mode: `MaxEncodedLen`)
/// Storage: `Tokens::TotalIssuance` (r:1 w:0)
/// Proof: `Tokens::TotalIssuance` (`max_values`: None, `max_size`: Some(38), added: 2513, mode: `MaxEncodedLen`)
fn payout() -> Weight {
// Proof Size summary in bytes:
// Measured: `1190`
// Estimated: `4655`
// Minimum execution time: 71_558_000 picoseconds.
Weight::from_parts(72_649_000, 4655)
// Measured: `550`
// Estimated: `4015`
// Minimum execution time: 16_437_000 picoseconds.
Weight::from_parts(17_241_000, 4015)
.saturating_add(RocksDbWeight::get().reads(4_u64))
}
fn on_redeem_success() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 1_672_000 picoseconds.
Weight::from_parts(2_317_000, 0)
// Minimum execution time: 201_000 picoseconds.
Weight::from_parts(243_000, 0)
}
fn on_redeemed() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 2_053_000 picoseconds.
Weight::from_parts(2_416_000, 0)
// Minimum execution time: 234_000 picoseconds.
Weight::from_parts(282_000, 0)
}
/// Storage: SystemStaking TokenList (r:1 w:1)
/// Proof Skipped: SystemStaking TokenList (max_values: Some(1), max_size: None, mode: Measured)
/// Storage: SystemStaking TokenStatus (r:0 w:1)
/// Proof Skipped: SystemStaking TokenStatus (max_values: None, max_size: None, mode: Measured)
/// Storage: `SystemStaking::TokenList` (r:1 w:1)
/// Proof: `SystemStaking::TokenList` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `SystemStaking::TokenStatus` (r:0 w:1)
/// Proof: `SystemStaking::TokenStatus` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn delete_token() -> Weight {
// Proof Size summary in bytes:
// Measured: `169`
// Estimated: `1654`
// Minimum execution time: 24_359_000 picoseconds.
Weight::from_parts(25_049_000, 1654)
// Minimum execution time: 5_654_000 picoseconds.
Weight::from_parts(5_826_000, 1654)
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().writes(2_u64))
}
Expand Down

0 comments on commit af3d472

Please sign in to comment.