Skip to content

Commit 9e030aa

Browse files
authored
Migrate remaining benchmarks to Criterion (#1490)
Translate everything still using the old test harness to Criterion.
1 parent 79f1b0f commit 9e030aa

20 files changed

+621
-1033
lines changed

.github/workflows/benches.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ jobs:
2323
- name: Clippy
2424
run: cargo clippy --all-targets -- -D warnings
2525
- name: Build
26-
run: RUSTFLAGS=-Dwarnings cargo build --all-targets
26+
run: RUSTFLAGS=-Dwarnings cargo test --benches

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
1313
- Add `IndexedRandom::choose_multiple_array`, `index::sample_array` (#1453, #1469)
1414
- Bump the MSRV to 1.61.0
1515
- Rename `Rng::gen` to `Rng::random` to avoid conflict with the new `gen` keyword in Rust 2024 (#1435)
16-
- Move all benchmarks to new `benches` crate (#1439)
16+
- Move all benchmarks to new `benches` crate (#1439) and migrate to Criterion (#1490)
1717
- Annotate panicking methods with `#[track_caller]` (#1442, #1447)
1818
- Enable feature `small_rng` by default (#1455)
1919
- Allow `UniformFloat::new` samples and `UniformFloat::sample_single` to yield `high` (#1462)

benches/Cargo.toml

+21-6
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,42 @@ rand_distr = { path = "../rand_distr" }
1414
criterion = "0.5"
1515
criterion-cycles-per-byte = "0.6"
1616

17+
[[bench]]
18+
name = "array"
19+
harness = false
20+
21+
[[bench]]
22+
name = "bool"
23+
harness = false
24+
1725
[[bench]]
1826
name = "distr"
19-
path = "src/distr.rs"
2027
harness = false
2128

2229
[[bench]]
23-
name = "uniform"
24-
path = "src/uniform.rs"
30+
name = "generators"
2531
harness = false
2632

2733
[[bench]]
2834
name = "seq_choose"
29-
path = "src/seq_choose.rs"
3035
harness = false
3136

3237
[[bench]]
3338
name = "shuffle"
34-
path = "src/shuffle.rs"
39+
harness = false
40+
41+
[[bench]]
42+
name = "standard"
43+
harness = false
44+
45+
[[bench]]
46+
name = "uniform"
3547
harness = false
3648

3749
[[bench]]
3850
name = "uniform_float"
39-
path = "src/uniform_float.rs"
51+
harness = false
52+
53+
[[bench]]
54+
name = "weighted"
4055
harness = false

benches/benches/array.rs

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright 2018-2023 Developers of the Rand project.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
//! Generating/filling arrays and iterators of output
10+
11+
use criterion::{criterion_group, criterion_main, Criterion};
12+
use rand::distr::Standard;
13+
use rand::prelude::*;
14+
use rand_pcg::Pcg64Mcg;
15+
16+
criterion_group!(
17+
name = benches;
18+
config = Criterion::default();
19+
targets = bench
20+
);
21+
criterion_main!(benches);
22+
23+
pub fn bench(c: &mut Criterion) {
24+
let mut g = c.benchmark_group("gen_1kb");
25+
g.throughput(criterion::Throughput::Bytes(1024));
26+
27+
g.bench_function("u16_iter_repeat", |b| {
28+
use core::iter;
29+
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
30+
b.iter(|| {
31+
let v: Vec<u16> = iter::repeat(()).map(|()| rng.random()).take(512).collect();
32+
v
33+
});
34+
});
35+
36+
g.bench_function("u16_sample_iter", |b| {
37+
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
38+
b.iter(|| {
39+
let v: Vec<u16> = Standard.sample_iter(&mut rng).take(512).collect();
40+
v
41+
});
42+
});
43+
44+
g.bench_function("u16_gen_array", |b| {
45+
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
46+
b.iter(|| {
47+
let v: [u16; 512] = rng.random();
48+
v
49+
});
50+
});
51+
52+
g.bench_function("u16_fill", |b| {
53+
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
54+
let mut buf = [0u16; 512];
55+
b.iter(|| {
56+
rng.fill(&mut buf[..]);
57+
buf
58+
});
59+
});
60+
61+
g.bench_function("u64_iter_repeat", |b| {
62+
use core::iter;
63+
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
64+
b.iter(|| {
65+
let v: Vec<u64> = iter::repeat(()).map(|()| rng.random()).take(128).collect();
66+
v
67+
});
68+
});
69+
70+
g.bench_function("u64_sample_iter", |b| {
71+
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
72+
b.iter(|| {
73+
let v: Vec<u64> = Standard.sample_iter(&mut rng).take(128).collect();
74+
v
75+
});
76+
});
77+
78+
g.bench_function("u64_gen_array", |b| {
79+
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
80+
b.iter(|| {
81+
let v: [u64; 128] = rng.random();
82+
v
83+
});
84+
});
85+
86+
g.bench_function("u64_fill", |b| {
87+
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
88+
let mut buf = [0u64; 128];
89+
b.iter(|| {
90+
rng.fill(&mut buf[..]);
91+
buf
92+
});
93+
});
94+
}

0 commit comments

Comments
 (0)