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

Commit

Permalink
Fix Benchmarks for Statemine-V4 release (#639)
Browse files Browse the repository at this point in the history
* register validators

* register_as_candidate & leave_intent fixed

* new_session benchmark fixed

* intent_leave_modified

* clean up

* clean up

* benchmark script updated

* update cargo.lock

* done

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>
  • Loading branch information
NachoPal and apopiak committed Oct 8, 2021
1 parent bf6ae74 commit 621d45f
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 11 deletions.
6 changes: 5 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pallets/collator-selection/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ targets = ['x86_64-unknown-linux-gnu']
[dependencies]
log = { version = "0.4.0", default-features = false }
codec = { default-features = false, features = ['derive'], package = 'parity-scale-codec', version = '2.3.0' }
rand = { version = "0.7.2", default-features = false }
scale-info = { version = "1.0.0", default-features = false, features = ["derive"] }
serde = { version = "1.0.119", default-features = false }

sp-std = { default-features = false, git = 'https://github.com/paritytech/substrate', branch = "master" }
sp-runtime = { default-features = false, git = 'https://github.com/paritytech/substrate', branch = "master" }
sp-staking = { default-features = false, git = 'https://github.com/paritytech/substrate', branch = "master" }
Expand Down Expand Up @@ -47,7 +49,11 @@ runtime-benchmarks = [
std = [
'codec/std',
'log/std',
<<<<<<< HEAD
'scale-info/std',
=======
'rand/std',
>>>>>>> 288a98d3... Fix Benchmarks for Statemine-V4 release (#639)
'sp-runtime/std',
'sp-staking/std',
'sp-std/std',
Expand Down
85 changes: 77 additions & 8 deletions pallets/collator-selection/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ use crate::Pallet as CollatorSelection;
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use frame_support::{
assert_ok,
traits::{Currency, EnsureOrigin, Get},
traits::{Currency, Get, EnsureOrigin},
codec::Decode
};
use frame_system::{EventRecord, RawOrigin};
use pallet_authorship::EventHandler;
use pallet_session::SessionManager;
use pallet_session::{self as session, SessionManager};
use sp_std::prelude::*;

pub type BalanceOf<T> =
Expand All @@ -51,17 +52,60 @@ fn assert_last_event<T: Config>(generic_event: <T as Config>::Event) {
assert_eq!(event, &system_event);
}

fn create_funded_user<T: Config>(
string: &'static str,
n: u32,
balance_factor: u32,
) -> T::AccountId {
let user = account(string, n, SEED);
let balance = T::Currency::minimum_balance() * balance_factor.into();
let _ = T::Currency::make_free_balance_be(&user, balance);
user
}

fn keys<T: Config + session::Config>(c: u32) -> <T as session::Config>::Keys {
use rand::{RngCore, SeedableRng};

let keys = {
let mut keys = [0u8; 128];

if c > 0 {
let mut rng = rand::rngs::StdRng::seed_from_u64(c as u64);
rng.fill_bytes(&mut keys);
}

keys
};

Decode::decode(&mut &keys[..]).unwrap()
}

fn validator<T: Config + session::Config>(c: u32)-> (T::AccountId, <T as session::Config>::Keys) {
(create_funded_user::<T>("candidate", c, 1000), keys::<T>(c))
}

fn register_validators<T: Config + session::Config>(count: u32) {
let validators = (0..count).map(|c| validator::<T>(c)).collect::<Vec<_>>();

for (who, keys) in validators {
<session::Module<T>>::set_keys(
RawOrigin::Signed(who).into(), keys, vec![]
).unwrap();
}
}

fn register_candidates<T: Config>(count: u32) {
let candidates = (0..count).map(|c| account("candidate", c, SEED)).collect::<Vec<_>>();
assert!(<CandidacyBond<T>>::get() > 0u32.into(), "Bond cannot be zero!");

for who in candidates {
T::Currency::make_free_balance_be(&who, <CandidacyBond<T>>::get() * 2u32.into());
<CollatorSelection<T>>::register_as_candidate(RawOrigin::Signed(who).into()).unwrap();
}
}

benchmarks! {
where_clause { where T: pallet_authorship::Config }
where_clause { where T: pallet_authorship::Config + session::Config }

set_invulnerables {
let b in 1 .. T::MaxInvulnerables::get();
Expand Down Expand Up @@ -107,22 +151,32 @@ benchmarks! {

<CandidacyBond<T>>::put(T::Currency::minimum_balance());
<DesiredCandidates<T>>::put(c + 1);

register_validators::<T>(c);
register_candidates::<T>(c);

let caller: T::AccountId = whitelisted_caller();
let bond: BalanceOf<T> = T::Currency::minimum_balance() * 2u32.into();
T::Currency::make_free_balance_be(&caller, bond.clone());

<session::Module<T>>::set_keys(
RawOrigin::Signed(caller.clone()).into(),
keys::<T>(c + 1),
vec![]
).unwrap();

}: _(RawOrigin::Signed(caller.clone()))
verify {
assert_last_event::<T>(Event::CandidateAdded(caller, bond / 2u32.into()).into());
}

// worse case is the last candidate leaving.
leave_intent {
let c in 1 .. T::MaxCandidates::get();
let c in (T::MinCandidates::get() + 1) .. T::MaxCandidates::get();
<CandidacyBond<T>>::put(T::Currency::minimum_balance());
<DesiredCandidates<T>>::put(c);

register_validators::<T>(c);
register_candidates::<T>(c);

let leaving = <Candidates<T>>::get().last().unwrap().who.clone();
Expand Down Expand Up @@ -160,6 +214,8 @@ benchmarks! {
<CandidacyBond<T>>::put(T::Currency::minimum_balance());
<DesiredCandidates<T>>::put(c);
frame_system::Pallet::<T>::set_block_number(0u32.into());

register_validators::<T>(c);
register_candidates::<T>(c);

let new_block: T::BlockNumber = 1800u32.into();
Expand All @@ -171,19 +227,32 @@ benchmarks! {
for i in 0..c {
<LastAuthoredBlock<T>>::insert(candidates[i as usize].who.clone(), zero_block);
}
for i in 0..non_removals {
<LastAuthoredBlock<T>>::insert(candidates[i as usize].who.clone(), new_block);

if non_removals > 0 {
for i in 0..non_removals {
<LastAuthoredBlock<T>>::insert(candidates[i as usize].who.clone(), new_block);
}
} else {
for i in 0..c {
<LastAuthoredBlock<T>>::insert(candidates[i as usize].who.clone(), new_block);
}
}

let pre_length = <Candidates<T>>::get().len();

frame_system::Pallet::<T>::set_block_number(new_block);

assert!(<Candidates<T>>::get().len() == c as usize);

}: {
<CollatorSelection<T> as SessionManager<_>>::new_session(0)
} verify {
assert!(<Candidates<T>>::get().len() < pre_length);
if c > r && non_removals >= T::MinCandidates::get() {
assert!(<Candidates<T>>::get().len() < pre_length);
} else if c > r && non_removals < T::MinCandidates::get() {
assert!(<Candidates<T>>::get().len() == T::MinCandidates::get() as usize);
} else {
assert!(<Candidates<T>>::get().len() == pre_length);
}
}
}

Expand Down
23 changes: 21 additions & 2 deletions scripts/benchmarks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

steps=50
repeat=20
statemineOutput=./polkadot-parachains/statemine-runtime/src/weights
statemintOutput=./polkadot-parachains/statemint-runtime/src/weights

statemineOutput=./polkadot-parachains/statemine/src/weights
statemintOutput=./polkadot-parachains/statemint/src/weights
westmintOutput=./polkadot-parachains/westmint/src/weights

statemineChain=statemine-dev
statemintChain=statemint-dev
westmintChain=westmint-dev

pallets=(
pallet_assets
pallet_balances
Expand All @@ -15,6 +20,7 @@ pallets=(
pallet_session
pallet_timestamp
pallet_utility
pallet_uniques
)

for p in ${pallets[@]}
Expand All @@ -28,6 +34,7 @@ do
--steps=$steps \
--repeat=$repeat \
--raw \
--header=./file_header.txt \
--output=$statemineOutput

./target/release/polkadot-collator benchmark \
Expand All @@ -39,6 +46,18 @@ do
--steps=$steps \
--repeat=$repeat \
--raw \
--header=./file_header.txt \
--output=$statemintOutput

./target/release/polkadot-collator benchmark \
--chain=$westmintChain \
--execution=wasm \
--wasm-execution=compiled \
--pallet=$p \
--extrinsic='*' \
--steps=$steps \
--repeat=$repeat \
--raw \
--header=./file_header.txt \
--output=$westmintOutput
done

0 comments on commit 621d45f

Please sign in to comment.