Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate pallet-election-provider-support-benchmarking benchmark to v2 #6315

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
10 changes: 10 additions & 0 deletions prdoc/pr_6315.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
title: Migrate pallet-election-provider-support-benchmarking benchmark to v2
doc:
- audience: Runtime Dev
description: |-
Part of:

- #6202.
gui1117 marked this conversation as resolved.
Show resolved Hide resolved
crates:
- name: pallet-election-provider-support-benchmarking
bump: patch
81 changes: 47 additions & 34 deletions substrate/frame/election-provider-support/benchmarking/src/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@

use alloc::vec::Vec;
use codec::Decode;
use frame_benchmarking::v1::benchmarks;
use frame_benchmarking::v2::*;
use frame_election_provider_support::{NposSolver, PhragMMS, SequentialPhragmen};

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

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;

pub trait Config: frame_system::Config {}

pub struct Pallet<T: Config>(frame_system::Pallet<T>);
gui1117 marked this conversation as resolved.
Show resolved Hide resolved

fn set_up_voters_targets<AccountId: Decode + Clone>(
voters_len: u32,
targets_len: u32,
Expand All @@ -54,36 +56,47 @@ fn set_up_voters_targets<AccountId: Decode + Clone>(
(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()
);
#[benchmarks]
mod benchmarks {
use super::*;

#[benchmark]
fn phragmen(
// Number of votes in snapshot.
v: Linear<{ VOTERS[0] }, { VOTERS[1] }>,
// Number of targets in snapshot.
t: Linear<{ TARGETS[0] }, { TARGETS[1] }>,
// Number of votes per voter (ie the degree).
d: Linear<{ VOTES_PER_VOTER[0] }, { VOTES_PER_VOTER[1] }>,
) {
let (voters, targets) = set_up_voters_targets::<T::AccountId>(v, t, d as _);
let result;

#[block]
{
result = SequentialPhragmen::<T::AccountId, Perbill>::solve(d as _, targets, voters);
}

assert!(result.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()
);
#[benchmark]
fn phragmms(
// Number of votes in snapshot.
v: Linear<{ VOTERS[0] }, { VOTERS[1] }>,
// Number of targets in snapshot.
t: Linear<{ TARGETS[0] }, { TARGETS[1] }>,
// Number of votes per voter (ie the degree).
d: Linear<{ VOTES_PER_VOTER[0] }, { VOTES_PER_VOTER[1] }>,
) {
let (voters, targets) = set_up_voters_targets::<T::AccountId>(v, t, d as _);
let result;

#[block]
{
result = PhragMMS::<T::AccountId, Perbill>::solve(d as _, targets, voters);
}

assert!(result.is_ok());
}
}