Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 23c1956

Browse files
expensesbkchr
authored andcommitted
Switch sr-arithmetic benchmarking to criterion (#3902)
* Change DefaultMaxDepth from 1024 to 32 * Switch sr-arithmetic benchmarking to criterion * Update core/sr-arithmetic/Cargo.toml Co-Authored-By: Bastian Köcher <[email protected]> * Update core/sr-arithmetic/benches/bench.rs Co-Authored-By: Bastian Köcher <[email protected]> * Test on variable limb lengths * Change license * Rework division
1 parent 43961e8 commit 23c1956

File tree

7 files changed

+87
-86
lines changed

7 files changed

+87
-86
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/primitives/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ rand = "0.7.2"
4545
criterion = "0.2.11"
4646

4747
[[bench]]
48-
name = "benches"
48+
name = "bench"
4949
harness = false
5050

5151
[lib]

core/sr-arithmetic/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ substrate-debug-derive = { path = "../primitives/debug-derive", default-features
1515
[dev-dependencies]
1616
primitive-types = "0.6.0"
1717
rand = "0.7.2"
18+
criterion = "0.3"
1819

1920
[features]
20-
bench = []
2121
default = ["std"]
2222
std = [
2323
"codec/std",
@@ -26,3 +26,7 @@ std = [
2626
"serde",
2727
"substrate-debug-derive/std",
2828
]
29+
30+
[[bench]]
31+
name = "bench"
32+
harness = false
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2019 Parity Technologies (UK) Ltd.
2+
// This file is part of Substrate.
3+
4+
// Substrate is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Substrate is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
16+
17+
use criterion::{Criterion, Throughput, BenchmarkId, criterion_group, criterion_main};
18+
use sr_arithmetic::biguint::{BigUint, Single};
19+
use rand::Rng;
20+
21+
fn random_big_uint(size: usize) -> BigUint {
22+
let mut rng = rand::thread_rng();
23+
let digits: Vec<_> = (0..size).map(|_| rng.gen_range(0, Single::max_value())).collect();
24+
BigUint::from_limbs(&digits)
25+
}
26+
27+
fn bench_op<F: Fn(&BigUint, &BigUint)>(c: &mut Criterion, name: &str, op: F) {
28+
let mut group = c.benchmark_group(name);
29+
30+
for size in [2, 4, 6, 8, 10].iter() {
31+
group.throughput(Throughput::Elements(*size));
32+
group.bench_with_input(BenchmarkId::from_parameter(size), size, |bencher, &size| {
33+
let a = random_big_uint(size as usize);
34+
let b = random_big_uint(size as usize);
35+
36+
bencher.iter(|| op(&a, &b));
37+
});
38+
}
39+
}
40+
41+
fn bench_addition(c: &mut Criterion) {
42+
bench_op(c, "addition", |a, b| {
43+
let _ = a.clone().add(&b);
44+
});
45+
}
46+
47+
fn bench_subtraction(c: &mut Criterion) {
48+
bench_op(c, "subtraction", |a, b| {
49+
let _ = a.clone().sub(&b);
50+
});
51+
}
52+
53+
fn bench_multiplication(c: &mut Criterion) {
54+
bench_op(c, "multiplication", |a, b| {
55+
let _ = a.clone().mul(&b);
56+
});
57+
}
58+
59+
fn bench_division(c: &mut Criterion) {
60+
let mut group = c.benchmark_group("division");
61+
62+
for size in [4, 6, 8, 10].iter() {
63+
group.throughput(Throughput::Elements(*size));
64+
group.bench_with_input(BenchmarkId::from_parameter(size), size, |bencher, &size| {
65+
let a = random_big_uint(size as usize);
66+
let b = random_big_uint(rand::thread_rng().gen_range(2, size as usize));
67+
68+
bencher.iter(|| {
69+
let _ = a.clone().div(&b, true);
70+
});
71+
});
72+
}
73+
}
74+
75+
criterion_group!{
76+
name = benches;
77+
config = Criterion::default();
78+
targets = bench_addition, bench_subtraction, bench_multiplication, bench_division
79+
}
80+
criterion_main!(benches);

core/sr-arithmetic/src/biguint.rs

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,6 @@ impl From<Double> for BigUint {
561561
#[cfg(test)]
562562
pub mod tests {
563563
use super::*;
564-
#[cfg(feature = "bench")]
565-
use test::Bencher;
566564

567565
fn with_limbs(n: usize) -> BigUint {
568566
BigUint { digits: vec![1; n] }
@@ -734,82 +732,4 @@ pub mod tests {
734732
assert_eq!(b.clone().div_unit(7), BigUint::from(((B + 100) / 7) as Single));
735733

736734
}
737-
738-
#[cfg(feature = "bench")]
739-
fn random_big_uint(size: usize) -> BigUint {
740-
use rand::Rng;
741-
let mut rng = rand::thread_rng();
742-
let digits = (0..size).map(|_| rng.gen_range(0, Single::max_value())).collect();
743-
BigUint { digits }
744-
}
745-
746-
#[cfg(feature = "bench")]
747-
#[bench]
748-
fn bench_addition_2_digit(bencher: &mut Bencher) {
749-
let a = random_big_uint(2);
750-
let b = random_big_uint(2);
751-
bencher.iter(|| {
752-
let _ = a.clone().add(&b);
753-
});
754-
}
755-
756-
#[cfg(feature = "bench")]
757-
#[bench]
758-
fn bench_addition_4_digit(bencher: &mut Bencher) {
759-
let a = random_big_uint(4);
760-
let b = random_big_uint(4);
761-
bencher.iter(|| {
762-
let _ = a.clone().add(&b);
763-
});
764-
}
765-
766-
#[cfg(feature = "bench")]
767-
#[bench]
768-
fn bench_subtraction_2_digit(bencher: &mut Bencher) {
769-
let a = random_big_uint(2);
770-
let b = random_big_uint(2);
771-
bencher.iter(|| {
772-
let _ = a.clone().sub(&b);
773-
});
774-
}
775-
776-
#[cfg(feature = "bench")]
777-
#[bench]
778-
fn bench_subtraction_4_digit(bencher: &mut Bencher) {
779-
let a = random_big_uint(4);
780-
let b = random_big_uint(4);
781-
bencher.iter(|| {
782-
let _ = a.clone().sub(&b);
783-
});
784-
}
785-
786-
#[cfg(feature = "bench")]
787-
#[bench]
788-
fn bench_multiplication_2_digit(bencher: &mut Bencher) {
789-
let a = random_big_uint(2);
790-
let b = random_big_uint(2);
791-
bencher.iter(|| {
792-
let _ = a.clone().mul(&b);
793-
});
794-
}
795-
796-
#[cfg(feature = "bench")]
797-
#[bench]
798-
fn bench_multiplication_4_digit(bencher: &mut Bencher) {
799-
let a = random_big_uint(4);
800-
let b = random_big_uint(4);
801-
bencher.iter(|| {
802-
let _ = a.clone().mul(&b);
803-
});
804-
}
805-
806-
#[cfg(feature = "bench")]
807-
#[bench]
808-
fn bench_division_4_digit(bencher: &mut Bencher) {
809-
let a = random_big_uint(4);
810-
let b = random_big_uint(2);
811-
bencher.iter(|| {
812-
let _ = a.clone().div(&b, true);
813-
});
814-
}
815735
}

core/sr-arithmetic/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818
1919
#![cfg_attr(not(feature = "std"), no_std)]
2020

21-
// to allow benchmarking
22-
#![cfg_attr(feature = "bench", feature(test))]
23-
#[cfg(feature = "bench")] extern crate test;
24-
2521
/// Copied from `sr-primitives` and documented there.
2622
#[cfg(test)]
2723
macro_rules! assert_eq_error_rate {

0 commit comments

Comments
 (0)