Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate remaining benchmarks to Criterion #1490

Merged
merged 19 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/benches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ jobs:
- name: Clippy
run: cargo clippy --all-targets -- -D warnings
- name: Build
run: RUSTFLAGS=-Dwarnings cargo build --all-targets
run: RUSTFLAGS=-Dwarnings cargo test --benches
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
- Add `IndexedRandom::choose_multiple_array`, `index::sample_array` (#1453, #1469)
- Bump the MSRV to 1.61.0
- Rename `Rng::gen` to `Rng::random` to avoid conflict with the new `gen` keyword in Rust 2024 (#1435)
- Move all benchmarks to new `benches` crate (#1439)
- Move all benchmarks to new `benches` crate (#1439) and migrate to Criterion (#1490)
- Annotate panicking methods with `#[track_caller]` (#1442, #1447)
- Enable feature `small_rng` by default (#1455)
- Allow `UniformFloat::new` samples and `UniformFloat::sample_single` to yield `high` (#1462)
Expand Down
27 changes: 21 additions & 6 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,42 @@ rand_distr = { path = "../rand_distr" }
criterion = "0.5"
criterion-cycles-per-byte = "0.6"

[[bench]]
name = "array"
harness = false

[[bench]]
name = "bool"
harness = false

[[bench]]
name = "distr"
path = "src/distr.rs"
harness = false

[[bench]]
name = "uniform"
path = "src/uniform.rs"
name = "generators"
harness = false

[[bench]]
name = "seq_choose"
path = "src/seq_choose.rs"
harness = false

[[bench]]
name = "shuffle"
path = "src/shuffle.rs"
harness = false

[[bench]]
name = "standard"
harness = false

[[bench]]
name = "uniform"
harness = false

[[bench]]
name = "uniform_float"
path = "src/uniform_float.rs"
harness = false

[[bench]]
name = "weighted"
harness = false
94 changes: 94 additions & 0 deletions benches/benches/array.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2018-2023 Developers of the Rand project.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Generating/filling arrays and iterators of output

use criterion::{criterion_group, criterion_main, Criterion};
use rand::distr::Standard;
use rand::prelude::*;
use rand_pcg::Pcg64Mcg;

criterion_group!(
name = benches;
config = Criterion::default();
targets = bench
);
criterion_main!(benches);

pub fn bench(c: &mut Criterion) {
let mut g = c.benchmark_group("gen_1kb");
g.throughput(criterion::Throughput::Bytes(1024));

g.bench_function("u16_iter_repeat", |b| {
use core::iter;
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
b.iter(|| {
let v: Vec<u16> = iter::repeat(()).map(|()| rng.random()).take(512).collect();
v
});
});

g.bench_function("u16_sample_iter", |b| {
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
b.iter(|| {
let v: Vec<u16> = Standard.sample_iter(&mut rng).take(512).collect();
v
});
});

g.bench_function("u16_gen_array", |b| {
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
b.iter(|| {
let v: [u16; 512] = rng.random();
v
});
});

g.bench_function("u16_fill", |b| {
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
let mut buf = [0u16; 512];
b.iter(|| {
rng.fill(&mut buf[..]);
buf
});
});

g.bench_function("u64_iter_repeat", |b| {
use core::iter;
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
b.iter(|| {
let v: Vec<u64> = iter::repeat(()).map(|()| rng.random()).take(128).collect();
v
});
});

g.bench_function("u64_sample_iter", |b| {
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
b.iter(|| {
let v: Vec<u64> = Standard.sample_iter(&mut rng).take(128).collect();
v
});
});

g.bench_function("u64_gen_array", |b| {
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
b.iter(|| {
let v: [u64; 128] = rng.random();
v
});
});

g.bench_function("u64_fill", |b| {
let mut rng = Pcg64Mcg::from_rng(&mut thread_rng());
let mut buf = [0u64; 128];
b.iter(|| {
rng.fill(&mut buf[..]);
buf
});
});
}
Loading