summaryrefslogtreecommitdiffstats
path: root/third_party/rust/triple_buffer/benches/benchmarks.rs
blob: c97d1db19e7025be0d65ff4c1a01d42dc83874c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use triple_buffer::TripleBuffer;

pub fn benchmark(c: &mut Criterion) {
    let (mut input, mut output) = TripleBuffer::<u8>::default().split();

    {
        let mut uncontended = c.benchmark_group("uncontended");
        uncontended.bench_function("read output", |b| b.iter(|| *output.output_buffer()));
        uncontended.bench_function("clean update", |b| {
            b.iter(|| {
                output.update();
            })
        });
        uncontended.bench_function("clean receive", |b| b.iter(|| *output.read()));
        uncontended.bench_function("write input", |b| {
            b.iter(|| {
                *input.input_buffer() = black_box(0);
            })
        });
        uncontended.bench_function("publish", |b| {
            b.iter(|| {
                input.publish();
            })
        });
        uncontended.bench_function("send", |b| b.iter(|| input.write(black_box(0))));
        uncontended.bench_function("publish + dirty update", |b| {
            b.iter(|| {
                input.publish();
                output.update();
            })
        });
        uncontended.bench_function("transmit", |b| {
            b.iter(|| {
                input.write(black_box(0));
                *output.read()
            })
        });
    }

    {
        let mut read_contended = c.benchmark_group("read contention");
        testbench::run_under_contention(
            || black_box(*output.read()),
            || {
                read_contended.bench_function("write input", |b| {
                    b.iter(|| {
                        *input.input_buffer() = black_box(0);
                    })
                });
                read_contended.bench_function("publish", |b| {
                    b.iter(|| {
                        input.publish();
                    })
                });
                read_contended.bench_function("send", |b| b.iter(|| input.write(black_box(0))));
            },
        );
    }

    {
        let mut write_contended = c.benchmark_group("write contention");
        testbench::run_under_contention(
            || input.write(black_box(0)),
            || {
                write_contended
                    .bench_function("read output", |b| b.iter(|| *output.output_buffer()));
                write_contended.bench_function("update", |b| {
                    b.iter(|| {
                        output.update();
                    })
                });
                write_contended.bench_function("receive", |b| b.iter(|| *output.read()));
            },
        );
    }
}

criterion_group!(benches, benchmark);
criterion_main!(benches);