Skip to content

Move test::Bencher to a new (unstable) std::bench module #66290

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

Closed
wants to merge 2 commits into from
Closed
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 src/liballoc/alloc/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn allocate_zeroed() {

#[bench]
#[cfg(not(miri))] // Miri does not support benchmarks
fn alloc_owned_small(b: &mut Bencher) {
fn alloc_owned_small(b: &mut Bencher<'_>) {
b.iter(|| {
let _: Box<_> = box 10;
})
Expand Down
8 changes: 4 additions & 4 deletions src/liballoc/collections/vec_deque/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ::test;

#[bench]
#[cfg(not(miri))] // Miri does not support benchmarks
fn bench_push_back_100(b: &mut test::Bencher) {
fn bench_push_back_100(b: &mut test::Bencher<'_>) {
let mut deq = VecDeque::with_capacity(101);
b.iter(|| {
for i in 0..100 {
Expand All @@ -17,7 +17,7 @@ fn bench_push_back_100(b: &mut test::Bencher) {

#[bench]
#[cfg(not(miri))] // Miri does not support benchmarks
fn bench_push_front_100(b: &mut test::Bencher) {
fn bench_push_front_100(b: &mut test::Bencher<'_>) {
let mut deq = VecDeque::with_capacity(101);
b.iter(|| {
for i in 0..100 {
Expand All @@ -30,7 +30,7 @@ fn bench_push_front_100(b: &mut test::Bencher) {

#[bench]
#[cfg(not(miri))] // Miri does not support benchmarks
fn bench_pop_back_100(b: &mut test::Bencher) {
fn bench_pop_back_100(b: &mut test::Bencher<'_>) {
let mut deq = VecDeque::<i32>::with_capacity(101);

b.iter(|| {
Expand All @@ -44,7 +44,7 @@ fn bench_pop_back_100(b: &mut test::Bencher) {

#[bench]
#[cfg(not(miri))] // Miri does not support benchmarks
fn bench_pop_front_100(b: &mut test::Bencher) {
fn bench_pop_front_100(b: &mut test::Bencher<'_>) {
let mut deq = VecDeque::<i32>::with_capacity(101);

b.iter(|| {
Expand Down
10 changes: 5 additions & 5 deletions src/libarena/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ pub fn test_copy() {
}

#[bench]
pub fn bench_copy(b: &mut Bencher) {
pub fn bench_copy(b: &mut Bencher<'_>) {
let arena = TypedArena::default();
b.iter(|| arena.alloc(Point { x: 1, y: 2, z: 3 }))
}

#[bench]
pub fn bench_copy_nonarena(b: &mut Bencher) {
pub fn bench_copy_nonarena(b: &mut Bencher<'_>) {
b.iter(|| {
let _: Box<_> = Box::new(Point { x: 1, y: 2, z: 3 });
})
Expand Down Expand Up @@ -118,7 +118,7 @@ pub fn test_typed_arena_clear() {
}

#[bench]
pub fn bench_typed_arena_clear(b: &mut Bencher) {
pub fn bench_typed_arena_clear(b: &mut Bencher<'_>) {
let mut arena = TypedArena::default();
b.iter(|| {
arena.alloc(Point { x: 1, y: 2, z: 3 });
Expand Down Expand Up @@ -192,7 +192,7 @@ fn test_typed_arena_drop_small_count() {
}

#[bench]
pub fn bench_noncopy(b: &mut Bencher) {
pub fn bench_noncopy(b: &mut Bencher<'_>) {
let arena = TypedArena::default();
b.iter(|| {
arena.alloc(Noncopy {
Expand All @@ -203,7 +203,7 @@ pub fn bench_noncopy(b: &mut Bencher) {
}

#[bench]
pub fn bench_noncopy_nonarena(b: &mut Bencher) {
pub fn bench_noncopy_nonarena(b: &mut Bencher<'_>) {
b.iter(|| {
let _: Box<_> = Box::new(Noncopy {
string: "hello world".to_string(),
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/benches/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Trait for Struct {
}

#[bench]
fn trait_vtable_method_call(b: &mut Bencher) {
fn trait_vtable_method_call(b: &mut Bencher<'_>) {
let s = Struct { field: 10 };
let t = &s as &Trait;
b.iter(|| {
Expand All @@ -26,7 +26,7 @@ fn trait_vtable_method_call(b: &mut Bencher) {
}

#[bench]
fn trait_static_method_call(b: &mut Bencher) {
fn trait_static_method_call(b: &mut Bencher<'_>) {
let s = Struct { field: 10 };
b.iter(|| {
s.method()
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/benches/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use test::Bencher;
// Overhead of various match forms

#[bench]
fn option_some(b: &mut Bencher) {
fn option_some(b: &mut Bencher<'_>) {
let x = Some(10);
b.iter(|| {
match x {
Expand All @@ -14,7 +14,7 @@ fn option_some(b: &mut Bencher) {
}

#[bench]
fn vec_pattern(b: &mut Bencher) {
fn vec_pattern(b: &mut Bencher<'_>) {
let x = [1,2,3,4,5,6];
b.iter(|| {
match x {
Expand Down
16 changes: 8 additions & 8 deletions src/librustc_data_structures/tiny_list/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn test_remove_single() {
}

#[bench]
fn bench_insert_empty(b: &mut Bencher) {
fn bench_insert_empty(b: &mut Bencher<'_>) {
b.iter(|| {
let mut list = black_box(TinyList::new());
list.insert(1);
Expand All @@ -105,7 +105,7 @@ fn bench_insert_empty(b: &mut Bencher) {
}

#[bench]
fn bench_insert_one(b: &mut Bencher) {
fn bench_insert_one(b: &mut Bencher<'_>) {
b.iter(|| {
let mut list = black_box(TinyList::new_single(0));
list.insert(1);
Expand All @@ -114,42 +114,42 @@ fn bench_insert_one(b: &mut Bencher) {
}

#[bench]
fn bench_contains_empty(b: &mut Bencher) {
fn bench_contains_empty(b: &mut Bencher<'_>) {
b.iter(|| {
black_box(TinyList::new()).contains(&1)
});
}

#[bench]
fn bench_contains_unknown(b: &mut Bencher) {
fn bench_contains_unknown(b: &mut Bencher<'_>) {
b.iter(|| {
black_box(TinyList::new_single(0)).contains(&1)
});
}

#[bench]
fn bench_contains_one(b: &mut Bencher) {
fn bench_contains_one(b: &mut Bencher<'_>) {
b.iter(|| {
black_box(TinyList::new_single(1)).contains(&1)
});
}

#[bench]
fn bench_remove_empty(b: &mut Bencher) {
fn bench_remove_empty(b: &mut Bencher<'_>) {
b.iter(|| {
black_box(TinyList::new()).remove(&1)
});
}

#[bench]
fn bench_remove_unknown(b: &mut Bencher) {
fn bench_remove_unknown(b: &mut Bencher<'_>) {
b.iter(|| {
black_box(TinyList::new_single(0)).remove(&1)
});
}

#[bench]
fn bench_remove_one(b: &mut Bencher) {
fn bench_remove_one(b: &mut Bencher<'_>) {
b.iter(|| {
black_box(TinyList::new_single(1)).remove(&1)
});
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_index/bit_set/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ fn sparse_matrix_iter() {

/// Merge dense hybrid set into empty sparse hybrid set.
#[bench]
fn union_hybrid_sparse_empty_to_dense(b: &mut Bencher) {
fn union_hybrid_sparse_empty_to_dense(b: &mut Bencher<'_>) {
let mut pre_dense: HybridBitSet<usize> = HybridBitSet::new_empty(256);
for i in 0..10 {
assert!(pre_dense.insert(i));
Expand All @@ -301,7 +301,7 @@ fn union_hybrid_sparse_empty_to_dense(b: &mut Bencher) {

/// Merge dense hybrid set into full hybrid set with same indices.
#[bench]
fn union_hybrid_sparse_full_to_dense(b: &mut Bencher) {
fn union_hybrid_sparse_full_to_dense(b: &mut Bencher<'_>) {
let mut pre_dense: HybridBitSet<usize> = HybridBitSet::new_empty(256);
for i in 0..10 {
assert!(pre_dense.insert(i));
Expand All @@ -319,7 +319,7 @@ fn union_hybrid_sparse_full_to_dense(b: &mut Bencher) {

/// Merge dense hybrid set into full hybrid set with indices over the whole domain.
#[bench]
fn union_hybrid_sparse_domain_to_dense(b: &mut Bencher) {
fn union_hybrid_sparse_domain_to_dense(b: &mut Bencher<'_>) {
let mut pre_dense: HybridBitSet<usize> = HybridBitSet::new_empty(SPARSE_MAX*64);
for i in 0..10 {
assert!(pre_dense.insert(i));
Expand All @@ -337,7 +337,7 @@ fn union_hybrid_sparse_domain_to_dense(b: &mut Bencher) {

/// Merge dense hybrid set into empty hybrid set where the domain is very small.
#[bench]
fn union_hybrid_sparse_empty_small_domain(b: &mut Bencher) {
fn union_hybrid_sparse_empty_small_domain(b: &mut Bencher<'_>) {
let mut pre_dense: HybridBitSet<usize> = HybridBitSet::new_empty(SPARSE_MAX);
for i in 0..SPARSE_MAX {
assert!(pre_dense.insert(i));
Expand All @@ -352,7 +352,7 @@ fn union_hybrid_sparse_empty_small_domain(b: &mut Bencher) {

/// Merge dense hybrid set into full hybrid set where the domain is very small.
#[bench]
fn union_hybrid_sparse_full_small_domain(b: &mut Bencher) {
fn union_hybrid_sparse_full_small_domain(b: &mut Bencher<'_>) {
let mut pre_dense: HybridBitSet<usize> = HybridBitSet::new_empty(SPARSE_MAX);
for i in 0..SPARSE_MAX {
assert!(pre_dense.insert(i));
Expand Down
4 changes: 2 additions & 2 deletions src/libserialize/hex/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn test_from_hex_all_bytes() {
}

#[bench]
pub fn bench_to_hex(b: &mut Bencher) {
pub fn bench_to_hex(b: &mut Bencher<'_>) {
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
b.iter(|| {
Expand All @@ -63,7 +63,7 @@ pub fn bench_to_hex(b: &mut Bencher) {
}

#[bench]
pub fn bench_from_hex(b: &mut Bencher) {
pub fn bench_from_hex(b: &mut Bencher<'_>) {
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
let sb = s.as_bytes().to_hex();
Expand Down
8 changes: 4 additions & 4 deletions src/libserialize/json/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn test_stack() {
}

#[bench]
fn bench_streaming_small(b: &mut Bencher) {
fn bench_streaming_small(b: &mut Bencher<'_>) {
b.iter( || {
let mut parser = Parser::new(
r#"{
Expand All @@ -91,7 +91,7 @@ fn bench_streaming_small(b: &mut Bencher) {
});
}
#[bench]
fn bench_small(b: &mut Bencher) {
fn bench_small(b: &mut Bencher<'_>) {
b.iter( || {
let _ = from_str(r#"{
"a": 1.0,
Expand All @@ -115,7 +115,7 @@ fn big_json() -> string::String {
}

#[bench]
fn bench_streaming_large(b: &mut Bencher) {
fn bench_streaming_large(b: &mut Bencher<'_>) {
let src = big_json();
b.iter( || {
let mut parser = Parser::new(src.chars());
Expand All @@ -128,7 +128,7 @@ fn bench_streaming_large(b: &mut Bencher) {
});
}
#[bench]
fn bench_large(b: &mut Bencher) {
fn bench_large(b: &mut Bencher<'_>) {
let src = big_json();
b.iter( || { let _ = from_str(&src); });
}
55 changes: 55 additions & 0 deletions src/libstd/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//! Benchmarking

#![unstable(feature = "test", issue = "50297")]

use crate::hint::black_box;
use crate::time::{Instant, Duration};


/// Manager of the benchmarking runs.
///
/// This is fed into functions marked with `#[bench]` to allow for
/// set-up & tear-down before running a piece of code repeatedly via a
/// call to `iter`.
#[allow(missing_debug_implementations)]
pub struct Bencher<'a> {
callback: Callback<'a>,
/// FIXME
pub bytes: u64,
}

impl<'a> Bencher<'a> {
/// Callback for benchmark functions to run in their body.
pub fn iter<T>(&mut self, mut f: impl FnMut() -> T) {
(self.callback)(self.bytes, &mut |n_iterations| {
let start = Instant::now();
for _ in 0..n_iterations {
black_box(f());
}
ns_from_dur(start.elapsed())
})
}
}

fn ns_from_dur(dur: Duration) -> u64 {
dur.as_secs() * 1_000_000_000 + (dur.subsec_nanos() as u64)
}

// Permanently-unstable implementation details, only public for use by the `test` crate:

/// n_iterations -> nanoseconds
#[doc(hidden)]
#[unstable(feature = "test_internals", issue = "0")]
pub type TimeIterations<'i> = &'i mut dyn FnMut(u64) -> u64;

#[doc(hidden)]
#[unstable(feature = "test_internals", issue = "0")]
pub type Callback<'a> = &'a mut dyn FnMut(/* bytes: */ u64, TimeIterations<'_>);

#[doc(hidden)]
#[unstable(feature = "test_internals", issue = "0")]
impl<'a> Bencher<'a> {
pub fn new(callback: Callback<'a>) -> Self {
Self { callback, bytes: 0 }
}
}
Loading