Skip to content

Commit 7d9262a

Browse files
authored
Merge pull request #1638 from Kobzol/runtime-benchmarks-changes
Runtime benchmarks changes
2 parents da93e0d + 455e4fa commit 7d9262a

File tree

8 files changed

+96
-39
lines changed

8 files changed

+96
-39
lines changed

collector/benchlib/src/benchmark.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ impl BenchmarkGroup {
8080

8181
for (name, benchmark_fn) in items {
8282
let mut stats: Vec<BenchmarkStats> = Vec::with_capacity(args.iterations as usize);
83+
// Warm-up
84+
for _ in 0..3 {
85+
let benchmark_stats = benchmark_fn()?;
86+
black_box(benchmark_stats);
87+
}
88+
89+
// Actual measurement
8390
for i in 0..args.iterations {
8491
let benchmark_stats = benchmark_fn()?;
8592
log::info!("Benchmark (run {i}) `{name}` completed: {benchmark_stats:?}");
@@ -120,7 +127,7 @@ pub fn passes_filter(name: &str, exclude: Option<&str>, include: Option<&str>) -
120127
}
121128
}
122129

123-
/// Copied from `iai`, so that we don't have to use unstable features.
130+
/// Copied from `iai`, so that it works on Rustc older than 1.66.
124131
pub fn black_box<T>(dummy: T) -> T {
125132
unsafe {
126133
let ret = std::ptr::read_volatile(&dummy);

collector/runtime-benchmarks/bufreader/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

collector/runtime-benchmarks/bufreader/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "bufreader"
2+
name = "bufreader-bench"
33
version = "0.1.0"
44
edition = "2021"
55

collector/runtime-benchmarks/hashmap/Cargo.lock

Lines changed: 5 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

collector/runtime-benchmarks/hashmap/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2021"
77

88
[dependencies]
99
benchlib = { path = "../../benchlib" }
10-
hashbrown = "0.12.3"
10+
hashbrown = { version = "0.13.2" }
1111
fxhash = "0.2.1"
1212

1313
[workspace]
Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,85 @@
11
use benchlib;
2-
use benchlib::benchmark::run_benchmark_group;
2+
use benchlib::benchmark::{black_box, run_benchmark_group};
33

44
fn main() {
55
run_benchmark_group(|group| {
6-
// Measures how long does it take to insert 10 thousand numbers into a `hashbrown` hashmap.
7-
group.register_benchmark("hashmap_insert_10k", || {
6+
// Measures how long does it take to insert 1 million numbers into a hashmap.
7+
group.register_benchmark("hashmap_insert_1m", || {
8+
let count = 1_000_000;
89
let mut map = hashbrown::HashMap::with_capacity_and_hasher(
9-
10000,
10+
// Over allocate the hashmap to avoid reallocations when inserting
11+
count * 2,
1012
fxhash::FxBuildHasher::default(),
1113
);
1214
move || {
13-
for index in 0..10000 {
15+
for index in 0..count {
1416
map.insert(index, index);
1517
}
1618
}
1719
});
20+
21+
// Measures how long it takes to remove 1 million elements from a hashmap.
22+
group.register_benchmark("hashmap_remove_1m", || {
23+
let mut map = hashbrown::HashMap::with_capacity_and_hasher(
24+
1_000_000,
25+
fxhash::FxBuildHasher::default(),
26+
);
27+
for index in 0..map.capacity() {
28+
map.insert(index, index);
29+
}
30+
31+
move || {
32+
for index in 0..map.capacity() {
33+
map.remove(&index);
34+
}
35+
}
36+
});
37+
38+
// Measures how long it takes to find 1 million elements that are in a hashmap.
39+
group.register_benchmark("hashmap_find_1m", || {
40+
let mut map = hashbrown::HashMap::with_capacity_and_hasher(
41+
1_000_000,
42+
fxhash::FxBuildHasher::default(),
43+
);
44+
for index in 0..map.capacity() {
45+
map.insert(index, index);
46+
}
47+
48+
move || {
49+
for index in 0..map.capacity() {
50+
black_box(map.get(&index));
51+
}
52+
}
53+
});
54+
55+
// Measures how long it takes to find 1 million elements that are not in a hashmap.
56+
group.register_benchmark("hashmap_find_misses_1m", || {
57+
let mut map = hashbrown::HashMap::with_capacity_and_hasher(
58+
1_000_000,
59+
fxhash::FxBuildHasher::default(),
60+
);
61+
for index in 0..map.capacity() {
62+
map.insert(index, index);
63+
}
64+
65+
move || {
66+
for index in map.capacity()..(map.capacity() * 2) {
67+
black_box(map.get(&index));
68+
}
69+
}
70+
});
71+
72+
// Measures how long it takes to iterate through values of a hashmap with 1 million elements.
73+
group.register_benchmark("hashmap_iterate_1m", || {
74+
let mut map = hashbrown::HashMap::with_capacity_and_hasher(
75+
1_000_000,
76+
fxhash::FxBuildHasher::default(),
77+
);
78+
for index in 0..map.capacity() {
79+
map.insert(index, index as u64);
80+
}
81+
82+
move || map.values().sum::<u64>()
83+
});
1884
});
1985
}

collector/runtime-benchmarks/nbody/src/main.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
//! Calculates the N-body simulation.
2-
//! Code taken from https://github.com/prestontw/rust-nbody
3-
41
use benchlib::benchmark::run_benchmark_group;
52

63
mod nbody;
74

85
fn main() {
96
run_benchmark_group(|group| {
10-
group.register_benchmark("nbody_10k", || {
11-
let mut nbody_10k = nbody::init(10000);
7+
// Calculates the N-body simulation.
8+
// Code taken from https://github.com/prestontw/rust-nbody
9+
group.register_benchmark("nbody_5k", || {
10+
let mut nbody = nbody::init(5000);
1211
|| {
1312
for _ in 0..10 {
14-
nbody_10k = nbody::compute_forces(nbody_10k);
13+
nbody = nbody::compute_forces(nbody);
1514
}
16-
nbody_10k
15+
nbody
1716
}
1817
});
1918
});

collector/src/runtime/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,11 @@ fn print_stats(result: &BenchmarkResult) {
221221
.map(|v| (v as f64 - mean).powf(2.0)),
222222
)
223223
.sqrt();
224+
let min = result.stats.iter().map(&f).min().unwrap_or(0);
224225

225226
println!(
226-
"{name:>18}: {:>16} (+/- {:>11})",
227+
"{name:>18}: min:{:>16} mean: {:>16} stddev: {:>11}",
228+
min.separate_with_commas(),
227229
(mean as u64).separate_with_commas(),
228230
(stddev as u64).separate_with_commas()
229231
);

0 commit comments

Comments
 (0)