diff --git a/Cargo.lock b/Cargo.lock
index a87da6586cc6e..e33ca23d03092 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -539,6 +539,30 @@ dependencies = [
"walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
]
+[[package]]
+name = "criterion"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
+ "cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "criterion-plot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "csv 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rand_os 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rand_xoshiro 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rayon 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_derive 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)",
+ "tinytemplate 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
[[package]]
name = "criterion-plot"
version = "0.3.1"
@@ -549,6 +573,15 @@ dependencies = [
"itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
+[[package]]
+name = "criterion-plot"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
[[package]]
name = "crossbeam-channel"
version = "0.3.9"
@@ -3317,6 +3350,15 @@ dependencies = [
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
+[[package]]
+name = "rand_os"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "getrandom 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
[[package]]
name = "rand_pcg"
version = "0.1.2"
@@ -3343,6 +3385,14 @@ dependencies = [
"rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
+[[package]]
+name = "rand_xoshiro"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
[[package]]
name = "rayon"
version = "1.2.0"
@@ -3845,6 +3895,7 @@ dependencies = [
name = "sr-arithmetic"
version = "2.0.0"
dependencies = [
+ "criterion 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"integer-sqrt 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -6826,7 +6877,9 @@ dependencies = [
"checksum core-foundation-sys 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e7ca8a5221364ef15ce201e8ed2f609fc312682a8f4e0e3d4aa5879764e0fa3b"
"checksum crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1"
"checksum criterion 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "0363053954f3e679645fc443321ca128b7b950a6fe288cf5f9335cc22ee58394"
+"checksum criterion 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "938703e165481c8d612ea3479ac8342e5615185db37765162e762ec3523e2fc6"
"checksum criterion-plot 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "76f9212ddf2f4a9eb2d401635190600656a1f88a932ef53d06e7fa4c7e02fb8e"
+"checksum criterion-plot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "eccdc6ce8bbe352ca89025bee672aa6d24f4eb8c53e3a8b5d1bc58011da072a2"
"checksum crossbeam-channel 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c8ec7fcd21571dc78f96cc96243cab8d8f035247c3efd16c687be154c3fa9efa"
"checksum crossbeam-deque 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b18cd2e169ad86297e6bc0ad9aa679aee9daa4f19e8163860faf7c164e4f5a71"
"checksum crossbeam-epoch 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "fedcd6772e37f3da2a9af9bf12ebe046c0dfe657992377b4df982a2b54cd37a9"
@@ -7081,9 +7134,11 @@ dependencies = [
"checksum rand_isaac 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08"
"checksum rand_jitter 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1166d5c91dc97b88d1decc3285bb0a99ed84b05cfd0bc2341bdf2d43fc41e39b"
"checksum rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071"
+"checksum rand_os 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a788ae3edb696cfcba1c19bfd388cc4b8c21f8a408432b199c072825084da58a"
"checksum rand_pcg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44"
"checksum rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c"
"checksum rand_xoshiro 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "03b418169fb9c46533f326efd6eed2576699c44ca92d3052a066214a8d828929"
+"checksum rand_xoshiro 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0e18c91676f670f6f0312764c759405f13afb98d5d73819840cf72a518487bff"
"checksum rayon 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "83a27732a533a1be0a0035a111fe76db89ad312f6f0347004c220c57f209a123"
"checksum rayon-core 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "98dcf634205083b17d0861252431eb2acbfb698ab7478a2d20de07954f47ec7b"
"checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
diff --git a/core/primitives/Cargo.toml b/core/primitives/Cargo.toml
index 557ce55b88315..82f614be2b75b 100644
--- a/core/primitives/Cargo.toml
+++ b/core/primitives/Cargo.toml
@@ -43,7 +43,7 @@ rand = "0.7.2"
criterion = "0.2.11"
[[bench]]
-name = "benches"
+name = "bench"
harness = false
[lib]
diff --git a/core/primitives/benches/benches.rs b/core/primitives/benches/bench.rs
similarity index 100%
rename from core/primitives/benches/benches.rs
rename to core/primitives/benches/bench.rs
diff --git a/core/sr-arithmetic/Cargo.toml b/core/sr-arithmetic/Cargo.toml
index 3962daf493146..fd484671dd848 100644
--- a/core/sr-arithmetic/Cargo.toml
+++ b/core/sr-arithmetic/Cargo.toml
@@ -15,9 +15,9 @@ substrate-debug-derive = { path = "../primitives/debug-derive", default-features
[dev-dependencies]
primitive-types = "0.6.0"
rand = "0.7.2"
+criterion = "0.3"
[features]
-bench = []
default = ["std"]
std = [
"codec/std",
@@ -26,3 +26,7 @@ std = [
"serde",
"substrate-debug-derive/std",
]
+
+[[bench]]
+name = "bench"
+harness = false
diff --git a/core/sr-arithmetic/benches/bench.rs b/core/sr-arithmetic/benches/bench.rs
new file mode 100644
index 0000000000000..22c0ce6f566e9
--- /dev/null
+++ b/core/sr-arithmetic/benches/bench.rs
@@ -0,0 +1,80 @@
+// Copyright 2019 Parity Technologies (UK) Ltd.
+// This file is part of Substrate.
+
+// Substrate is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Substrate is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Substrate. If not, see .
+
+use criterion::{Criterion, Throughput, BenchmarkId, criterion_group, criterion_main};
+use sr_arithmetic::biguint::{BigUint, Single};
+use rand::Rng;
+
+fn random_big_uint(size: usize) -> BigUint {
+ let mut rng = rand::thread_rng();
+ let digits: Vec<_> = (0..size).map(|_| rng.gen_range(0, Single::max_value())).collect();
+ BigUint::from_limbs(&digits)
+}
+
+fn bench_op(c: &mut Criterion, name: &str, op: F) {
+ let mut group = c.benchmark_group(name);
+
+ for size in [2, 4, 6, 8, 10].iter() {
+ group.throughput(Throughput::Elements(*size));
+ group.bench_with_input(BenchmarkId::from_parameter(size), size, |bencher, &size| {
+ let a = random_big_uint(size as usize);
+ let b = random_big_uint(size as usize);
+
+ bencher.iter(|| op(&a, &b));
+ });
+ }
+}
+
+fn bench_addition(c: &mut Criterion) {
+ bench_op(c, "addition", |a, b| {
+ let _ = a.clone().add(&b);
+ });
+}
+
+fn bench_subtraction(c: &mut Criterion) {
+ bench_op(c, "subtraction", |a, b| {
+ let _ = a.clone().sub(&b);
+ });
+}
+
+fn bench_multiplication(c: &mut Criterion) {
+ bench_op(c, "multiplication", |a, b| {
+ let _ = a.clone().mul(&b);
+ });
+}
+
+fn bench_division(c: &mut Criterion) {
+ let mut group = c.benchmark_group("division");
+
+ for size in [4, 6, 8, 10].iter() {
+ group.throughput(Throughput::Elements(*size));
+ group.bench_with_input(BenchmarkId::from_parameter(size), size, |bencher, &size| {
+ let a = random_big_uint(size as usize);
+ let b = random_big_uint(rand::thread_rng().gen_range(2, size as usize));
+
+ bencher.iter(|| {
+ let _ = a.clone().div(&b, true);
+ });
+ });
+ }
+}
+
+criterion_group!{
+ name = benches;
+ config = Criterion::default();
+ targets = bench_addition, bench_subtraction, bench_multiplication, bench_division
+}
+criterion_main!(benches);
diff --git a/core/sr-arithmetic/src/biguint.rs b/core/sr-arithmetic/src/biguint.rs
index b7d93c2f0d3ca..cc2d13850e15b 100644
--- a/core/sr-arithmetic/src/biguint.rs
+++ b/core/sr-arithmetic/src/biguint.rs
@@ -561,8 +561,6 @@ impl From for BigUint {
#[cfg(test)]
pub mod tests {
use super::*;
- #[cfg(feature = "bench")]
- use test::Bencher;
fn with_limbs(n: usize) -> BigUint {
BigUint { digits: vec![1; n] }
@@ -734,82 +732,4 @@ pub mod tests {
assert_eq!(b.clone().div_unit(7), BigUint::from(((B + 100) / 7) as Single));
}
-
- #[cfg(feature = "bench")]
- fn random_big_uint(size: usize) -> BigUint {
- use rand::Rng;
- let mut rng = rand::thread_rng();
- let digits = (0..size).map(|_| rng.gen_range(0, Single::max_value())).collect();
- BigUint { digits }
- }
-
- #[cfg(feature = "bench")]
- #[bench]
- fn bench_addition_2_digit(bencher: &mut Bencher) {
- let a = random_big_uint(2);
- let b = random_big_uint(2);
- bencher.iter(|| {
- let _ = a.clone().add(&b);
- });
- }
-
- #[cfg(feature = "bench")]
- #[bench]
- fn bench_addition_4_digit(bencher: &mut Bencher) {
- let a = random_big_uint(4);
- let b = random_big_uint(4);
- bencher.iter(|| {
- let _ = a.clone().add(&b);
- });
- }
-
- #[cfg(feature = "bench")]
- #[bench]
- fn bench_subtraction_2_digit(bencher: &mut Bencher) {
- let a = random_big_uint(2);
- let b = random_big_uint(2);
- bencher.iter(|| {
- let _ = a.clone().sub(&b);
- });
- }
-
- #[cfg(feature = "bench")]
- #[bench]
- fn bench_subtraction_4_digit(bencher: &mut Bencher) {
- let a = random_big_uint(4);
- let b = random_big_uint(4);
- bencher.iter(|| {
- let _ = a.clone().sub(&b);
- });
- }
-
- #[cfg(feature = "bench")]
- #[bench]
- fn bench_multiplication_2_digit(bencher: &mut Bencher) {
- let a = random_big_uint(2);
- let b = random_big_uint(2);
- bencher.iter(|| {
- let _ = a.clone().mul(&b);
- });
- }
-
- #[cfg(feature = "bench")]
- #[bench]
- fn bench_multiplication_4_digit(bencher: &mut Bencher) {
- let a = random_big_uint(4);
- let b = random_big_uint(4);
- bencher.iter(|| {
- let _ = a.clone().mul(&b);
- });
- }
-
- #[cfg(feature = "bench")]
- #[bench]
- fn bench_division_4_digit(bencher: &mut Bencher) {
- let a = random_big_uint(4);
- let b = random_big_uint(2);
- bencher.iter(|| {
- let _ = a.clone().div(&b, true);
- });
- }
}
diff --git a/core/sr-arithmetic/src/lib.rs b/core/sr-arithmetic/src/lib.rs
index 847ca9e797cb0..7b285002e5400 100644
--- a/core/sr-arithmetic/src/lib.rs
+++ b/core/sr-arithmetic/src/lib.rs
@@ -18,10 +18,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
-// to allow benchmarking
-#![cfg_attr(feature = "bench", feature(test))]
-#[cfg(feature = "bench")] extern crate test;
-
/// Copied from `sr-primitives` and documented there.
#[cfg(test)]
macro_rules! assert_eq_error_rate {