referrerpolicy=no-referrer-when-downgrade

pallet_election_provider_support_benchmarking/
inner.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Election provider support pallet benchmarking.
19//! This is separated into its own crate to avoid bloating the size of the runtime.
20
21use 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	// fill targets.
42	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	// fill voters.
49	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		// Number of votes in snapshot.
66		v: Linear<{ VOTERS[0] }, { VOTERS[1] }>,
67		// Number of targets in snapshot.
68		t: Linear<{ TARGETS[0] }, { TARGETS[1] }>,
69		// Number of votes per voter (ie the degree).
70		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		// Number of votes in snapshot.
86		v: Linear<{ VOTERS[0] }, { VOTERS[1] }>,
87		// Number of targets in snapshot.
88		t: Linear<{ TARGETS[0] }, { TARGETS[1] }>,
89		// Number of votes per voter (ie the degree).
90		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}