From 1d351bf46109a110a60befd4d2924da8eeaa2f0a Mon Sep 17 00:00:00 2001 From: Xavier Lau <x@acg.box> Date: Thu, 7 Nov 2024 16:05:09 +0800 Subject: [PATCH] Migrate pallet-election-provider-support-benchmarking benchmark to v2 (#6315) Part of: - #6202. --------- Co-authored-by: GitHub Action <action@github.com> Co-authored-by: Guillaume Thiolliere <guillaume.thiolliere@parity.io> Co-authored-by: Giuseppe Re <giuseppe.re@parity.io> Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com> --- prdoc/pr_6315.prdoc | 8 ++ .../benchmarking/src/inner.rs | 81 +++++++++++-------- 2 files changed, 55 insertions(+), 34 deletions(-) create mode 100644 prdoc/pr_6315.prdoc diff --git a/prdoc/pr_6315.prdoc b/prdoc/pr_6315.prdoc new file mode 100644 index 00000000000..6fc070b43b3 --- /dev/null +++ b/prdoc/pr_6315.prdoc @@ -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 diff --git a/substrate/frame/election-provider-support/benchmarking/src/inner.rs b/substrate/frame/election-provider-support/benchmarking/src/inner.rs index 8cca0d459ea..7fb8c1bdb72 100644 --- a/substrate/frame/election-provider-support/benchmarking/src/inner.rs +++ b/substrate/frame/election-provider-support/benchmarking/src/inner.rs @@ -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, @@ -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()); } } -- GitLab