Move phragmen benchmarks out of Staking (#3588)

* Move phragmen benches to.. phragmen.

* Move some basic phragmen tests to.. phragmen.

* Line-width

* Add phragmen equ implementation as flot

* Add phragmen equ implementation as flot

* Add mock and test file.
This commit is contained in:
Kian Paimani
2019-09-13 08:41:33 +02:00
committed by GitHub
parent 7b87cd206b
commit a6b5d1d155
10 changed files with 661 additions and 374 deletions
-186
View File
@@ -1,186 +0,0 @@
// Copyright 2019 Parity Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Benchmarks of the phragmen election algorithm.
//! Note that execution times will not be accurate in an absolute scale, since
//! - Everything is executed in the context of `TestExternalities`
//! - Everything is executed in native environment.
//!
//! Run using:
//!
//! ```zsh
//! cargo bench --features bench --color always
//! ```
use test::Bencher;
use runtime_io::with_externalities;
use mock::*;
use super::*;
use phragmen;
use rand::{self, Rng};
const VALIDATORS: u64 = 1000;
const NOMINATORS: u64 = 10_000;
const EDGES: u64 = 2;
const TO_ELECT: usize = 100;
const STAKE: u64 = 1000;
type C<T> = <T as Trait>::CurrencyToVote;
fn do_phragmen(
b: &mut Bencher,
num_vals: u64,
num_noms: u64,
count: usize,
votes_per: u64,
eq_iters: usize,
_eq_tolerance: u128,
) {
with_externalities(&mut ExtBuilder::default().nominate(false).build(), || {
assert!(num_vals > votes_per);
let rr = |a, b| rand::thread_rng().gen_range(a as usize, b as usize) as u64;
// prefix to distinguish the validator and nominator account ranges.
let np = 10_000;
(1 ..= 2*num_vals)
.step_by(2)
.for_each(|acc| bond_validator(acc, STAKE + rr(10, 50)));
(np ..= (np + 2*num_noms))
.step_by(2)
.for_each(|acc| {
let mut stashes_to_vote = (1 ..= 2*num_vals)
.step_by(2)
.map(|ctrl| ctrl + 1)
.collect::<Vec<AccountId>>();
let votes = (0 .. votes_per)
.map(|_| {
stashes_to_vote.remove(rr(0, stashes_to_vote.len()) as usize)
})
.collect::<Vec<AccountId>>();
bond_nominator(acc, STAKE + rr(10, 50), votes);
});
b.iter(|| {
let r = phragmen::elect::<_, _, _, <Test as Trait>::CurrencyToVote>(
count,
1_usize,
<Validators<Test>>::enumerate().map(|(who, _)| who).collect::<Vec<u64>>(),
<Nominators<Test>>::enumerate().collect(),
Staking::slashable_balance_of,
true,
).unwrap();
// Do the benchmarking with equalize.
if eq_iters > 0 {
let elected_stashes = r.winners;
let mut assignments = r.assignments;
let to_votes = |b: Balance|
<C<Test> as Convert<Balance, AccountId>>::convert(b) as ExtendedBalance;
let ratio_of = |b, r: ExtendedBalance| r.saturating_mul(to_votes(b)) / ACCURACY;
// Initialize the support of each candidate.
let mut supports = <SupportMap<u64>>::new();
elected_stashes
.iter()
.map(|e| (e, to_votes(Staking::slashable_balance_of(e))))
.for_each(|(e, s)| {
let item = Support { own: s, total: s, ..Default::default() };
supports.insert(e.clone(), item);
});
for (n, assignment) in assignments.iter_mut() {
for (c, r) in assignment.iter_mut() {
let nominator_stake = Staking::slashable_balance_of(n);
let other_stake = ratio_of(nominator_stake, *r);
if let Some(support) = supports.get_mut(c) {
support.total = support.total.saturating_add(other_stake);
support.others.push((n.clone(), other_stake));
}
*r = other_stake;
}
}
let tolerance = 0_u128;
let iterations = 2_usize;
phragmen::equalize::<_, _, <Test as Trait>::CurrencyToVote, _>(
assignments,
&mut supports,
tolerance,
iterations,
Staking::slashable_balance_of,
);
}
})
})
}
macro_rules! phragmen_benches {
($($name:ident: $tup:expr,)*) => {
$(
#[bench]
fn $name(b: &mut Bencher) {
let (v, n, t, e, eq_iter, eq_tol) = $tup;
println!("");
println!(
r#"
++ Benchmark: {} Validators // {} Nominators // {} Edges-per-nominator // {} total edges //
electing {} // Equalize: {} iterations -- {} tolerance"#,
v, n, e, e * n, t, eq_iter, eq_tol,
);
do_phragmen(b, v, n, t, e, eq_iter, eq_tol);
}
)*
}
}
phragmen_benches! {
bench_1_1: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES, 0, 0),
bench_1_2: (VALIDATORS*2, NOMINATORS, TO_ELECT, EDGES, 0, 0),
bench_1_3: (VALIDATORS*4, NOMINATORS, TO_ELECT, EDGES, 0, 0),
bench_1_4: (VALIDATORS*8, NOMINATORS, TO_ELECT, EDGES, 0, 0),
bench_1_1_eq: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES, 2, 0),
bench_1_2_eq: (VALIDATORS*2, NOMINATORS, TO_ELECT, EDGES, 2, 0),
bench_1_3_eq: (VALIDATORS*4, NOMINATORS, TO_ELECT, EDGES, 2, 0),
bench_1_4_eq: (VALIDATORS*8, NOMINATORS, TO_ELECT, EDGES, 2, 0),
bench_0_1: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES, 0, 0),
bench_0_2: (VALIDATORS, NOMINATORS, TO_ELECT * 4, EDGES, 0, 0),
bench_0_3: (VALIDATORS, NOMINATORS, TO_ELECT * 8, EDGES, 0, 0),
bench_0_4: (VALIDATORS, NOMINATORS, TO_ELECT * 16, EDGES , 0, 0),
bench_0_1_eq: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES, 2, 0),
bench_0_2_eq: (VALIDATORS, NOMINATORS, TO_ELECT * 4, EDGES, 2, 0),
bench_0_3_eq: (VALIDATORS, NOMINATORS, TO_ELECT * 8, EDGES, 2, 0),
bench_0_4_eq: (VALIDATORS, NOMINATORS, TO_ELECT * 16, EDGES , 2, 0),
bench_2_1: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES, 0, 0),
bench_2_2: (VALIDATORS, NOMINATORS*2, TO_ELECT, EDGES, 0, 0),
bench_2_3: (VALIDATORS, NOMINATORS*4, TO_ELECT, EDGES, 0, 0),
bench_2_4: (VALIDATORS, NOMINATORS*8, TO_ELECT, EDGES, 0, 0),
bench_2_1_eq: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES, 2, 0),
bench_2_2_eq: (VALIDATORS, NOMINATORS*2, TO_ELECT, EDGES, 2, 0),
bench_2_3_eq: (VALIDATORS, NOMINATORS*4, TO_ELECT, EDGES, 2, 0),
bench_2_4_eq: (VALIDATORS, NOMINATORS*8, TO_ELECT, EDGES, 2, 0),
bench_3_1: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES, 0, 0 ),
bench_3_2: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES*2, 0, 0),
bench_3_3: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES*4, 0, 0),
bench_3_4: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES*8, 0, 0),
bench_3_1_eq: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES, 2, 0),
bench_3_2_eq: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES*2, 2, 0),
bench_3_3_eq: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES*4, 2, 0),
bench_3_4_eq: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES*8, 2, 0),
}
+2 -10
View File
@@ -243,22 +243,14 @@
#![recursion_limit="128"]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(all(feature = "bench", test), feature(test))]
#[cfg(all(feature = "bench", test))]
extern crate test;
#[cfg(any(feature = "bench", test))]
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
pub mod inflation;
#[cfg(all(feature = "bench", test))]
mod benches;
use rstd::{prelude::*, result};
use codec::{HasCompact, Encode, Decode};
use support::{
@@ -1295,7 +1287,7 @@ impl<T: Trait> Module<T> {
if cfg!(feature = "equalize") {
let tolerance = 0_u128;
let iterations = 2_usize;
equalize::<_, _, T::CurrencyToVote, _>(
equalize::<_, _, _, T::CurrencyToVote>(
assignments,
&mut supports,
tolerance,
+1 -53
View File
@@ -1307,7 +1307,7 @@ fn phragmen_poc_works() {
// 40 has load 0 and supported
// 40 with stake 0
// Sequential Phragmén with post processing gives
// Sequential Phragmén with post processing gives
// 10 is elected with stake 1500.0 and score 0.0005
// 20 is elected with stake 1500.0 and score 0.00075
//
@@ -1410,58 +1410,6 @@ fn phragmen_poc_works() {
});
}
#[test]
fn phragmen_poc_2_works() {
// tests the encapsulated phragmen::elect function.
// Votes [
// ('10', 1000, ['10']),
// ('20', 1000, ['20']),
// ('30', 1000, ['30']),
// ('2', 50, ['10', '20']),
// ('4', 1000, ['10', '30'])
// ]
// Sequential Phragmén gives
// 10 is elected with stake 1705.7377049180327 and score 0.0004878048780487805
// 30 is elected with stake 1344.2622950819673 and score 0.0007439024390243903
with_externalities(&mut ExtBuilder::default().nominate(false).build(), || {
// initial setup of 10 and 20, both validators
assert_eq_uvec!(validator_controllers(), vec![20, 10]);
// Bond [30, 31] as the third validator
assert_ok!(Staking::bond_extra(Origin::signed(31), 999));
assert_ok!(Staking::validate(Origin::signed(30), ValidatorPrefs::default()));
// bond [2,1](A), [4,3](B), as 2 nominators
for i in &[1, 3] { let _ = Balances::deposit_creating(i, 2000); }
assert_ok!(Staking::bond(Origin::signed(1), 2, 50, RewardDestination::default()));
assert_ok!(Staking::nominate(Origin::signed(2), vec![11, 21]));
assert_ok!(Staking::bond(Origin::signed(3), 4, 1000, RewardDestination::default()));
assert_ok!(Staking::nominate(Origin::signed(4), vec![11, 31]));
let results = phragmen::elect::<_, _, _, <Test as Trait>::CurrencyToVote>(
2,
Staking::minimum_validator_count() as usize,
<Validators<Test>>::enumerate().map(|(who, _)| who).collect::<Vec<u64>>(),
<Nominators<Test>>::enumerate().collect(),
Staking::slashable_balance_of,
true,
);
let phragmen::PhragmenResult { winners, assignments } = results.unwrap();
// 10 and 30 must be the winners
assert_eq!(winners, vec![11, 31]);
assert_eq!(assignments, vec![
(3, vec![(11, 2816371998), (31, 1478595298)]),
(1, vec![(11, 4294967296)]),
]);
check_exposure_all();
check_nominator_all();
})
}
#[test]
fn switching_roles() {
// Test that it should be possible to switch between roles (nominator, validator, idle) with minimal overhead.