Debian Codegraph @

third_party/rust/triple_buffer/benches/benchmarks.rs

raw
1 use criterion::{black_box, criterion_group, criterion_main, Criterion};
2 use triple_buffer::TripleBuffer;
3
4 pub fn benchmark(c: &mut Criterion) {
5 let (mut input, mut output) = TripleBuffer::<u8>::default().split();
6
7 {
8 let mut uncontended = c.benchmark_group("uncontended");
9 uncontended.bench_function("read output", |b| b.iter(|| *output.output_buffer()));
10 uncontended.bench_function("clean update", |b| {
11 b.iter(|| {
12 output.update();
13 })
14 });
15 uncontended.bench_function("clean receive", |b| b.iter(|| *output.read()));
16 uncontended.bench_function("write input", |b| {
17 b.iter(|| {
18 *input.input_buffer() = black_box(0);
19 })
20 });
21 uncontended.bench_function("publish", |b| {
22 b.iter(|| {
23 input.publish();
24 })
25 });
26 uncontended.bench_function("send", |b| b.iter(|| input.write(black_box(0))));
27 uncontended.bench_function("publish + dirty update", |b| {
28 b.iter(|| {
29 input.publish();
30 output.update();
31 })
32 });
33 uncontended.bench_function("transmit", |b| {
34 b.iter(|| {
35 input.write(black_box(0));
36 *output.read()
37 })
38 });
39 }
40
41 {
42 let mut read_contended = c.benchmark_group("read contention");
43 testbench::run_under_contention(
44 || black_box(*output.read()),
45 || {
46 read_contended.bench_function("write input", |b| {
47 b.iter(|| {
48 *input.input_buffer() = black_box(0);
49 })
50 });
51 read_contended.bench_function("publish", |b| {
52 b.iter(|| {
53 input.publish();
54 })
55 });
56 read_contended.bench_function("send", |b| b.iter(|| input.write(black_box(0))));
57 },
58 );
59 }
60
61 {
62 let mut write_contended = c.benchmark_group("write contention");
63 testbench::run_under_contention(
64 || input.write(black_box(0)),
65 || {
66 write_contended
67 .bench_function("read output", |b| b.iter(|| *output.output_buffer()));
68 write_contended.bench_function("update", |b| {
69 b.iter(|| {
70 output.update();
71 })
72 });
73 write_contended.bench_function("receive", |b| b.iter(|| *output.read()));
74 },
75 );
76 }
77 }
78
79 criterion_group!(benches, benchmark);
80 criterion_main!(benches);