Skip to content

Commit 5e72aea

Browse files
committed
chore: add sleep benchmarks
1 parent 165ea0b commit 5e72aea

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

crates/criterion_compat/benches/criterion_integration/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub mod iter_with_large_setup;
66
pub mod iter_with_setup;
77
pub mod measurement_overhead;
88
pub mod sampling_mode;
9+
pub mod sleep;
910
pub mod special_characters;
1011
pub mod with_inputs;
1112

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use codspeed_criterion_compat::{criterion_group, Criterion};
2+
use std::time::Duration;
3+
4+
fn sleep_benchmarks(c: &mut Criterion) {
5+
c.bench_function("sleep_1ns", |b| {
6+
b.iter(|| std::thread::sleep(Duration::from_nanos(1)))
7+
});
8+
9+
c.bench_function("sleep_100ns", |b| {
10+
b.iter(|| std::thread::sleep(Duration::from_nanos(100)))
11+
});
12+
13+
c.bench_function("sleep_1us", |b| {
14+
b.iter(|| std::thread::sleep(Duration::from_micros(1)))
15+
});
16+
17+
c.bench_function("sleep_100us", |b| {
18+
b.iter(|| std::thread::sleep(Duration::from_micros(100)))
19+
});
20+
21+
c.bench_function("sleep_1ms", |b| {
22+
b.iter(|| std::thread::sleep(Duration::from_millis(1)))
23+
});
24+
25+
c.bench_function("sleep_10ms", |b| {
26+
b.iter(|| std::thread::sleep(Duration::from_millis(10)))
27+
});
28+
29+
c.bench_function("sleep_50ms", |b| {
30+
b.iter(|| std::thread::sleep(Duration::from_millis(50)))
31+
});
32+
33+
c.bench_function("sleep_100ms", |b| {
34+
b.iter(|| std::thread::sleep(Duration::from_millis(100)))
35+
});
36+
37+
c.bench_function("sleep_100ms_with_custom_sample", |b| {
38+
b.iter(|| std::thread::sleep(Duration::from_millis(100)))
39+
});
40+
}
41+
42+
criterion_group!(benches, sleep_benchmarks);

crates/criterion_compat/benches/criterion_integration_main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ criterion_main! {
1313
criterion_integration::measurement_overhead::benches,
1414
criterion_integration::custom_measurement::benches,
1515
criterion_integration::sampling_mode::benches,
16+
criterion_integration::sleep::benches,
1617
criterion_integration::async_measurement_overhead::benches,
1718
}

crates/divan_compat/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ codspeed-divan-compat-macros = { version = "=2.10.1", path = './macros' }
2525
[[bench]]
2626
name = "basic_example"
2727
harness = false
28+
29+
[[bench]]
30+
name = "sleep_benches"
31+
harness = false
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#[divan::bench]
2+
fn sleep_1ns() {
3+
std::thread::sleep(std::time::Duration::from_nanos(1));
4+
}
5+
6+
#[divan::bench]
7+
fn sleep_100ns() {
8+
std::thread::sleep(std::time::Duration::from_nanos(100));
9+
}
10+
11+
#[divan::bench]
12+
fn sleep_1us() {
13+
std::thread::sleep(std::time::Duration::from_micros(1));
14+
}
15+
16+
#[divan::bench]
17+
fn sleep_100us() {
18+
std::thread::sleep(std::time::Duration::from_micros(100));
19+
}
20+
21+
#[divan::bench]
22+
fn sleep_1ms() {
23+
std::thread::sleep(std::time::Duration::from_millis(1));
24+
}
25+
26+
#[divan::bench]
27+
fn sleep_10ms() {
28+
std::thread::sleep(std::time::Duration::from_millis(10));
29+
}
30+
31+
#[divan::bench]
32+
fn sleep_50ms() {
33+
std::thread::sleep(std::time::Duration::from_millis(50));
34+
}
35+
36+
#[divan::bench]
37+
fn sleep_100ms() {
38+
std::thread::sleep(std::time::Duration::from_millis(100));
39+
}
40+
41+
// Tests COD-1044, do not modify the sample size or count!
42+
#[divan::bench(sample_size = 3, sample_count = 6)]
43+
fn sleep_100ms_with_custom_sample() {
44+
std::thread::sleep(std::time::Duration::from_millis(100));
45+
}
46+
47+
fn main() {
48+
codspeed_divan_compat::main();
49+
}

0 commit comments

Comments
 (0)