Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a46d35f
First stab at adding benchmarking for
Mar 29, 2022
2c275fc
Adding `BoundedPhragMMS` and fixing stuff
Mar 29, 2022
e96e7f9
Fixing node runtime
Mar 29, 2022
575e6d9
Fixing tests
Mar 29, 2022
a92a735
Finalising all benchmarking stuff
Mar 31, 2022
6c884cd
better comments
Apr 1, 2022
d96872a
Merge branch 'paritytech:master' into onchain-election-benchmarking
georgesdib Apr 1, 2022
5b0bdc3
Better benchmarking config
Apr 1, 2022
728eeec
Better `WeightInfo` and benchmarking
Apr 1, 2022
d50bd98
Fixing tests
Apr 1, 2022
be0d0dc
Merge branch 'paritytech:master' into onchain-election-benchmarking
georgesdib Apr 2, 2022
382a74a
Adding some documentation
Apr 2, 2022
37f2129
Merge branch 'onchain-election-benchmarking' of https://github.com/ge…
Apr 2, 2022
bb19b77
Fixing some typos
Apr 3, 2022
6eb4978
Incorporating review feedback
Apr 4, 2022
f2faaeb
cleanup of rustdocs
Apr 4, 2022
52aef01
Merge branch 'paritytech:master' into onchain-election-benchmarking
georgesdib Apr 5, 2022
9e55d7f
rustdoc changes
Apr 5, 2022
9d2e07b
Merge branch 'onchain-election-benchmarking' of https://github.com/ge…
Apr 5, 2022
1729278
changes after code review
Apr 11, 2022
23c1453
Fixing some errors.
Apr 11, 2022
1375dde
Merge branch 'paritytech:master' into onchain-election-benchmarking
georgesdib Apr 11, 2022
5c679fd
Fixing dependencies post merge
Apr 11, 2022
b61dae9
Bringing back `UnboundedExecution`
Apr 12, 2022
3654c7b
Better rustdoc and naming
Apr 12, 2022
d43e909
Cargo.toml formatting
Apr 12, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ members = [
"frame/try-runtime",
"frame/election-provider-multi-phase",
"frame/election-provider-support",
"frame/election-provider-support/benchmarking",
"frame/election-provider-support/solution-type",
"frame/election-provider-support/solution-type/fuzzer",
"frame/examples/basic",
Expand Down
2 changes: 2 additions & 0 deletions bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pallet-contracts-rpc-runtime-api = { version = "4.0.0-dev", default-features = f
pallet-conviction-voting = { version = "4.0.0-dev", default-features = false, path = "../../../frame/conviction-voting" }
pallet-democracy = { version = "4.0.0-dev", default-features = false, path = "../../../frame/democracy" }
pallet-election-provider-multi-phase = { version = "4.0.0-dev", default-features = false, path = "../../../frame/election-provider-multi-phase" }
pallet-election-provider-support-benchmarking = { version = "4.0.0-dev", default-features = false, path = "../../../frame/election-provider-support/benchmarking", optional = true }
pallet-elections-phragmen = { version = "5.0.0-dev", default-features = false, path = "../../../frame/elections-phragmen" }
pallet-gilt = { version = "4.0.0-dev", default-features = false, path = "../../../frame/gilt" }
pallet-grandpa = { version = "4.0.0-dev", default-features = false, path = "../../../frame/grandpa" }
Expand Down Expand Up @@ -196,6 +197,7 @@ runtime-benchmarks = [
"pallet-conviction-voting/runtime-benchmarks",
"pallet-democracy/runtime-benchmarks",
"pallet-election-provider-multi-phase/runtime-benchmarks",
"pallet-election-provider-support-benchmarking/runtime-benchmarks",
"pallet-elections-phragmen/runtime-benchmarks",
"pallet-gilt/runtime-benchmarks",
"pallet-grandpa/runtime-benchmarks",
Expand Down
11 changes: 8 additions & 3 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,17 +643,18 @@ impl Get<Option<(usize, ExtendedBalance)>> for OffchainRandomBalancing {
}

pub struct OnChainSeqPhragmen;
impl onchain::ExecutionConfig for OnChainSeqPhragmen {
impl onchain::Config for OnChainSeqPhragmen {
type System = Runtime;
type Solver = SequentialPhragmen<
AccountId,
pallet_election_provider_multi_phase::SolutionAccuracyOf<Runtime>,
>;
type DataProvider = <Runtime as pallet_election_provider_multi_phase::Config>::DataProvider;
type WeightInfo = frame_election_provider_support::weights::SubstrateWeight<Runtime>;
}

impl onchain::BoundedExecutionConfig for OnChainSeqPhragmen {
type VotersBound = ConstU32<20_000>;
impl onchain::BoundedConfig for OnChainSeqPhragmen {
type VotersBound = MaxElectingVoters;
type TargetsBound = ConstU32<2_000>;
}

Expand Down Expand Up @@ -1531,6 +1532,7 @@ mod benches {
[pallet_contracts, Contracts]
[pallet_democracy, Democracy]
[pallet_election_provider_multi_phase, ElectionProviderMultiPhase]
[pallet_election_provider_support_benchmarking, EPSBench::<Runtime>]
[pallet_elections_phragmen, Elections]
[pallet_gilt, Gilt]
[pallet_grandpa, Grandpa]
Expand Down Expand Up @@ -1847,6 +1849,7 @@ impl_runtime_apis! {
// which is why we need these two lines below.
use pallet_session_benchmarking::Pallet as SessionBench;
use pallet_offences_benchmarking::Pallet as OffencesBench;
use pallet_election_provider_support_benchmarking::Pallet as EPSBench;
use frame_system_benchmarking::Pallet as SystemBench;
use baseline::Pallet as BaselineBench;

Expand All @@ -1868,11 +1871,13 @@ impl_runtime_apis! {
// which is why we need these two lines below.
use pallet_session_benchmarking::Pallet as SessionBench;
use pallet_offences_benchmarking::Pallet as OffencesBench;
use pallet_election_provider_support_benchmarking::Pallet as EPSBench;
use frame_system_benchmarking::Pallet as SystemBench;
use baseline::Pallet as BaselineBench;

impl pallet_session_benchmarking::Config for Runtime {}
impl pallet_offences_benchmarking::Config for Runtime {}
impl pallet_election_provider_support_benchmarking::Config for Runtime {}
impl frame_system_benchmarking::Config for Runtime {}
impl baseline::Config for Runtime {}

Expand Down
3 changes: 2 additions & 1 deletion frame/babe/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,11 @@ parameter_types! {
}

pub struct OnChainSeqPhragmen;
impl onchain::ExecutionConfig for OnChainSeqPhragmen {
impl onchain::Config for OnChainSeqPhragmen {
type System = Test;
type Solver = SequentialPhragmen<DummyValidatorId, Perbill>;
type DataProvider = Staking;
type WeightInfo = ();
}

impl pallet_staking::Config for Test {
Expand Down
10 changes: 7 additions & 3 deletions frame/election-provider-multi-phase/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ static_assertions = "1.1.0"
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [
"derive",
] }
scale-info = { version = "2.0.1", default-features = false, features = ["derive"] }
scale-info = { version = "2.0.1", default-features = false, features = [
"derive",
] }
log = { version = "0.4.14", default-features = false }

frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
Expand All @@ -33,11 +35,14 @@ frame-election-provider-support = { version = "4.0.0-dev", default-features = fa

# Optional imports for benchmarking
frame-benchmarking = { version = "4.0.0-dev", default-features = false, path = "../benchmarking", optional = true }
pallet-election-provider-support-benchmarking = { version = "4.0.0-dev", default-features = false, path = "../election-provider-support/benchmarking", optional = true }
rand = { version = "0.7.3", default-features = false, optional = true, features = [
"alloc",
"small_rng",
] }
strum = { optional = true, default-features = false, version = "0.23.0", features = ["derive"] }
strum = { optional = true, default-features = false, version = "0.23.0", features = [
"derive",
] }

[dev-dependencies]
parking_lot = "0.12.0"
Expand All @@ -46,7 +51,6 @@ sp-core = { version = "6.0.0", default-features = false, path = "../../primitive
sp-io = { version = "6.0.0", path = "../../primitives/io" }
sp-npos-elections = { version = "4.0.0-dev", default-features = false, path = "../../primitives/npos-elections" }
sp-tracing = { version = "5.0.0", path = "../../primitives/tracing" }
frame-election-provider-support = { version = "4.0.0-dev", path = "../election-provider-support" }
pallet-balances = { version = "4.0.0-dev", path = "../balances" }
frame-benchmarking = { version = "4.0.0-dev", path = "../benchmarking" }

Expand Down
5 changes: 3 additions & 2 deletions frame/election-provider-multi-phase/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,11 @@ parameter_types! {
}

pub struct OnChainSeqPhragmen;
impl onchain::ExecutionConfig for OnChainSeqPhragmen {
impl onchain::Config for OnChainSeqPhragmen {
type System = Runtime;
type Solver = SequentialPhragmen<AccountId, SolutionAccuracyOf<Runtime>, Balancing>;
type DataProvider = StakingMock;
type WeightInfo = ();
}

pub struct MockFallback;
Expand All @@ -304,7 +305,7 @@ impl InstantElectionProvider for MockFallback {
max_voters,
max_targets,
)
.map_err(|_| "UnboundedExecution failed")
.map_err(|_| "onchain::UnboundedExecution failed.")
} else {
super::NoFallback::<Runtime>::elect_with_bounds(max_voters, max_targets)
}
Expand Down
38 changes: 38 additions & 0 deletions frame/election-provider-support/benchmarking/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "pallet-election-provider-support-benchmarking"
version = "4.0.0-dev"
authors = ["Parity Technologies <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
homepage = "https://substrate.io"
repository = "https://github.com/paritytech/substrate/"
description = "Benchmarking for election provider support onchain config trait"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [
"derive",
] }
sp-npos-elections = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/npos-elections" }
sp-runtime = { version = "6.0.0", default-features = false, path = "../../../primitives/runtime" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../../system" }
frame-election-provider-support = { version = "4.0.0-dev", default-features = false, path = ".." }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, path = "../../benchmarking", optional = true }


[features]
default = ["std"]
std = [
"codec/std",
"sp-npos-elections/std",
"sp-runtime/std",
"frame-benchmarking/std",
"frame-system/std",
]

runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-election-provider-support/runtime-benchmarks",
]
91 changes: 91 additions & 0 deletions frame/election-provider-support/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// This file is part of Substrate.

// Copyright (C) 2021-2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Election provider support pallet benchmarking.
//! This is separated into its own crate to avoid bloating the size of the runtime.

#![cfg(feature = "runtime-benchmarks")]
#![cfg_attr(not(feature = "std"), no_std)]

use codec::Decode;
use frame_benchmarking::{benchmarks, Vec};
use frame_election_provider_support::{NposSolver, PhragMMS, SequentialPhragmen};

pub struct Pallet<T: Config>(frame_system::Pallet<T>);
pub trait Config: frame_system::Config {}

const VOTERS: [u32; 2] = [1_000, 2_000];
const TARGETS: [u32; 2] = [500, 1_000];
const VOTES_PER_VOTER: [u32; 2] = [5, 16];

const SEED: u32 = 999;
fn set_up_voters_targets<AccountId: Decode + Clone>(
voters_len: u32,
targets_len: u32,
degree: usize,
) -> (Vec<(AccountId, u64, impl IntoIterator<Item = AccountId>)>, Vec<AccountId>) {
// fill targets.
let mut targets = (0..targets_len)
.map(|i| frame_benchmarking::account::<AccountId>("Target", i, SEED))
.collect::<Vec<_>>();
assert!(targets.len() > degree, "we should always have enough voters to fill");
targets.truncate(degree);

// fill voters.
let voters = (0..voters_len)
.map(|i| {
let voter = frame_benchmarking::account::<AccountId>("Voter", i, SEED);
(voter, 1_000, targets.clone())
})
.collect::<Vec<_>>();

(voters, targets)
}

benchmarks! {
phragmen {
// number of votes in snapshot.
let v in (VOTERS[0]) .. VOTERS[1];
// number of targets in snapshot.
let t in (TARGETS[0]) .. TARGETS[1];
// number of votes per voter (ie the degree).
let d in (VOTES_PER_VOTER[0]) .. VOTES_PER_VOTER[1];

let (voters, targets) = set_up_voters_targets::<T::AccountId>(v, t, d as usize);
}: {
assert!(
SequentialPhragmen::<T::AccountId, sp_runtime::Perbill>
::solve(d as usize, targets, voters).is_ok()
);
}

phragmms {
// number of votes in snapshot.
let v in (VOTERS[0]) .. VOTERS[1];
// number of targets in snapshot.
let t in (TARGETS[0]) .. TARGETS[1];
// number of votes per voter (ie the degree).
let d in (VOTES_PER_VOTER[0]) .. VOTES_PER_VOTER[1];

let (voters, targets) = set_up_voters_targets::<T::AccountId>(v, t, d as usize);
}: {
assert!(
PhragMMS::<T::AccountId, sp_runtime::Perbill>
::solve(d as usize, targets, voters).is_ok()
);
}
}
19 changes: 18 additions & 1 deletion frame/election-provider-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ pub mod onchain;
pub mod traits;
#[cfg(feature = "std")]
use codec::{Decode, Encode};
use frame_support::{BoundedVec, RuntimeDebug};
use frame_support::{weights::Weight, BoundedVec, RuntimeDebug};
use sp_runtime::traits::Bounded;
use sp_std::{fmt::Debug, prelude::*};

Expand All @@ -195,6 +195,9 @@ pub use sp_arithmetic;
#[doc(hidden)]
pub use sp_std;

pub mod weights;
pub use weights::WeightInfo;

#[cfg(test)]
mod mock;
#[cfg(test)]
Expand Down Expand Up @@ -523,6 +526,12 @@ pub trait NposSolver {
targets: Vec<Self::AccountId>,
voters: Vec<(Self::AccountId, VoteWeight, impl IntoIterator<Item = Self::AccountId>)>,
) -> Result<ElectionResult<Self::AccountId, Self::Accuracy>, Self::Error>;

/// Measure the weight used in the calculation of the solver.
/// - `voters` is the number of voters.
/// - `targets` is the number of targets.
/// - `vote_degree` is the degree ie the maximum numbers of votes per voter.
fn weight<T: WeightInfo>(voters: u32, targets: u32, vote_degree: u32) -> Weight;
}

/// A wrapper for [`sp_npos_elections::seq_phragmen`] that implements [`NposSolver`]. See the
Expand All @@ -547,6 +556,10 @@ impl<
) -> Result<ElectionResult<Self::AccountId, Self::Accuracy>, Self::Error> {
sp_npos_elections::seq_phragmen(winners, targets, voters, Balancing::get())
}

fn weight<T: WeightInfo>(voters: u32, targets: u32, vote_degree: u32) -> Weight {
T::phragmen(voters, targets, vote_degree)
}
}

/// A wrapper for [`sp_npos_elections::phragmms()`] that implements [`NposSolver`]. See the
Expand All @@ -571,6 +584,10 @@ impl<
) -> Result<ElectionResult<Self::AccountId, Self::Accuracy>, Self::Error> {
sp_npos_elections::phragmms(winners, targets, voters, Balancing::get())
}

fn weight<T: WeightInfo>(voters: u32, targets: u32, vote_degree: u32) -> Weight {
T::phragmms(voters, targets, vote_degree)
}
}

/// A voter, at the level of abstraction of this crate.
Expand Down
Loading