Skip to content

Commit

Permalink
Migrate pallet-election-provider-support-benchmarking benchmark to v2 (
Browse files Browse the repository at this point in the history
…#6315)

Part of:

- #6202.

---------

Co-authored-by: GitHub Action <[email protected]>
Co-authored-by: Guillaume Thiolliere <[email protected]>
Co-authored-by: Giuseppe Re <[email protected]>
Co-authored-by: Guillaume Thiolliere <[email protected]>
  • Loading branch information
5 people authored Nov 7, 2024
1 parent 27bf54b commit 1d351bf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 34 deletions.
8 changes: 8 additions & 0 deletions prdoc/pr_6315.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
title: Migrate pallet-election-provider-support-benchmarking benchmark to v2
doc:
- audience: Runtime Dev
description: |-
Migrate pallet-election-provider-support-benchmarking benchmark to v2
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>);

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());
}
}

0 comments on commit 1d351bf

Please sign in to comment.