Skip to content

Commit 8b37245

Browse files
committed
Allow criterion to capture profiles
Signed-off-by: Jakub Sztandera <[email protected]>
1 parent 4923c09 commit 8b37245

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

ipld/bitfield/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ rand_xorshift = "0.2.0"
2020
rand = "0.7.3"
2121
criterion = "0.3"
2222
serde_json = "1.0"
23+
gperftools = "0.2.0"
2324

2425
[features]
2526
json = []

ipld/bitfield/benches/benchmarks/main.rs

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,34 @@
33

44
mod examples;
55

6+
use std::fs;
7+
use std::path::Path;
68
use criterion::{black_box, criterion_group, criterion_main, Criterion};
79
use examples::{example1, example2};
810
use fvm_ipld_bitfield::BitField;
11+
use gperftools::profiler::PROFILER;
12+
13+
struct Profiler;
14+
15+
impl criterion::profiler::Profiler for Profiler {
16+
fn start_profiling(&mut self, benchmark_id: &str, benchmark_dir: &Path) {
17+
fs::create_dir_all(benchmark_dir).unwrap();
18+
let bench_file =benchmark_id.to_owned() + ".prof";
19+
let bench_path = benchmark_dir.join(bench_file);
20+
let bench_str = bench_path.to_str().unwrap();
21+
22+
PROFILER.lock().unwrap().start(bench_str).unwrap();
23+
}
24+
25+
fn stop_profiling(&mut self, _: &str, _: &Path) {
26+
PROFILER.lock().unwrap().stop().unwrap();
27+
}
28+
}
29+
30+
fn profiled() -> Criterion {
31+
Criterion::default().with_profiler(Profiler{})
32+
}
33+
934

1035
fn len(c: &mut Criterion) {
1136
let bf = example1();
@@ -99,21 +124,23 @@ fn get(c: &mut Criterion) {
99124
}
100125

101126
criterion_group!(
102-
benches,
103-
len,
104-
bits,
105-
new,
106-
decode,
107-
decode_encode,
108-
from_ranges,
109-
is_empty,
110-
intersection,
111-
union,
112-
difference,
113-
symmetric_difference,
114-
cut,
115-
contains_all,
116-
contains_any,
117-
get,
127+
name = benches;
128+
config = profiled();
129+
targets =
130+
len,
131+
bits,
132+
new,
133+
decode,
134+
decode_encode,
135+
from_ranges,
136+
is_empty,
137+
intersection,
138+
union,
139+
difference,
140+
symmetric_difference,
141+
cut,
142+
contains_all,
143+
contains_any,
144+
get,
118145
);
119146
criterion_main!(benches);

0 commit comments

Comments
 (0)