Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
fix bench
Browse files Browse the repository at this point in the history
  • Loading branch information
slumber committed Apr 26, 2023
1 parent 2d2fbb0 commit a50ca39
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 120 deletions.
40 changes: 15 additions & 25 deletions runtime/common/src/auctions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1731,17 +1731,16 @@ mod tests {
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking {
use super::{Pallet as Auctions, *};
use crate::mock::{conclude_pvf_checking, validators_public_keys};
use frame_support::traits::{EnsureOrigin, OnInitialize};
use frame_support::{
assert_ok,
traits::{EnsureOrigin, OnInitialize},
};
use frame_system::RawOrigin;
use runtime_parachains::{paras, shared};
use sp_keyring::Sr25519Keyring;
use runtime_parachains::paras;
use sp_runtime::{traits::Bounded, SaturatedConversion};

use frame_benchmarking::{account, benchmarks, whitelisted_caller, BenchmarkError};

const VALIDATORS: &[Sr25519Keyring] = &[Sr25519Keyring::Alice];

fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
let events = frame_system::Pallet::<T>::events();
let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
Expand All @@ -1750,12 +1749,9 @@ mod benchmarking {
assert_eq!(event, &system_event);
}

fn fill_winners<T: Config + paras::Config + shared::Config>(
lease_period_index: LeasePeriodOf<T>,
) {
fn fill_winners<T: Config + paras::Config>(lease_period_index: LeasePeriodOf<T>) {
let auction_index = AuctionCounter::<T>::get();
let minimum_balance = CurrencyOf::<T>::minimum_balance();
let session_index = shared::Pallet::<T>::session_index();

for n in 1..=SlotRange::SLOT_RANGE_COUNT as u32 {
let owner = account("owner", n, 0);
Expand All @@ -1771,11 +1767,10 @@ mod benchmarking {
)
.is_ok());
}
conclude_pvf_checking::<T>(
&T::Registrar::worst_validation_code(),
VALIDATORS,
session_index,
);
assert_ok!(paras::Pallet::<T>::add_trusted_validation_code(
frame_system::Origin::<T>::Root.into(),
T::Registrar::worst_validation_code(),
));

T::Registrar::execute_pending_transitions();

Expand All @@ -1799,7 +1794,7 @@ mod benchmarking {
}

benchmarks! {
where_clause { where T: pallet_babe::Config + paras::Config + shared::Config }
where_clause { where T: pallet_babe::Config + paras::Config }

new_auction {
let duration = T::BlockNumber::max_value();
Expand All @@ -1817,8 +1812,6 @@ mod benchmarking {

// Worst case scenario a new bid comes in which kicks out an existing bid for the same slot.
bid {
let public = validators_public_keys(VALIDATORS);
shared::Pallet::<T>::set_active_validators_ascending(public);
// If there is an offset, we need to be on that block to be able to do lease things.
let (_, offset) = T::Leaser::lease_period_length();
frame_system::Pallet::<T>::set_block_number(offset + One::one());
Expand All @@ -1840,9 +1833,10 @@ mod benchmarking {
let worst_validation_code = T::Registrar::worst_validation_code();
T::Registrar::register(owner.clone(), para, worst_head_data.clone(), worst_validation_code.clone())?;
T::Registrar::register(owner, new_para, worst_head_data, worst_validation_code.clone())?;

let session_index = shared::Pallet::<T>::session_index();
conclude_pvf_checking::<T>(&T::Registrar::worst_validation_code(), VALIDATORS, session_index);
assert_ok!(paras::Pallet::<T>::add_trusted_validation_code(
frame_system::Origin::<T>::Root.into(),
worst_validation_code,
));

T::Registrar::execute_pending_transitions();

Expand Down Expand Up @@ -1875,8 +1869,6 @@ mod benchmarking {
// Worst case: 10 bidders taking all wining spots, and we need to calculate the winner for auction end.
// Entire winner map should be full and removed at the end of the benchmark.
on_initialize {
let public = validators_public_keys(VALIDATORS);
shared::Pallet::<T>::set_active_validators_ascending(public);
// If there is an offset, we need to be on that block to be able to do lease things.
let (lease_length, offset) = T::Leaser::lease_period_length();
frame_system::Pallet::<T>::set_block_number(offset + One::one());
Expand Down Expand Up @@ -1922,8 +1914,6 @@ mod benchmarking {

// Worst case: 10 bidders taking all wining spots, and winning data is full.
cancel_auction {
let public = validators_public_keys(VALIDATORS);
shared::Pallet::<T>::set_active_validators_ascending(public);
// If there is an offset, we need to be on that block to be able to do lease things.
let (lease_length, offset) = T::Leaser::lease_period_length();
frame_system::Pallet::<T>::set_block_number(offset + One::one());
Expand Down
63 changes: 16 additions & 47 deletions runtime/common/src/crowdloan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1951,19 +1951,15 @@ mod tests {
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking {
use super::{Pallet as Crowdloan, *};
use crate::mock::{conclude_pvf_checking, validators_public_keys};
use frame_support::{assert_ok, traits::OnInitialize};
use frame_system::RawOrigin;
use runtime_parachains::{paras, shared};
use runtime_parachains::paras;
use sp_core::crypto::UncheckedFrom;
use sp_keyring::Sr25519Keyring;
use sp_runtime::traits::{Bounded, CheckedSub};
use sp_std::prelude::*;

use frame_benchmarking::{account, benchmarks, whitelisted_caller};

const VALIDATORS: &[Sr25519Keyring] = &[Sr25519Keyring::Alice];

fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
let events = frame_system::Pallet::<T>::events();
let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
Expand All @@ -1972,10 +1968,7 @@ mod benchmarking {
assert_eq!(event, &system_event);
}

fn create_fund<T: Config + shared::Config + paras::Config>(
id: u32,
end: T::BlockNumber,
) -> ParaId {
fn create_fund<T: Config + paras::Config>(id: u32, end: T::BlockNumber) -> ParaId {
let cap = BalanceOf::<T>::max_value();
let (_, offset) = T::Auctioneer::lease_period_length();
// Set to the very beginning of lease period index 0.
Expand All @@ -1993,7 +1986,6 @@ mod benchmarking {
// Assume ed25519 is most complex signature format
let pubkey = crypto::create_ed25519_pubkey(b"//verifier".to_vec());

let session_index = shared::Pallet::<T>::session_index();
let head_data = T::Registrar::worst_head_data();
let validation_code = T::Registrar::worst_validation_code();
assert_ok!(T::Registrar::register(
Expand All @@ -2002,8 +1994,10 @@ mod benchmarking {
head_data,
validation_code.clone()
));
conclude_pvf_checking::<T>(&validation_code, VALIDATORS, session_index);

assert_ok!(paras::Pallet::<T>::add_trusted_validation_code(
frame_system::Origin::<T>::Root.into(),
validation_code,
));
T::Registrar::execute_pending_transitions();

assert_ok!(Crowdloan::<T>::create(
Expand Down Expand Up @@ -2036,12 +2030,9 @@ mod benchmarking {
}

benchmarks! {
where_clause { where T: paras::Config + shared::Config }
where_clause { where T: paras::Config }

create {
let public = validators_public_keys(VALIDATORS);
shared::Pallet::<T>::set_active_validators_ascending(public);

let para_id = ParaId::from(1_u32);
let cap = BalanceOf::<T>::max_value();
let first_period = 0u32.into();
Expand All @@ -2050,15 +2041,18 @@ mod benchmarking {
let end = lpl + offset;

let caller: T::AccountId = whitelisted_caller();
let session_index = shared::Pallet::<T>::session_index();
let head_data = T::Registrar::worst_head_data();
let validation_code = T::Registrar::worst_validation_code();

let verifier = MultiSigner::unchecked_from(account::<[u8; 32]>("verifier", 0, 0));

CurrencyOf::<T>::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
T::Registrar::register(caller.clone(), para_id, head_data, validation_code.clone())?;
conclude_pvf_checking::<T>(&validation_code, VALIDATORS, session_index);
assert_ok!(paras::Pallet::<T>::add_trusted_validation_code(
frame_system::Origin::<T>::Root.into(),
validation_code,
));

T::Registrar::execute_pending_transitions();

}: _(RawOrigin::Signed(caller), para_id, cap, first_period, last_period, end, Some(verifier))
Expand All @@ -2068,9 +2062,6 @@ mod benchmarking {

// Contribute has two arms: PreEnding and Ending, but both are equal complexity.
contribute {
let public = validators_public_keys(VALIDATORS);
shared::Pallet::<T>::set_active_validators_ascending(public);

let (lpl, offset) = T::Auctioneer::lease_period_length();
let end = lpl + offset;
let fund_index = create_fund::<T>(1, end);
Expand All @@ -2091,9 +2082,6 @@ mod benchmarking {
}

withdraw {
let public = validators_public_keys(VALIDATORS);
shared::Pallet::<T>::set_active_validators_ascending(public);

let (lpl, offset) = T::Auctioneer::lease_period_length();
let end = lpl + offset;
let fund_index = create_fund::<T>(1337, end);
Expand All @@ -2110,10 +2098,6 @@ mod benchmarking {
#[skip_meta]
refund {
let k in 0 .. T::RemoveKeysLimit::get();

let public = validators_public_keys(VALIDATORS);
shared::Pallet::<T>::set_active_validators_ascending(public);

let (lpl, offset) = T::Auctioneer::lease_period_length();
let end = lpl + offset;
let fund_index = create_fund::<T>(1337, end);
Expand All @@ -2123,7 +2107,6 @@ mod benchmarking {
contribute_fund::<T>(&account("contributor", i, 0), fund_index);
}


let caller: T::AccountId = whitelisted_caller();
frame_system::Pallet::<T>::set_block_number(T::BlockNumber::max_value());
}: _(RawOrigin::Signed(caller), fund_index)
Expand All @@ -2132,9 +2115,6 @@ mod benchmarking {
}

dissolve {
let public = validators_public_keys(VALIDATORS);
shared::Pallet::<T>::set_active_validators_ascending(public);

let (lpl, offset) = T::Auctioneer::lease_period_length();
let end = lpl + offset;
let fund_index = create_fund::<T>(1337, end);
Expand All @@ -2146,9 +2126,6 @@ mod benchmarking {
}

edit {
let public = validators_public_keys(VALIDATORS);
shared::Pallet::<T>::set_active_validators_ascending(public);

let para_id = ParaId::from(1_u32);
let cap = BalanceOf::<T>::max_value();
let first_period = 0u32.into();
Expand All @@ -2159,13 +2136,15 @@ mod benchmarking {
let caller: T::AccountId = whitelisted_caller();
let head_data = T::Registrar::worst_head_data();
let validation_code = T::Registrar::worst_validation_code();
let session_index = shared::Pallet::<T>::session_index();

let verifier = MultiSigner::unchecked_from(account::<[u8; 32]>("verifier", 0, 0));

CurrencyOf::<T>::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
T::Registrar::register(caller.clone(), para_id, head_data, validation_code.clone())?;
conclude_pvf_checking::<T>(&validation_code, VALIDATORS, session_index);
assert_ok!(paras::Pallet::<T>::add_trusted_validation_code(
frame_system::Origin::<T>::Root.into(),
validation_code,
));

T::Registrar::execute_pending_transitions();

Expand All @@ -2181,9 +2160,6 @@ mod benchmarking {
}

add_memo {
let public = validators_public_keys(VALIDATORS);
shared::Pallet::<T>::set_active_validators_ascending(public);

let (lpl, offset) = T::Auctioneer::lease_period_length();
let end = lpl + offset;
let fund_index = create_fund::<T>(1, end);
Expand All @@ -2200,9 +2176,6 @@ mod benchmarking {
}

poke {
let public = validators_public_keys(VALIDATORS);
shared::Pallet::<T>::set_active_validators_ascending(public);

let (lpl, offset) = T::Auctioneer::lease_period_length();
let end = lpl + offset;
let fund_index = create_fund::<T>(1, end);
Expand All @@ -2222,10 +2195,6 @@ mod benchmarking {
on_initialize {
// We test the complexity over different number of new raise
let n in 2 .. 100;

let public = validators_public_keys(VALIDATORS);
shared::Pallet::<T>::set_active_validators_ascending(public);

let (lpl, offset) = T::Auctioneer::lease_period_length();
let end_block = lpl + offset - 1u32.into();

Expand Down
Loading

0 comments on commit a50ca39

Please sign in to comment.