Skip to content

Commit

Permalink
Chores/update capacity benchmarks #1949 (#1966)
Browse files Browse the repository at this point in the history
# Goal
The goal of this PR is to update the benchmark for `on_initialize` to include the weight when a new RewardEra must be created.

Closes #1949
  • Loading branch information
shannonwells committed May 13, 2024
1 parent 7aa2442 commit f8ada08
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 29 deletions.
29 changes: 26 additions & 3 deletions pallets/capacity/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,39 @@ benchmarks! {
assert_last_event::<T>(Event::<T>::StakeWithdrawn {account: caller, amount: total.into() }.into());
}

on_initialize {
start_new_epoch_if_needed {
let current_block: BlockNumberFor<T> = 100_000u32.into();
let current_epoch: T::EpochNumber = 10_000u32.into();
set_up_epoch::<T>(current_block, current_epoch);
}: {
Capacity::<T>::on_initialize(current_block);
}: {
Capacity::<T>::start_new_epoch_if_needed(current_block)
} verify {
assert_eq!(current_epoch.saturating_add(1u32.into()), Capacity::<T>::get_current_epoch());
assert_eq!(current_block, CurrentEpochInfo::<T>::get().epoch_start);
}

start_new_reward_era_if_needed {
let current_block: BlockNumberFor<T> = 1_209_600u32.into();
let history_limit: u32 = <T as Config>::StakingRewardsPastErasMax::get();
let total_reward_pool: BalanceOf<T> = <T as Config>::RewardPoolEachEra::get();
let unclaimed_balance: BalanceOf<T> = 5_000u32.into();
let total_staked_token: BalanceOf<T> = 5_000u32.into();
let started_at: BlockNumberFor<T> = current_block.saturating_sub(<T as Config>::EraLength::get().into());

let current_era: T::RewardEra = (history_limit + 1u32).into();
CurrentEraInfo::<T>::set(RewardEraInfo{ era_index: current_era, started_at });

for i in 0..history_limit {
let era: T::RewardEra = i.into();
StakingRewardPool::<T>::insert(era, RewardPoolInfo { total_staked_token, total_reward_pool, unclaimed_balance});
}
}: {
Capacity::<T>::start_new_reward_era_if_needed(current_block);
} verify {
let new_era_info = Capacity::<T>::get_current_era();
assert_eq!(current_era.saturating_add(1u32.into()), new_era_info.era_index);
assert_eq!(current_block, new_era_info.started_at);
}
unstake {
let caller: T::AccountId = create_funded_account::<T>("account", SEED, 5u32);
let staking_amount: BalanceOf<T> = T::MinimumStakingAmount::get().saturating_add(20u32.into());
Expand Down
10 changes: 3 additions & 7 deletions pallets/capacity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,9 +952,7 @@ impl<T: Config> Pallet<T> {
let current_epoch = Self::get_current_epoch();
CurrentEpoch::<T>::set(current_epoch.saturating_add(One::one()));
CurrentEpochInfo::<T>::set(EpochInfo { epoch_start: current_block });
T::WeightInfo::on_initialize()
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(2))
T::WeightInfo::start_new_epoch_if_needed()
} else {
// 1 for get_current_epoch_info, 1 for get_epoch_length
T::DbWeight::get().reads(2).saturating_add(RocksDbWeight::get().writes(1))
Expand All @@ -966,6 +964,7 @@ impl<T: Config> Pallet<T> {
Self::get_current_era(); // 1r

if current_block.saturating_sub(current_era_info.started_at) >= T::EraLength::get().into() {
// 1r
let new_era_info = RewardEraInfo {
era_index: current_era_info.era_index.saturating_add(One::one()),
started_at: current_block,
Expand All @@ -992,10 +991,7 @@ impl<T: Config> Pallet<T> {
unclaimed_balance: total_reward_pool,
};
StakingRewardPool::<T>::insert(new_era_info.era_index, new_reward_pool); // 1w

T::WeightInfo::on_initialize()
.saturating_add(T::DbWeight::get().reads(3))
.saturating_add(T::DbWeight::get().writes(3))
T::WeightInfo::start_new_reward_era_if_needed()
} else {
T::DbWeight::get().reads(1)
}
Expand Down
65 changes: 46 additions & 19 deletions pallets/capacity/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
//! Autogenerated weights for pallet_capacity
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2024-05-08, STEPS: `20`, REPEAT: `10`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2024-05-10, STEPS: `20`, REPEAT: `10`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `UL-Mac`, CPU: `<UNKNOWN>`
//! HOSTNAME: `UL-Mac.local`, CPU: `<UNKNOWN>`
//! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("frequency-bench"), DB CACHE: 1024

// Executed Command:
Expand Down Expand Up @@ -51,7 +51,8 @@ use core::marker::PhantomData;
pub trait WeightInfo {
fn stake() -> Weight;
fn withdraw_unstaked() -> Weight;
fn on_initialize() -> Weight;
fn start_new_epoch_if_needed() -> Weight;
fn start_new_reward_era_if_needed() -> Weight;
fn unstake() -> Weight;
fn set_epoch_length() -> Weight;
fn change_staking_target() -> Weight;
Expand Down Expand Up @@ -79,7 +80,7 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `174`
// Estimated: `6249`
// Minimum execution time: 35_000_000 picoseconds.
// Minimum execution time: 36_000_000 picoseconds.
Weight::from_parts(37_000_000, 6249)
.saturating_add(T::DbWeight::get().reads(7_u64))
.saturating_add(T::DbWeight::get().writes(4_u64))
Expand All @@ -97,23 +98,36 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Measured: `226`
// Estimated: `6249`
// Minimum execution time: 23_000_000 picoseconds.
Weight::from_parts(23_000_000, 6249)
Weight::from_parts(24_000_000, 6249)
.saturating_add(T::DbWeight::get().reads(4_u64))
.saturating_add(T::DbWeight::get().writes(2_u64))
}
/// Storage: `Capacity::CurrentEpochInfo` (r:1 w:1)
/// Proof: `Capacity::CurrentEpochInfo` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
/// Storage: `Capacity::EpochLength` (r:1 w:0)
/// Proof: `Capacity::EpochLength` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
fn on_initialize() -> Weight {
fn start_new_epoch_if_needed() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `2974`
// Minimum execution time: 4_000_000 picoseconds.
// Minimum execution time: 3_000_000 picoseconds.
Weight::from_parts(4_000_000, 2974)
.saturating_add(T::DbWeight::get().reads(2_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
/// Storage: `Capacity::StakingRewardPool` (r:3 w:2)
/// Proof: `Capacity::StakingRewardPool` (`max_values`: None, `max_size`: Some(60), added: 2535, mode: `MaxEncodedLen`)
/// Storage: `Capacity::CounterForStakingRewardPool` (r:1 w:1)
/// Proof: `Capacity::CounterForStakingRewardPool` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
fn start_new_reward_era_if_needed() -> Weight {
// Proof Size summary in bytes:
// Measured: `638`
// Estimated: `10080`
// Minimum execution time: 14_000_000 picoseconds.
Weight::from_parts(15_000_000, 10080)
.saturating_add(T::DbWeight::get().reads(4_u64))
.saturating_add(T::DbWeight::get().writes(3_u64))
}
/// Storage: `Capacity::StakingAccountLedger` (r:1 w:1)
/// Proof: `Capacity::StakingAccountLedger` (`max_values`: None, `max_size`: Some(57), added: 2532, mode: `MaxEncodedLen`)
/// Storage: `Capacity::StakingRewardPool` (r:1 w:1)
Expand All @@ -128,8 +142,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `343`
// Estimated: `5071`
// Minimum execution time: 28_000_000 picoseconds.
Weight::from_parts(29_000_000, 5071)
// Minimum execution time: 29_000_000 picoseconds.
Weight::from_parts(30_000_000, 5071)
.saturating_add(T::DbWeight::get().reads(5_u64))
.saturating_add(T::DbWeight::get().writes(5_u64))
}
Expand Down Expand Up @@ -157,7 +171,7 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `315`
// Estimated: `7601`
// Minimum execution time: 29_000_000 picoseconds.
// Minimum execution time: 30_000_000 picoseconds.
Weight::from_parts(31_000_000, 7601)
.saturating_add(T::DbWeight::get().reads(7_u64))
.saturating_add(T::DbWeight::get().writes(5_u64))
Expand Down Expand Up @@ -185,7 +199,7 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Measured: `247`
// Estimated: `6249`
// Minimum execution time: 45_000_000 picoseconds.
Weight::from_parts(48_000_000, 6249)
Weight::from_parts(46_000_000, 6249)
.saturating_add(T::DbWeight::get().reads(9_u64))
.saturating_add(T::DbWeight::get().writes(6_u64))
}
Expand All @@ -211,7 +225,7 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `174`
// Estimated: `6249`
// Minimum execution time: 35_000_000 picoseconds.
// Minimum execution time: 36_000_000 picoseconds.
Weight::from_parts(37_000_000, 6249)
.saturating_add(RocksDbWeight::get().reads(7_u64))
.saturating_add(RocksDbWeight::get().writes(4_u64))
Expand All @@ -229,23 +243,36 @@ impl WeightInfo for () {
// Measured: `226`
// Estimated: `6249`
// Minimum execution time: 23_000_000 picoseconds.
Weight::from_parts(23_000_000, 6249)
Weight::from_parts(24_000_000, 6249)
.saturating_add(RocksDbWeight::get().reads(4_u64))
.saturating_add(RocksDbWeight::get().writes(2_u64))
}
/// Storage: `Capacity::CurrentEpochInfo` (r:1 w:1)
/// Proof: `Capacity::CurrentEpochInfo` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
/// Storage: `Capacity::EpochLength` (r:1 w:0)
/// Proof: `Capacity::EpochLength` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
fn on_initialize() -> Weight {
fn start_new_epoch_if_needed() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `2974`
// Minimum execution time: 4_000_000 picoseconds.
// Minimum execution time: 3_000_000 picoseconds.
Weight::from_parts(4_000_000, 2974)
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
/// Storage: `Capacity::StakingRewardPool` (r:3 w:2)
/// Proof: `Capacity::StakingRewardPool` (`max_values`: None, `max_size`: Some(60), added: 2535, mode: `MaxEncodedLen`)
/// Storage: `Capacity::CounterForStakingRewardPool` (r:1 w:1)
/// Proof: `Capacity::CounterForStakingRewardPool` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
fn start_new_reward_era_if_needed() -> Weight {
// Proof Size summary in bytes:
// Measured: `638`
// Estimated: `10080`
// Minimum execution time: 14_000_000 picoseconds.
Weight::from_parts(15_000_000, 10080)
.saturating_add(RocksDbWeight::get().reads(4_u64))
.saturating_add(RocksDbWeight::get().writes(3_u64))
}
/// Storage: `Capacity::StakingAccountLedger` (r:1 w:1)
/// Proof: `Capacity::StakingAccountLedger` (`max_values`: None, `max_size`: Some(57), added: 2532, mode: `MaxEncodedLen`)
/// Storage: `Capacity::StakingRewardPool` (r:1 w:1)
Expand All @@ -260,8 +287,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `343`
// Estimated: `5071`
// Minimum execution time: 28_000_000 picoseconds.
Weight::from_parts(29_000_000, 5071)
// Minimum execution time: 29_000_000 picoseconds.
Weight::from_parts(30_000_000, 5071)
.saturating_add(RocksDbWeight::get().reads(5_u64))
.saturating_add(RocksDbWeight::get().writes(5_u64))
}
Expand Down Expand Up @@ -289,7 +316,7 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `315`
// Estimated: `7601`
// Minimum execution time: 29_000_000 picoseconds.
// Minimum execution time: 30_000_000 picoseconds.
Weight::from_parts(31_000_000, 7601)
.saturating_add(RocksDbWeight::get().reads(7_u64))
.saturating_add(RocksDbWeight::get().writes(5_u64))
Expand Down Expand Up @@ -317,7 +344,7 @@ impl WeightInfo for () {
// Measured: `247`
// Estimated: `6249`
// Minimum execution time: 45_000_000 picoseconds.
Weight::from_parts(48_000_000, 6249)
Weight::from_parts(46_000_000, 6249)
.saturating_add(RocksDbWeight::get().reads(9_u64))
.saturating_add(RocksDbWeight::get().writes(6_u64))
}
Expand Down

0 comments on commit f8ada08

Please sign in to comment.