Skip to content

Commit

Permalink
Add benchmarking for init_members
Browse files Browse the repository at this point in the history
Signed-off-by: koushiro <koushiro.cqx@gmail.com>
  • Loading branch information
koushiro committed Oct 11, 2021
1 parent f3b3cec commit 7375ee7
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 47 deletions.
7 changes: 7 additions & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,9 @@ impl pallet_collective::Config<AllianceCollective> for Runtime {
}

parameter_types! {
pub const MaxFounders: u32 = 10;
pub const MaxFellows: u32 = AllianceMaxMembers::get() - MaxFounders::get();
pub const MaxAllies: u32 = 100;
pub const MaxBlacklistCount: u32 = 100;
pub const MaxWebsiteUrlLength: u32 = 255;
}
Expand All @@ -1278,6 +1281,10 @@ impl pallet_alliance::Config<AllianceCollective> for Runtime {
type MembershipChanged = AllianceMotion;
type IdentityVerifier = AllianceIdentityVerifier;
type ProposalProvider = AllianceProposalProvider;
type MaxProposals = AllianceMaxProposals;
type MaxFounders = MaxFounders;
type MaxFellows = MaxFellows;
type MaxAllies = MaxAllies;
type MaxBlacklistCount = MaxBlacklistCount;
type MaxWebsiteUrlLength = MaxWebsiteUrlLength;
type CandidateDeposit = CandidateDeposit;
Expand Down
28 changes: 24 additions & 4 deletions frame/alliance/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,26 @@ fn set_candidates<T: Config<I>, I: 'static>(indexes: Vec<u32>) {
}

