xref: /aosp_15_r20/system/extras/libatrace_rust/benchmark/README.md (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker# libatrace_rust benchmarks
2*288bf522SAndroid Build Coastguard Worker
3*288bf522SAndroid Build Coastguard WorkerBenchmarks to compare the performance of Rust ATrace bindings with directly calling the
4*288bf522SAndroid Build Coastguard Worker`libcutils` methods from C++.
5*288bf522SAndroid Build Coastguard Worker
6*288bf522SAndroid Build Coastguard Worker## Benchmarks
7*288bf522SAndroid Build Coastguard Worker
8*288bf522SAndroid Build Coastguard Worker### ATrace wrapper benchmarks
9*288bf522SAndroid Build Coastguard Worker
10*288bf522SAndroid Build Coastguard WorkerThere are two binaries implementing the same benchmarks:
11*288bf522SAndroid Build Coastguard Worker
12*288bf522SAndroid Build Coastguard Worker* `libatrace_rust_benchmark` (`atrace_benchmark.rs`) for Rust.
13*288bf522SAndroid Build Coastguard Worker* `libatrace_rust_benchmark_cc` (`atrace_benchmark.cc`) for C++.
14*288bf522SAndroid Build Coastguard Worker
15*288bf522SAndroid Build Coastguard WorkerThe benchmarks emit ATrace events with tracing off and tracing on. `atrace_begin` is measured
16*288bf522SAndroid Build Coastguard Workerwith short and long event names to check if the string length affects timings. For example,
17*288bf522SAndroid Build Coastguard Worker`tracing_on_begin/1000` measures `atrace_begin` with a 1000-character name and tracing enabled.
18*288bf522SAndroid Build Coastguard Worker
19*288bf522SAndroid Build Coastguard Worker### ATrace tracing subscriber benchmark
20*288bf522SAndroid Build Coastguard Worker
21*288bf522SAndroid Build Coastguard WorkerThere is a benchmark for the tracing crate subscriber - `libatrace_tracing_subscriber_benchmark`.
22*288bf522SAndroid Build Coastguard WorkerWe use it to check overhead over the base `libatrace_rust`.
23*288bf522SAndroid Build Coastguard Worker
24*288bf522SAndroid Build Coastguard WorkerSimilarly to the wrapper benchmarks, the subscriber is measured with tracing off and on. There are
25*288bf522SAndroid Build Coastguard Workercases with and without extra fields to measure the cost of formatting. Cases that start with
26*288bf522SAndroid Build Coastguard Worker`filtered_` measure the subscriber in filtering mode with tracing disabled.
27*288bf522SAndroid Build Coastguard Worker
28*288bf522SAndroid Build Coastguard Worker## Running the benchmarks
29*288bf522SAndroid Build Coastguard Worker
30*288bf522SAndroid Build Coastguard WorkerTo run the benchmarks, push the binaries to the device with `adb` and launch them via `adb shell`.
31*288bf522SAndroid Build Coastguard WorkerYou may need to push dynamic libraries they depend on as well if they're not present on device and
32*288bf522SAndroid Build Coastguard Workerrun with `LD_LIBRARY_PATH`.
33*288bf522SAndroid Build Coastguard Worker
34*288bf522SAndroid Build Coastguard WorkerDo not enable ATrace collectors. The benchmarks effectively emit events in a loop and will spam
35*288bf522SAndroid Build Coastguard Workerany trace and distort performance results.
36*288bf522SAndroid Build Coastguard Worker
37*288bf522SAndroid Build Coastguard WorkerThe benchmarks will override system properties to enable or disable events, specifically ATrace App
38*288bf522SAndroid Build Coastguard Workerevent collection in `debug.atrace.app_number` and `debug.atrace.app_0`. After a successful execution
39*288bf522SAndroid Build Coastguard Workerthe events will be disabled.
40*288bf522SAndroid Build Coastguard Worker
41*288bf522SAndroid Build Coastguard Worker## Results
42*288bf522SAndroid Build Coastguard Worker
43*288bf522SAndroid Build Coastguard WorkerThe timings are not representative of actual cost of fully enabling tracing, only of emitting
44*288bf522SAndroid Build Coastguard Workerevents via API, since there's nothing receiving the events.
45*288bf522SAndroid Build Coastguard Worker
46*288bf522SAndroid Build Coastguard WorkerThe tests were done on a `aosp_cf_x86_64_phone-userdebug` Cuttlefish VM. Execution times on real
47*288bf522SAndroid Build Coastguard Workerdevice may be different but we expect similar relative performance between Rust wrappers and C.
48*288bf522SAndroid Build Coastguard Worker
49*288bf522SAndroid Build Coastguard Worker*If you notice that measurements with tracing off and tracing on have similar times, it might mean
50*288bf522SAndroid Build Coastguard Workerthat enabling ATrace events failed and you need to debug the benchmark.*
51*288bf522SAndroid Build Coastguard Worker
52*288bf522SAndroid Build Coastguard Worker### ATrace wrapper
53*288bf522SAndroid Build Coastguard Worker
54*288bf522SAndroid Build Coastguard WorkerRust results from `libatrace_rust_benchmark 2>&1 | grep time`:
55*288bf522SAndroid Build Coastguard Worker
56*288bf522SAndroid Build Coastguard Worker```text
57*288bf522SAndroid Build Coastguard Workertracing_off_begin/10    time:   [6.0211 ns 6.0382 ns 6.0607 ns]
58*288bf522SAndroid Build Coastguard Workertracing_off_begin/1000  time:   [6.0119 ns 6.0418 ns 6.0823 ns]
59*288bf522SAndroid Build Coastguard Workertracing_off_end         time:   [6.5417 ns 6.6801 ns 6.8131 ns]
60*288bf522SAndroid Build Coastguard Workertracing_on_begin/10     time:   [1.2847 µs 1.2929 µs 1.3044 µs]
61*288bf522SAndroid Build Coastguard Workertracing_on_begin/1000   time:   [1.5395 µs 1.5476 µs 1.5580 µs]
62*288bf522SAndroid Build Coastguard Workertracing_on_end          time:   [1.1153 µs 1.1208 µs 1.1276 µs]
63*288bf522SAndroid Build Coastguard Worker```
64*288bf522SAndroid Build Coastguard Worker
65*288bf522SAndroid Build Coastguard WorkerC++ results from `libatrace_rust_benchmark_cc`:
66*288bf522SAndroid Build Coastguard Worker
67*288bf522SAndroid Build Coastguard Worker```text
68*288bf522SAndroid Build Coastguard Worker------------------------------------------------------------------------
69*288bf522SAndroid Build Coastguard WorkerBenchmark                              Time             CPU   Iterations
70*288bf522SAndroid Build Coastguard Worker------------------------------------------------------------------------
71*288bf522SAndroid Build Coastguard WorkerBM_TracingOffAtraceBegin/10         4.00 ns         3.96 ns    175953732
72*288bf522SAndroid Build Coastguard WorkerBM_TracingOffAtraceBegin/1000       4.05 ns         4.02 ns    176298494
73*288bf522SAndroid Build Coastguard WorkerBM_TracingOffAtraceEnd              4.08 ns         4.05 ns    176422059
74*288bf522SAndroid Build Coastguard WorkerBM_TracingOnAtraceBegin/10          1119 ns         1110 ns       640816
75*288bf522SAndroid Build Coastguard WorkerBM_TracingOnAtraceBegin/1000        1151 ns         1142 ns       615781
76*288bf522SAndroid Build Coastguard WorkerBM_TracingOnAtraceEnd               1076 ns         1069 ns       653646
77*288bf522SAndroid Build Coastguard Worker```
78*288bf522SAndroid Build Coastguard Worker
79*288bf522SAndroid Build Coastguard Worker### ATrace tracing subscriber
80*288bf522SAndroid Build Coastguard Worker
81*288bf522SAndroid Build Coastguard WorkerThe tracing subscriber time consists of the underlying `libatrace_rust` call plus the time spent in
82*288bf522SAndroid Build Coastguard Workerthe subscriber itself.
83*288bf522SAndroid Build Coastguard Worker
84*288bf522SAndroid Build Coastguard WorkerResults from `libatrace_tracing_subscriber_benchmark 2>&1 | grep time`:
85*288bf522SAndroid Build Coastguard Worker
86*288bf522SAndroid Build Coastguard Worker```text
87*288bf522SAndroid Build Coastguard Workertracing_off_event       time:   [47.444 ns 47.945 ns 48.585 ns]
88*288bf522SAndroid Build Coastguard Workerfiltered_event          time:   [26.852 ns 26.942 ns 27.040 ns]
89*288bf522SAndroid Build Coastguard Workertracing_off_event_args  time:   [80.597 ns 80.997 ns 81.475 ns]
90*288bf522SAndroid Build Coastguard Workerfiltered_event_args     time:   [26.680 ns 26.782 ns 26.887 ns]
91*288bf522SAndroid Build Coastguard Workertracing_off_span        time:   [316.48 ns 317.72 ns 319.12 ns]
92*288bf522SAndroid Build Coastguard Workerfiltered_span           time:   [27.900 ns 27.959 ns 28.018 ns]
93*288bf522SAndroid Build Coastguard Workertracing_off_span_args   time:   [364.92 ns 367.57 ns 370.95 ns]
94*288bf522SAndroid Build Coastguard Workerfiltered_span_args      time:   [27.625 ns 27.919 ns 28.207 ns]
95*288bf522SAndroid Build Coastguard Workertracing_on_event        time:   [1.4639 µs 1.4805 µs 1.4954 µs]
96*288bf522SAndroid Build Coastguard Workertracing_on_event_args   time:   [2.0088 µs 2.0197 µs 2.0314 µs]
97*288bf522SAndroid Build Coastguard Workertracing_on_span         time:   [2.7907 µs 2.7996 µs 2.8103 µs]
98*288bf522SAndroid Build Coastguard Workertracing_on_span_args    time:   [3.6846 µs 3.6992 µs 3.7168 µs]
99*288bf522SAndroid Build Coastguard Worker```
100