| 1 |
//! Tests every benchmarking loop combination in `Bencher`. When run under Miri, |
| 2 |
//! this catches memory leaks and UB in `unsafe` code. |
| 3 |
|
| 4 |
use std::{ |
| 5 |
collections::HashSet, |
| 6 |
sync::atomic::{AtomicUsize, Ordering::SeqCst}, |
| 7 |
}; |
| 8 |
|
| 9 |
use util::defer; |
| 10 |
|
| 11 |
use super::*; |
| 12 |
use crate::{ |
| 13 |
config::Action, |
| 14 |
time::{Timer, TimerKind}, |
| 15 |
}; |
| 16 |
|
| 17 |
// We use a small number of runs because Miri is very slow. |
| 18 |
const SAMPLE_COUNT: u32 = 3; |
| 19 |
|
| 20 |
const SAMPLE_SIZE: u32 = 2; |
| 21 |
|
| 22 |
// Tests `SAMPLE_COUNT` by including it in the middle and having higher numbers |
| 23 |
// where `SAMPLE_COUNT % n != 0`. |
| 24 |
const THREAD_COUNTS: &[usize] = if cfg!(miri) { |
| 25 |
// Speed up Miri tests while still catching UB/memory issues. |
| 26 |
&[1, 2] |
| 27 |
} else { |
| 28 |
// Exhaustively test expectations. |
| 29 |
// |
| 30 |
// Tests `SAMPLE_COUNT` by: |
| 31 |
// - Including it in the middle |
| 32 |
// - Having numbers where `SAMPLE_COUNT % n` varies |
| 33 |
&[1, 2, 3, 4, 5, 6, 9] |
| 34 |
}; |
| 35 |
|
| 36 |
#[track_caller] |
| 37 |
fn test_bencher(test: &mut dyn FnMut(Bencher)) { |
| 38 |
// Silence Miri about leaking threads. |
| 39 |
let _drop_threads = defer(|| BENCH_POOL.drop_threads()); |
| 40 |
|
| 41 |
let bench_options = BenchOptions { |
| 42 |
sample_count: Some(SAMPLE_COUNT), |
| 43 |
sample_size: Some(SAMPLE_SIZE), |
| 44 |
..BenchOptions::default() |
| 45 |
}; |
| 46 |
|
| 47 |
for timer in Timer::available() { |
| 48 |
for action in [Action::Bench, Action::Test] { |
| 49 |
let shared_context = SharedContext { action, timer }; |
| 50 |
|
| 51 |
for &thread_count in THREAD_COUNTS { |
| 52 |
let mut bench_context = BenchContext::new( |
| 53 |
&shared_context, |
| 54 |
&bench_options, |
| 55 |
NonZeroUsize::new(thread_count).unwrap(), |
| 56 |
); |
| 57 |
|
| 58 |
test(Bencher::new(&mut bench_context)); |
| 59 |
|
| 60 |
assert!(bench_context.did_run); |
| 61 |
|
| 62 |
let samples = &bench_context.samples; |
| 63 |
|
| 64 |
// '--test' should run the expected number of times but not |
| 65 |
// allocate any samples. |
| 66 |
if action.is_test() { |
| 67 |
assert_eq!(samples.time_samples.capacity(), 0); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
fn make_string() -> String { |
| 75 |
('a'..='z').collect() |
| 76 |
} |
| 77 |
|
| 78 |
/// Tests that the benchmarked function runs the expected number of times when |
| 79 |
/// running either in benchmark or test mode. |
| 80 |
/// |
| 81 |
/// Tests operate over all input/output combinations of: |
| 82 |
/// - `()` |
| 83 |
/// - `i32` |
| 84 |
/// - `String` |
| 85 |
/// - Zero sized type (ZST) that implements `Drop` |
| 86 |
/// |
| 87 |
/// This ensures that any special handling of `size_of` or `needs_drop` does not |
| 88 |
/// affect the number of runs. |
| 89 |
#[allow(clippy::unused_unit)] |
| 90 |
mod run_count { |
| 91 |
use super::*; |
| 92 |
|
| 93 |
fn test(run_bench: fn(Bencher, &(dyn Fn() + Sync))) { |
| 94 |
test_with_drop_counter(&AtomicUsize::new(usize::MAX), run_bench); |
| 95 |
} |
| 96 |
|
| 97 |
fn test_with_drop_counter( |
| 98 |
drop_count: &AtomicUsize, |
| 99 |
run_bench: fn(Bencher, &(dyn Fn() + Sync)), |
| 100 |
) { |
| 101 |
let test_drop_count = drop_count.load(SeqCst) != usize::MAX; |
| 102 |
|
| 103 |
let bench_count = AtomicUsize::new(0); |
| 104 |
let test_count = AtomicUsize::new(0); |
| 105 |
|
| 106 |
let mut thread_counts = HashSet::<u32>::new(); |
| 107 |
let mut timer_os = false; |
| 108 |
let mut timer_tsc = false; |
| 109 |
|
| 110 |
test_bencher(&mut |bencher| { |
| 111 |
let context = &bencher.context; |
| 112 |
|
| 113 |
let thread_count = context.thread_count.get(); |
| 114 |
thread_counts.insert(thread_count as u32); |
| 115 |
|
| 116 |
match context.shared_context.timer.kind() { |
| 117 |
TimerKind::Os => timer_os = true, |
| 118 |
TimerKind::Tsc => timer_tsc = true, |
| 119 |
} |
| 120 |
|
| 121 |
let is_test = context.shared_context.action.is_test(); |
| 122 |
|
| 123 |
let shared_run_count = if is_test { &test_count } else { &bench_count }; |
| 124 |
let start_run_count = shared_run_count.load(SeqCst); |
| 125 |
|
| 126 |
run_bench(bencher, &|| { |
| 127 |
shared_run_count.fetch_add(1, SeqCst); |
| 128 |
}); |
| 129 |
|
| 130 |
let end_run_count = shared_run_count.load(SeqCst); |
| 131 |
let run_count = end_run_count - start_run_count; |
| 132 |
|
| 133 |
if is_test { |
| 134 |
assert_eq!(run_count, thread_count); |
| 135 |
} else { |
| 136 |
let expected_samples = match SAMPLE_COUNT as usize % thread_count { |
| 137 |
0 => SAMPLE_COUNT, |
| 138 |
rem => SAMPLE_COUNT + (thread_count - rem) as u32, |
| 139 |
}; |
| 140 |
|
| 141 |
let expected_iters = (expected_samples * SAMPLE_SIZE) as usize; |
| 142 |
assert_eq!(run_count, expected_iters); |
| 143 |
} |
| 144 |
}); |
| 145 |
|
| 146 |
let thread_count = thread_counts.into_iter().sum::<u32>(); |
| 147 |
|
| 148 |
let timer_count = timer_os as u32 + timer_tsc as u32; |
| 149 |
let bench_count = bench_count.into_inner() as u32; |
| 150 |
let test_count = test_count.into_inner() as u32; |
| 151 |
|
| 152 |
let total_count = bench_count + test_count; |
| 153 |
assert_ne!(total_count, 0); |
| 154 |
|
| 155 |
// The drop count should equal the total run count. |
| 156 |
if test_drop_count { |
| 157 |
assert_eq!(drop_count.load(SeqCst), total_count as usize); |
| 158 |
} |
| 159 |
|
| 160 |
assert_eq!(test_count, timer_count * thread_count); |
| 161 |
} |
| 162 |
|
| 163 |
#[test] |
| 164 |
fn bench() { |
| 165 |
struct DroppedZst; |
| 166 |
|
| 167 |
static ZST_DROP_COUNT: AtomicUsize = AtomicUsize::new(0); |
| 168 |
|
| 169 |
impl Drop for DroppedZst { |
| 170 |
fn drop(&mut self) { |
| 171 |
ZST_DROP_COUNT.fetch_add(1, SeqCst); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
// `()` out. |
| 176 |
test(|b, f| b.bench(f)); |
| 177 |
|
| 178 |
// `i32` out. |
| 179 |
test(|b, f| { |
| 180 |
b.bench(|| -> i32 { |
| 181 |
f(); |
| 182 |
100i32 |
| 183 |
}) |
| 184 |
}); |
| 185 |
|
| 186 |
// `String` out. |
| 187 |
test(|b, f| { |
| 188 |
b.bench(|| -> String { |
| 189 |
f(); |
| 190 |
make_string() |
| 191 |
}) |
| 192 |
}); |
| 193 |
|
| 194 |
// `DroppedZst` out. |
| 195 |
test_with_drop_counter(&ZST_DROP_COUNT, |b, f| { |
| 196 |
b.bench(|| -> DroppedZst { |
| 197 |
f(); |
| 198 |
DroppedZst |
| 199 |
}) |
| 200 |
}); |
| 201 |
} |
| 202 |
|
| 203 |
#[test] |
| 204 |
fn bench_values() { |
| 205 |
struct DroppedZst; |
| 206 |
|
| 207 |
static ZST_DROP_COUNT: AtomicUsize = AtomicUsize::new(0); |
| 208 |
|
| 209 |
impl Drop for DroppedZst { |
| 210 |
fn drop(&mut self) { |
| 211 |
ZST_DROP_COUNT.fetch_add(1, SeqCst); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
let test_zst_drop = |run_bench| { |
| 216 |
ZST_DROP_COUNT.store(0, SeqCst); |
| 217 |
test_with_drop_counter(&ZST_DROP_COUNT, run_bench); |
| 218 |
}; |
| 219 |
|
| 220 |
// `()` in, `()` out. |
| 221 |
test(|b, f| b.with_inputs(|| ()).bench_values(|_: ()| -> () { f() })); |
| 222 |
|
| 223 |
// `()` in, `i32` out. |
| 224 |
test(|b, f| { |
| 225 |
b.with_inputs(|| ()).bench_values(|_: ()| -> i32 { |
| 226 |
f(); |
| 227 |
100i32 |
| 228 |
}) |
| 229 |
}); |
| 230 |
|
| 231 |
// `()` in, `String` out. |
| 232 |
test(|b, f| { |
| 233 |
b.with_inputs(|| ()).bench_values(|_: ()| -> String { |
| 234 |
f(); |
| 235 |
make_string() |
| 236 |
}) |
| 237 |
}); |
| 238 |
|
| 239 |
// `()` in, `DroppedZst` out. |
| 240 |
test_zst_drop(|b, f| { |
| 241 |
b.with_inputs(|| ()).bench_values(|_: ()| -> DroppedZst { |
| 242 |
f(); |
| 243 |
DroppedZst |
| 244 |
}) |
| 245 |
}); |
| 246 |
|
| 247 |
// `i32` in, `()` out. |
| 248 |
test(|b, f| b.with_inputs(|| 100i32).bench_values(|_: i32| -> () { f() })); |
| 249 |
|
| 250 |
// `i32` in, `i32` out. |
| 251 |
test(|b, f| { |
| 252 |
b.with_inputs(|| 100i32).bench_values(|value: i32| -> i32 { |
| 253 |
f(); |
| 254 |
value |
| 255 |
}) |
| 256 |
}); |
| 257 |
|
| 258 |
// `i32` in, `String` out. |
| 259 |
test(|b, f| { |
| 260 |
b.with_inputs(|| 100i32).bench_values(|_: i32| -> String { |
| 261 |
f(); |
| 262 |
make_string() |
| 263 |
}) |
| 264 |
}); |
| 265 |
|
| 266 |
// `i32` in, `DroppedZst` out. |
| 267 |
test_zst_drop(|b, f| { |
| 268 |
b.with_inputs(|| 100i32).bench_values(|_: i32| -> DroppedZst { |
| 269 |
f(); |
| 270 |
DroppedZst |
| 271 |
}) |
| 272 |
}); |
| 273 |
|
| 274 |
// `String` in, `()` out. |
| 275 |
test(|b, f| b.with_inputs(make_string).bench_values(|_: String| -> () { f() })); |
| 276 |
|
| 277 |
// `String` in, `i32` out. |
| 278 |
test(|b, f| { |
| 279 |
b.with_inputs(make_string).bench_values(|_: String| -> i32 { |
| 280 |
f(); |
| 281 |
100i32 |
| 282 |
}) |
| 283 |
}); |
| 284 |
|
| 285 |
// `String` in, `String` out. |
| 286 |
test(|b, f| { |
| 287 |
b.with_inputs(make_string).bench_values(|value: String| -> String { |
| 288 |
f(); |
| 289 |
value |
| 290 |
}) |
| 291 |
}); |
| 292 |
|
| 293 |
// `String` in, `DroppedZst` out. |
| 294 |
test_zst_drop(|b, f| { |
| 295 |
b.with_inputs(make_string).bench_values(|_: String| -> DroppedZst { |
| 296 |
f(); |
| 297 |
DroppedZst |
| 298 |
}) |
| 299 |
}); |
| 300 |
|
| 301 |
// `DroppedZst` in, `()` out. |
| 302 |
test_zst_drop(|b, f| { |
| 303 |
b.with_inputs(|| DroppedZst).bench_values(|_: DroppedZst| -> () { f() }) |
| 304 |
}); |
| 305 |
|
| 306 |
// `DroppedZst` in, `i32` out. |
| 307 |
test_zst_drop(|b, f| { |
| 308 |
b.with_inputs(|| DroppedZst).bench_values(|_: DroppedZst| -> i32 { |
| 309 |
f(); |
| 310 |
100i32 |
| 311 |
}) |
| 312 |
}); |
| 313 |
|
| 314 |
// `DroppedZst` in, `String` out. |
| 315 |
test_zst_drop(|b, f| { |
| 316 |
b.with_inputs(|| DroppedZst).bench_values(|_: DroppedZst| -> String { |
| 317 |
f(); |
| 318 |
make_string() |
| 319 |
}) |
| 320 |
}); |
| 321 |
|
| 322 |
// `DroppedZst` in, `DroppedZst` out. |
| 323 |
test_zst_drop(|b, f| { |
| 324 |
b.with_inputs(|| DroppedZst).bench_values(|value: DroppedZst| -> DroppedZst { |
| 325 |
f(); |
| 326 |
value |
| 327 |
}) |
| 328 |
}); |
| 329 |
} |
| 330 |
|
| 331 |
#[test] |
| 332 |
fn bench_refs() { |
| 333 |
struct DroppedZst; |
| 334 |
|
| 335 |
static ZST_DROP_COUNT: AtomicUsize = AtomicUsize::new(0); |
| 336 |
|
| 337 |
impl Drop for DroppedZst { |
| 338 |
fn drop(&mut self) { |
| 339 |
ZST_DROP_COUNT.fetch_add(1, SeqCst); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
let test_zst_drop = |run_bench| { |
| 344 |
ZST_DROP_COUNT.store(0, SeqCst); |
| 345 |
test_with_drop_counter(&ZST_DROP_COUNT, run_bench); |
| 346 |
}; |
| 347 |
|
| 348 |
// `&mut ()` in, `()` out. |
| 349 |
test(|b, f| b.with_inputs(|| ()).bench_refs(|_: &mut ()| -> () { f() })); |
| 350 |
|
| 351 |
// `&mut ()` in, `i32` out. |
| 352 |
test(|b, f| { |
| 353 |
b.with_inputs(|| ()).bench_refs(|_: &mut ()| -> i32 { |
| 354 |
f(); |
| 355 |
100i32 |
| 356 |
}) |
| 357 |
}); |
| 358 |
|
| 359 |
// `&mut ()` in, `String` out. |
| 360 |
test(|b, f| { |
| 361 |
b.with_inputs(|| ()).bench_refs(|_: &mut ()| -> String { |
| 362 |
f(); |
| 363 |
make_string() |
| 364 |
}) |
| 365 |
}); |
| 366 |
|
| 367 |
// `&mut ()` in, `DroppedZst` out. |
| 368 |
test_zst_drop(|b, f| { |
| 369 |
b.with_inputs(|| ()).bench_refs(|_: &mut ()| -> DroppedZst { |
| 370 |
f(); |
| 371 |
DroppedZst |
| 372 |
}) |
| 373 |
}); |
| 374 |
|
| 375 |
// `&mut i32` in, `()` out. |
| 376 |
test(|b, f| b.with_inputs(|| 100i32).bench_refs(|_: &mut i32| -> () { f() })); |
| 377 |
|
| 378 |
// `&mut i32` in, `i32` out. |
| 379 |
test(|b, f| { |
| 380 |
b.with_inputs(|| 100i32).bench_refs(|value: &mut i32| -> i32 { |
| 381 |
f(); |
| 382 |
*value |
| 383 |
}) |
| 384 |
}); |
| 385 |
|
| 386 |
// `&mut i32` in, `String` out. |
| 387 |
test(|b, f| { |
| 388 |
b.with_inputs(|| 100i32).bench_refs(|_: &mut i32| -> String { |
| 389 |
f(); |
| 390 |
make_string() |
| 391 |
}) |
| 392 |
}); |
| 393 |
|
| 394 |
// `&mut i32` in, `DroppedZst` out. |
| 395 |
test_zst_drop(|b, f| { |
| 396 |
b.with_inputs(|| 100i32).bench_refs(|_: &mut i32| -> DroppedZst { |
| 397 |
f(); |
| 398 |
DroppedZst |
| 399 |
}) |
| 400 |
}); |
| 401 |
|
| 402 |
// `&mut String` in, `()` out. |
| 403 |
test(|b, f| b.with_inputs(make_string).bench_refs(|_: &mut String| -> () { f() })); |
| 404 |
|
| 405 |
// `&mut String` in, `i32` out. |
| 406 |
test(|b, f| { |
| 407 |
b.with_inputs(make_string).bench_refs(|_: &mut String| -> i32 { |
| 408 |
f(); |
| 409 |
100i32 |
| 410 |
}) |
| 411 |
}); |
| 412 |
|
| 413 |
// `&mut String` in, `String` out. |
| 414 |
test(|b, f| { |
| 415 |
b.with_inputs(make_string).bench_refs(|value: &mut String| -> String { |
| 416 |
f(); |
| 417 |
value.clone() |
| 418 |
}) |
| 419 |
}); |
| 420 |
|
| 421 |
// `&mut String` in, `DroppedZst` out. |
| 422 |
test_zst_drop(|b, f| { |
| 423 |
b.with_inputs(make_string).bench_refs(|_: &mut String| -> DroppedZst { |
| 424 |
f(); |
| 425 |
DroppedZst |
| 426 |
}) |
| 427 |
}); |
| 428 |
|
| 429 |
// `&mut DroppedZst` in, `()` out. |
| 430 |
test_zst_drop(|b, f| { |
| 431 |
b.with_inputs(|| DroppedZst).bench_refs(|_: &mut DroppedZst| -> () { f() }) |
| 432 |
}); |
| 433 |
|
| 434 |
// `&mut DroppedZst` in, `i32` out. |
| 435 |
test_zst_drop(|b, f| { |
| 436 |
b.with_inputs(|| DroppedZst).bench_refs(|_: &mut DroppedZst| -> i32 { |
| 437 |
f(); |
| 438 |
100i32 |
| 439 |
}) |
| 440 |
}); |
| 441 |
|
| 442 |
// `&mut DroppedZst` in, `String` out. |
| 443 |
test_zst_drop(|b, f| { |
| 444 |
b.with_inputs(|| DroppedZst).bench_refs(|_: &mut DroppedZst| -> String { |
| 445 |
f(); |
| 446 |
make_string() |
| 447 |
}) |
| 448 |
}); |
| 449 |
|
| 450 |
// `&mut DroppedZst` in, `DroppedZst` out. |
| 451 |
test_zst_drop(|b, f| { |
| 452 |
b.with_inputs(|| { |
| 453 |
// Adjust counter for input ZST. |
| 454 |
ZST_DROP_COUNT.fetch_sub(1, SeqCst); |
| 455 |
|
| 456 |
DroppedZst |
| 457 |
}) |
| 458 |
.bench_refs(|_: &mut DroppedZst| -> DroppedZst { |
| 459 |
f(); |
| 460 |
DroppedZst |
| 461 |
}) |
| 462 |
}); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
mod no_input { |
| 467 |
use super::*; |
| 468 |
|
| 469 |
#[test] |
| 470 |
fn string_output() { |
| 471 |
test_bencher(&mut |b| b.bench(make_string)); |
| 472 |
} |
| 473 |
|
| 474 |
#[test] |
| 475 |
fn no_output() { |
| 476 |
test_bencher(&mut |b| b.bench(|| black_box_drop(make_string()))); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
mod string_input { |
| 481 |
use super::*; |
| 482 |
|
| 483 |
#[test] |
| 484 |
fn string_output() { |
| 485 |
test_bencher(&mut |b| b.with_inputs(make_string).bench_values(|s| s.to_ascii_uppercase())); |
| 486 |
} |
| 487 |
|
| 488 |
#[test] |
| 489 |
fn no_output() { |
| 490 |
test_bencher(&mut |b| b.with_inputs(make_string).bench_refs(|s| s.make_ascii_uppercase())); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
mod zst_input { |
| 495 |
use super::*; |
| 496 |
|
| 497 |
#[test] |
| 498 |
fn zst_output() { |
| 499 |
struct DroppedZst; |
| 500 |
|
| 501 |
// Each test has its own `ZST_COUNT` global because tests are run |
| 502 |
// independently in parallel. |
| 503 |
static ZST_COUNT: AtomicUsize = AtomicUsize::new(0); |
| 504 |
|
| 505 |
impl Drop for DroppedZst { |
| 506 |
fn drop(&mut self) { |
| 507 |
ZST_COUNT.fetch_sub(1, SeqCst); |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
test_bencher(&mut |b| { |
| 512 |
b.with_inputs(|| { |
| 513 |
ZST_COUNT.fetch_add(1, SeqCst); |
| 514 |
DroppedZst |
| 515 |
}) |
| 516 |
.bench_values(black_box); |
| 517 |
}); |
| 518 |
|
| 519 |
assert_eq!(ZST_COUNT.load(SeqCst), 0); |
| 520 |
} |
| 521 |
|
| 522 |
#[test] |
| 523 |
fn no_output() { |
| 524 |
struct DroppedZst; |
| 525 |
|
| 526 |
static ZST_COUNT: AtomicUsize = AtomicUsize::new(0); |
| 527 |
|
| 528 |
impl Drop for DroppedZst { |
| 529 |
fn drop(&mut self) { |
| 530 |
ZST_COUNT.fetch_sub(1, SeqCst); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
test_bencher(&mut |b| { |
| 535 |
b.with_inputs(|| { |
| 536 |
ZST_COUNT.fetch_add(1, SeqCst); |
| 537 |
DroppedZst |
| 538 |
}) |
| 539 |
.bench_values(drop); |
| 540 |
}); |
| 541 |
|
| 542 |
assert_eq!(ZST_COUNT.load(SeqCst), 0); |
| 543 |
} |
| 544 |
} |