benchmarks_instance_pallet! {
init_members {
// at least 2 founders
let a in 2 .. T::MaxFounders::get();
let b in 0 .. T::MaxFellows::get();
let c in 0 .. T::MaxAllies::get();

let mut founders = (2 .. a).map(founder::<T, I>).collect::<Vec<_>>();
let mut fellows = (0 .. b).map(fellow::<T, I>).collect::<Vec<_>>();
let mut allies = (0 .. c).map(ally::<T, I>).collect::<Vec<_>>();

}: _(RawOrigin::Root, founders.clone(), fellows.clone(), allies.clone())
verify {
founders.sort();
fellows.sort();
allies.sort();
assert_eq!(Alliance::<T, I>::members(MemberRole::Founder), founders);
assert_eq!(Alliance::<T, I>::members(MemberRole::Fellow), fellows);
assert_eq!(Alliance::<T, I>::members(MemberRole::Ally), allies);
}

set_rule {
set_members::<T, I>();

Expand Down Expand Up @@ -266,8 +286,8 @@ benchmarks_instance_pallet! {
}

add_blacklist {
let n in 0 .. T::MaxBlacklistCount::get();
let l in 0 .. T::MaxWebsiteUrlLength::get();
let n in 1 .. T::MaxBlacklistCount::get();
let l in 1 .. T::MaxWebsiteUrlLength::get();

set_members::<T, I>();

Expand All @@ -286,8 +306,8 @@ benchmarks_instance_pallet! {
}

remove_blacklist {
let n in 0 .. T::MaxBlacklistCount::get();
let l in 0 .. T::MaxWebsiteUrlLength::get();
let n in 1 .. T::MaxBlacklistCount::get();
let l in 1 .. T::MaxWebsiteUrlLength::get();

set_members::<T, I>();

Expand Down
8 changes: 6 additions & 2 deletions frame/alliance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,12 @@ pub mod pallet {

/// Initialize the founders/fellows/allies.
///
/// This should be called by the referendum and can only be called once.
#[pallet::weight(0)]
/// This should only be called once.
#[pallet::weight(T::WeightInfo::init_members(
T::MaxFounders::get(),
T::MaxFellows::get(),
T::MaxAllies::get()
))]
pub fn init_members(
origin: OriginFor<T>,
mut founders: Vec<T::AccountId>,
Expand Down
2 changes: 1 addition & 1 deletion frame/alliance/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl Config for Test {
type IdentityVerifier = AllianceIdentityVerifier;
type ProposalProvider = AllianceProposalProvider;
type MaxProposals = MaxProposals;
type MaxFounders = MaxMembers;
type MaxFounders = MaxFounders;
type MaxFellows = MaxFellows;
type MaxAllies = MaxAllies;
type MaxBlacklistCount = MaxBlacklistCount;
Expand Down
103 changes: 63 additions & 40 deletions frame/alliance/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use sp_std::marker::PhantomData;

/// Weight functions needed for pallet_alliance.
pub trait WeightInfo {
fn init_members(a: u32, b: u32, c: u32, ) -> Weight;
fn set_rule() -> Weight;
fn announce() -> Weight;
fn remove_announcement() -> Weight;
Expand All @@ -62,20 +63,31 @@ pub trait WeightInfo {
/// Weights for pallet_alliance using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Storage: Alliance Members (r:3 w:3)
// Storage: AllianceMotion Members (r:1 w:1)
fn init_members(_a: u32, b: u32, c: u32, ) -> Weight {
(42_518_000 as Weight)
// Standard Error: 2_000
.saturating_add((169_000 as Weight).saturating_mul(b as Weight))
// Standard Error: 2_000
.saturating_add((162_000 as Weight).saturating_mul(c as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
.saturating_add(T::DbWeight::get().writes(4 as Weight))
}
// Storage: Alliance Rule (r:0 w:1)
fn set_rule() -> Weight {
(15_168_000 as Weight)
(14_427_000 as Weight)
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: Alliance Announcements (r:1 w:1)
fn announce() -> Weight {
(16_982_000 as Weight)
(17_153_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: Alliance Announcements (r:1 w:1)
fn remove_announcement() -> Weight {
(17_312_000 as Weight)
(17_152_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
Expand All @@ -85,22 +97,22 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Storage: System Account (r:1 w:1)
// Storage: Alliance DepositOf (r:0 w:1)
fn submit_candidacy() -> Weight {
(58_701_000 as Weight)
(59_542_000 as Weight)
.saturating_add(T::DbWeight::get().reads(7 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
// Storage: Alliance Members (r:4 w:0)
// Storage: Alliance AccountBlacklist (r:1 w:0)
// Storage: Alliance Candidates (r:1 w:1)
fn nominate_candidacy() -> Weight {
(43_682_000 as Weight)
(43_241_000 as Weight)
.saturating_add(T::DbWeight::get().reads(6 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: Alliance Candidates (r:1 w:1)
// Storage: Alliance Members (r:4 w:1)
fn approve_candidate() -> Weight {
(43_382_000 as Weight)
(44_303_000 as Weight)
.saturating_add(T::DbWeight::get().reads(5 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
Expand All @@ -109,7 +121,7 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Storage: Alliance DepositOf (r:1 w:1)
// Storage: System Account (r:1 w:1)
fn reject_candidate() -> Weight {
(68_389_000 as Weight)
(68_839_000 as Weight)
.saturating_add(T::DbWeight::get().reads(7 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
Expand All @@ -118,7 +130,7 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Storage: AllianceMotion Members (r:0 w:1)
// Storage: AllianceMotion Prime (r:0 w:1)
fn elevate_ally() -> Weight {
(42_671_000 as Weight)
(42_540_000 as Weight)
.saturating_add(T::DbWeight::get().reads(4 as Weight))
.saturating_add(T::DbWeight::get().writes(4 as Weight))
}
Expand All @@ -130,7 +142,7 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Storage: AllianceMotion Members (r:0 w:1)
// Storage: AllianceMotion Prime (r:0 w:1)
fn retire() -> Weight {
(59_943_000 as Weight)
(59_933_000 as Weight)
.saturating_add(T::DbWeight::get().reads(7 as Weight))
.saturating_add(T::DbWeight::get().writes(5 as Weight))
}
Expand All @@ -142,50 +154,61 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Storage: AllianceMotion Members (r:0 w:1)
// Storage: AllianceMotion Prime (r:0 w:1)
fn kick_member() -> Weight {
(67_326_000 as Weight)
(66_675_000 as Weight)
.saturating_add(T::DbWeight::get().reads(7 as Weight))
.saturating_add(T::DbWeight::get().writes(5 as Weight))
}
// Storage: Alliance AccountBlacklist (r:1 w:0)
// Storage: Alliance WebsiteBlacklist (r:1 w:0)
// Storage: Alliance AccountBlacklist (r:1 w:1)
// Storage: Alliance WebsiteBlacklist (r:1 w:1)
fn add_blacklist(n: u32, l: u32, ) -> Weight {
(0 as Weight)
// Standard Error: 14_000
.saturating_add((2_579_000 as Weight).saturating_mul(n as Weight))
// Standard Error: 5_000
.saturating_add((187_000 as Weight).saturating_mul(l as Weight))
// Standard Error: 6_000
.saturating_add((2_626_000 as Weight).saturating_mul(n as Weight))
// Standard Error: 3_000
.saturating_add((206_000 as Weight).saturating_mul(l as Weight))
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
// Storage: Alliance AccountBlacklist (r:1 w:1)
// Storage: Alliance WebsiteBlacklist (r:1 w:1)
fn remove_blacklist(n: u32, l: u32, ) -> Weight {
(0 as Weight)
// Standard Error: 453_000
.saturating_add((59_643_000 as Weight).saturating_mul(n as Weight))
// Standard Error: 177_000
.saturating_add((7_981_000 as Weight).saturating_mul(l as Weight))
// Standard Error: 338_000
.saturating_add((58_173_000 as Weight).saturating_mul(n as Weight))
// Standard Error: 150_000
.saturating_add((6_640_000 as Weight).saturating_mul(l as Weight))
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
}

// For backwards compatibility and tests
impl WeightInfo for () {
// Storage: Alliance Members (r:3 w:3)
// Storage: AllianceMotion Members (r:1 w:1)
fn init_members(_a: u32, b: u32, c: u32, ) -> Weight {
(42_518_000 as Weight)
// Standard Error: 2_000
.saturating_add((169_000 as Weight).saturating_mul(b as Weight))
// Standard Error: 2_000
.saturating_add((162_000 as Weight).saturating_mul(c as Weight))
.saturating_add(RocksDbWeight::get().reads(4 as Weight))
.saturating_add(RocksDbWeight::get().writes(4 as Weight))
}
// Storage: Alliance Rule (r:0 w:1)
fn set_rule() -> Weight {
(15_168_000 as Weight)
(14_427_000 as Weight)
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
// Storage: Alliance Announcements (r:1 w:1)
fn announce() -> Weight {
(16_982_000 as Weight)
(17_153_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
// Storage: Alliance Announcements (r:1 w:1)
fn remove_announcement() -> Weight {
(17_312_000 as Weight)
(17_152_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
Expand All @@ -195,22 +218,22 @@ impl WeightInfo for () {
// Storage: System Account (r:1 w:1)
// Storage: Alliance DepositOf (r:0 w:1)
fn submit_candidacy() -> Weight {
(58_701_000 as Weight)
(59_542_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(7 as Weight))
.saturating_add(RocksDbWeight::get().writes(3 as Weight))
}
// Storage: Alliance Members (r:4 w:0)
// Storage: Alliance AccountBlacklist (r:1 w:0)
// Storage: Alliance Candidates (r:1 w:1)
fn nominate_candidacy() -> Weight {
(43_682_000 as Weight)
(43_241_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(6 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
// Storage: Alliance Candidates (r:1 w:1)
// Storage: Alliance Members (r:4 w:1)
fn approve_candidate() -> Weight {
(43_382_000 as Weight)
(44_303_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(5 as Weight))
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
}
Expand All @@ -219,7 +242,7 @@ impl WeightInfo for () {
// Storage: Alliance DepositOf (r:1 w:1)
// Storage: System Account (r:1 w:1)
fn reject_candidate() -> Weight {
(68_389_000 as Weight)
(68_839_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(7 as Weight))
.saturating_add(RocksDbWeight::get().writes(3 as Weight))
}
Expand All @@ -228,7 +251,7 @@ impl WeightInfo for () {
// Storage: AllianceMotion Members (r:0 w:1)
// Storage: AllianceMotion Prime (r:0 w:1)
fn elevate_ally() -> Weight {
(42_671_000 as Weight)
(42_540_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(4 as Weight))
.saturating_add(RocksDbWeight::get().writes(4 as Weight))
}
Expand All @@ -240,7 +263,7 @@ impl WeightInfo for () {
// Storage: AllianceMotion Members (r:0 w:1)
// Storage: AllianceMotion Prime (r:0 w:1)
fn retire() -> Weight {
(59_943_000 as Weight)
(59_933_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(7 as Weight))
.saturating_add(RocksDbWeight::get().writes(5 as Weight))
}
Expand All @@ -252,29 +275,29 @@ impl WeightInfo for () {
// Storage: AllianceMotion Members (r:0 w:1)
// Storage: AllianceMotion Prime (r:0 w:1)
fn kick_member() -> Weight {
(67_326_000 as Weight)
(66_675_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(7 as Weight))
.saturating_add(RocksDbWeight::get().writes(5 as Weight))
}
// Storage: Alliance AccountBlacklist (r:1 w:0)
// Storage: Alliance WebsiteBlacklist (r:1 w:0)
// Storage: Alliance AccountBlacklist (r:1 w:1)
// Storage: Alliance WebsiteBlacklist (r:1 w:1)
fn add_blacklist(n: u32, l: u32, ) -> Weight {
(0 as Weight)
// Standard Error: 14_000
.saturating_add((2_579_000 as Weight).saturating_mul(n as Weight))
// Standard Error: 5_000
.saturating_add((187_000 as Weight).saturating_mul(l as Weight))
// Standard Error: 6_000
.saturating_add((2_626_000 as Weight).saturating_mul(n as Weight))
// Standard Error: 3_000
.saturating_add((206_000 as Weight).saturating_mul(l as Weight))
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
}
// Storage: Alliance AccountBlacklist (r:1 w:1)
// Storage: Alliance WebsiteBlacklist (r:1 w:1)
fn remove_blacklist(n: u32, l: u32, ) -> Weight {
(0 as Weight)
// Standard Error: 453_000
.saturating_add((59_643_000 as Weight).saturating_mul(n as Weight))
// Standard Error: 177_000
.saturating_add((7_981_000 as Weight).saturating_mul(l as Weight))
// Standard Error: 338_000
.saturating_add((58_173_000 as Weight).saturating_mul(n as Weight))
// Standard Error: 150_000
.saturating_add((6_640_000 as Weight).saturating_mul(l as Weight))
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
}
Expand Down

0 comments on commit 7375ee7

Please sign in to comment.