Skip to content

Commit

Permalink
Migrated benchmarking of system-staking from v1 to v2.
Browse files Browse the repository at this point in the history
  • Loading branch information
MJLNSN committed Dec 18, 2024
1 parent 31d643d commit e9b6073
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 56 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(1000000000000000u128)));
assert_ok!(T::VtokenMintingInterface::mint(caller, KSM, BalanceOf::<T>::unique_saturated_from(10000000000u128), 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 @@ -480,3 +480,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()
}
30 changes: 15 additions & 15 deletions pallets/system-staking/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//! Autogenerated weights for bifrost_system_staking
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 42.0.1
//! DATE: 2024-12-17, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! 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
Expand Down Expand Up @@ -74,8 +74,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `445`
// Estimated: `6385`
// Minimum execution time: 8_533_000 picoseconds.
Weight::from_parts(8_844_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)
Expand All @@ -86,8 +86,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `109`
// Estimated: `3574`
// Minimum execution time: 9_376_000 picoseconds.
Weight::from_parts(9_899_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))
}
Expand All @@ -99,8 +99,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `370`
// Estimated: `3835`
// Minimum execution time: 13_277_000 picoseconds.
Weight::from_parts(13_964_000, 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))
}
Expand All @@ -116,23 +116,23 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `550`
// Estimated: `4015`
// Minimum execution time: 16_532_000 picoseconds.
Weight::from_parts(16_881_000, 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: 211_000 picoseconds.
Weight::from_parts(248_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: 221_000 picoseconds.
Weight::from_parts(250_000, 0)
// Minimum execution time: 234_000 picoseconds.
Weight::from_parts(282_000, 0)
}
/// Storage: `SystemStaking::TokenList` (r:1 w:1)
/// Proof: `SystemStaking::TokenList` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
Expand All @@ -142,8 +142,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `169`
// Estimated: `1654`
// Minimum execution time: 5_462_000 picoseconds.
Weight::from_parts(5_574_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 e9b6073

Please sign in to comment.