xref: /aosp_15_r20/external/perfetto/src/protozero/test/proto_ring_buffer_benchmark.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 // Copyright (C) 2022 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 #include <benchmark/benchmark.h>
16 
17 #include <algorithm>
18 #include <string>
19 
20 #include "perfetto/ext/base/file_utils.h"
21 #include "perfetto/ext/protozero/proto_ring_buffer.h"
22 #include "src/base/test/utils.h"
23 
BM_ProtoRingBufferReadLargeChunks(benchmark::State & state)24 static void BM_ProtoRingBufferReadLargeChunks(benchmark::State& state) {
25   std::string trace_data;
26   static const char kTestTrace[] = "test/data/example_android_trace_30s.pb";
27   perfetto::base::ReadFile(perfetto::base::GetTestDataPath(kTestTrace),
28                            &trace_data);
29   PERFETTO_CHECK(!trace_data.empty());
30 
31   size_t total_packet_size = 0;
32   protozero::ProtoRingBuffer buffer;
33   for (auto _ : state) {
34     protozero::ProtoRingBuffer::Message msg = buffer.ReadMessage();
35     if (msg.valid()) {
36       total_packet_size += msg.len;
37     } else {
38       state.PauseTiming();
39       buffer.Append(trace_data.data(), trace_data.size());
40       state.ResumeTiming();
41     }
42   }
43   benchmark::DoNotOptimize(total_packet_size);
44 }
45 
46 BENCHMARK(BM_ProtoRingBufferReadLargeChunks);
47 
BM_ProtoRingBufferRead(benchmark::State & state)48 static void BM_ProtoRingBufferRead(benchmark::State& state) {
49   std::string trace_data;
50   static const char kTestTrace[] = "test/data/example_android_trace_30s.pb";
51   perfetto::base::ReadFile(perfetto::base::GetTestDataPath(kTestTrace),
52                            &trace_data);
53   PERFETTO_CHECK(!trace_data.empty());
54 
55   constexpr size_t kChunkSize = 1024 * 1024 * 3;
56 
57   size_t offset = 0;
58   size_t total_packet_size = 0;
59   protozero::ProtoRingBuffer buffer;
60   for (auto _ : state) {
61     protozero::ProtoRingBuffer::Message msg = buffer.ReadMessage();
62     if (msg.valid()) {
63       total_packet_size += msg.len;
64     } else {
65       state.PauseTiming();
66       size_t sz = std::min(kChunkSize, trace_data.size() - offset);
67       buffer.Append(trace_data.data() + offset, sz);
68       offset = (offset + sz) % trace_data.size();
69       state.ResumeTiming();
70     }
71   }
72   benchmark::DoNotOptimize(total_packet_size);
73 }
74 
75 BENCHMARK(BM_ProtoRingBufferRead);
76