pallet_election_provider_support_benchmarking/
inner.rs1use alloc::vec::Vec;
22use codec::Decode;
23use frame_benchmarking::v2::*;
24use frame_election_provider_support::{NposSolver, PhragMMS, SequentialPhragmen};
25use sp_runtime::Perbill;
26
27const VOTERS: [u32; 2] = [1_000, 2_000];
28const TARGETS: [u32; 2] = [500, 1_000];
29const VOTES_PER_VOTER: [u32; 2] = [5, 16];
30const SEED: u32 = 999;
31
32pub trait Config: frame_system::Config {}
33
34pub struct Pallet<T: Config>(frame_system::Pallet<T>);
35
36fn set_up_voters_targets<AccountId: Decode + Clone>(
37 voters_len: u32,
38 targets_len: u32,
39 degree: usize,
40) -> (Vec<(AccountId, u64, impl Clone + IntoIterator<Item = AccountId>)>, Vec<AccountId>) {
41 let mut targets = (0..targets_len)
43 .map(|i| frame_benchmarking::account::<AccountId>("Target", i, SEED))
44 .collect::<Vec<_>>();
45 assert!(targets.len() > degree, "we should always have enough voters to fill");
46 targets.truncate(degree);
47
48 let voters = (0..voters_len)
50 .map(|i| {
51 let voter = frame_benchmarking::account::<AccountId>("Voter", i, SEED);
52 (voter, 1_000, targets.clone())
53 })
54 .collect::<Vec<_>>();
55
56 (voters, targets)
57}
58
59#[benchmarks]
60mod benchmarks {
61 use super::*;
62
63 #[benchmark]
64 fn phragmen(
65 v: Linear<{ VOTERS[0] }, { VOTERS[1] }>,
67 t: Linear<{ TARGETS[0] }, { TARGETS[1] }>,
69 d: Linear<{ VOTES_PER_VOTER[0] }, { VOTES_PER_VOTER[1] }>,
71 ) {
72 let (voters, targets) = set_up_voters_targets::<T::AccountId>(v, t, d as _);
73 let result;
74
75 #[block]
76 {
77 result = SequentialPhragmen::<T::AccountId, Perbill>::solve(d as _, targets, voters);
78 }
79
80 assert!(result.is_ok());
81 }
82
83 #[benchmark]
84 fn phragmms(
85 v: Linear<{ VOTERS[0] }, { VOTERS[1] }>,
87 t: Linear<{ TARGETS[0] }, { TARGETS[1] }>,
89 d: Linear<{ VOTES_PER_VOTER[0] }, { VOTES_PER_VOTER[1] }>,
91 ) {
92 let (voters, targets) = set_up_voters_targets::<T::AccountId>(v, t, d as _);
93 let result;
94
95 #[block]
96 {
97 result = PhragMMS::<T::AccountId, Perbill>::solve(d as _, targets, voters);
98 }
99
100 assert!(result.is_ok());
101 }
102}