1 // Copyright (C) 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //! Benchmark for ATrace bindings.
16
17 use atrace::AtraceTag;
18 use atrace_rust_benchmark_common::{new_criterion, turn_tracing_off, turn_tracing_on};
19 use criterion::{BenchmarkId, Criterion};
20
bench_tracing_off_begin(c: &mut Criterion, name_len: usize)21 fn bench_tracing_off_begin(c: &mut Criterion, name_len: usize) {
22 turn_tracing_off();
23 let name = "0".repeat(name_len);
24 c.bench_with_input(BenchmarkId::new("tracing_off_begin", name_len), &name, |b, name| {
25 b.iter(|| atrace::atrace_begin(AtraceTag::App, name.as_str()))
26 });
27 }
28
bench_tracing_off_end(c: &mut Criterion)29 fn bench_tracing_off_end(c: &mut Criterion) {
30 turn_tracing_off();
31 c.bench_function("tracing_off_end", |b| b.iter(|| atrace::atrace_end(AtraceTag::App)));
32 }
33
bench_tracing_on_begin(c: &mut Criterion, name_len: usize)34 fn bench_tracing_on_begin(c: &mut Criterion, name_len: usize) {
35 turn_tracing_on();
36 let name = "0".repeat(name_len);
37 c.bench_with_input(BenchmarkId::new("tracing_on_begin", name_len), &name, |b, name| {
38 b.iter(|| atrace::atrace_begin(AtraceTag::App, name.as_str()))
39 });
40 turn_tracing_off();
41 }
42
bench_tracing_on_end(c: &mut Criterion)43 fn bench_tracing_on_end(c: &mut Criterion) {
44 turn_tracing_on();
45 c.bench_function("tracing_on_end", |b| b.iter(|| atrace::atrace_end(AtraceTag::App)));
46 turn_tracing_off();
47 }
48
main() -> Result<(), Box<dyn std::error::Error>>49 fn main() -> Result<(), Box<dyn std::error::Error>> {
50 let mut criterion = new_criterion();
51
52 bench_tracing_off_begin(&mut criterion, 10);
53 bench_tracing_off_begin(&mut criterion, 1000);
54 bench_tracing_off_end(&mut criterion);
55
56 bench_tracing_on_begin(&mut criterion, 10);
57 bench_tracing_on_begin(&mut criterion, 1000);
58 bench_tracing_on_end(&mut criterion);
59
60 Ok(())
61 }
62