blob: ac7f8faa2a87f4614d9cbb5242cf4098ae999ebb [file] [log] [blame]
Lalit Magantibfc3d3e2018-03-22 20:28:38 +00001// Copyright (C) 2018 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
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000015#include <random>
16
Primiano Tucci2c5488f2019-06-01 03:27:28 +010017#include <benchmark/benchmark.h>
Primiano Tucci2c5488f2019-06-01 03:27:28 +010018
Eric Seckler83dcc8c2019-08-21 12:18:43 +010019#include "perfetto/base/time.h"
Primiano Tucci2c5488f2019-06-01 03:27:28 +010020#include "perfetto/ext/traced/traced.h"
Primiano Tucci2c5488f2019-06-01 03:27:28 +010021#include "perfetto/ext/tracing/core/trace_packet.h"
Primiano Tucci0f9e0222019-06-05 09:36:41 +010022#include "perfetto/tracing/core/trace_config.h"
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000023#include "src/base/test/test_task_runner.h"
Primiano Tucci919ca1e2019-08-21 20:26:58 +020024#include "test/gtest_and_gmock.h"
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010025#include "test/test_helper.h"
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000026
Primiano Tucci57dd66b2019-10-15 23:09:04 +010027#include "protos/perfetto/config/test_config.gen.h"
Primiano Tuccife502c42019-12-11 01:00:27 +000028#include "protos/perfetto/trace/test_event.gen.h"
Primiano Tucci355b8c82019-08-29 08:37:51 +020029#include "protos/perfetto/trace/trace_packet.pbzero.h"
Primiano Tucci07e104d2018-04-03 20:45:35 +020030
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000031namespace perfetto {
32
Lalit Maganti131b6e52018-03-29 18:29:31 +010033namespace {
34
Lalit Maganti131b6e52018-03-29 18:29:31 +010035bool IsBenchmarkFunctionalOnly() {
36 return getenv("BENCHMARK_FUNCTIONAL_TEST_ONLY") != nullptr;
37}
38
Lalit Maganti3f0b7c62018-04-18 19:10:09 +010039void BenchmarkProducer(benchmark::State& state) {
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000040 base::TestTaskRunner task_runner;
41
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010042 TestHelper helper(&task_runner);
43 helper.StartServiceIfRequired();
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000044
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010045 FakeProducer* producer = helper.ConnectFakeProducer();
46 helper.ConnectConsumer();
Lalit Maganti36557d82018-04-11 14:36:17 +010047 helper.WaitForConsumerConnect();
Lalit Maganti131b6e52018-03-29 18:29:31 +010048
Lalit Magantidd95ef92018-03-23 09:42:48 +000049 TraceConfig trace_config;
50 trace_config.add_buffers()->set_size_kb(512);
51
Lalit Magantidd95ef92018-03-23 09:42:48 +000052 auto* ds_config = trace_config.add_data_sources()->mutable_config();
53 ds_config->set_name("android.perfetto.FakeProducer");
54 ds_config->set_target_buffer(0);
55
Lalit Magantidd95ef92018-03-23 09:42:48 +000056 static constexpr uint32_t kRandomSeed = 42;
Primiano Tucci3cbb10a2018-04-10 17:52:40 +010057 uint32_t message_count = static_cast<uint32_t>(state.range(0));
58 uint32_t message_bytes = static_cast<uint32_t>(state.range(1));
59 uint32_t mb_per_s = static_cast<uint32_t>(state.range(2));
Lalit Maganti131b6e52018-03-29 18:29:31 +010060
Primiano Tucci3cbb10a2018-04-10 17:52:40 +010061 uint32_t messages_per_s = mb_per_s * 1024 * 1024 / message_bytes;
62 uint32_t time_for_messages_ms =
Lalit Maganti131b6e52018-03-29 18:29:31 +010063 10000 + (messages_per_s == 0 ? 0 : message_count * 1000 / messages_per_s);
Lalit Magantidd95ef92018-03-23 09:42:48 +000064
Lalit Magantidd95ef92018-03-23 09:42:48 +000065 ds_config->mutable_for_testing()->set_seed(kRandomSeed);
66 ds_config->mutable_for_testing()->set_message_count(message_count);
Lalit Maganti131b6e52018-03-29 18:29:31 +010067 ds_config->mutable_for_testing()->set_message_size(message_bytes);
68 ds_config->mutable_for_testing()->set_max_messages_per_second(messages_per_s);
Lalit Magantidd95ef92018-03-23 09:42:48 +000069
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010070 helper.StartTracing(trace_config);
Lalit Maganti36557d82018-04-11 14:36:17 +010071 helper.WaitForProducerEnabled();
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000072
Primiano Tucci3cbb10a2018-04-10 17:52:40 +010073 uint64_t wall_start_ns = static_cast<uint64_t>(base::GetWallTimeNs().count());
Lalit Maganti9782f492020-01-10 18:13:13 +000074 uint64_t service_start_ns =
75 helper.service_thread()->GetThreadCPUTimeNsForTesting();
76 uint64_t producer_start_ns =
77 helper.producer_thread()->GetThreadCPUTimeNsForTesting();
Primiano Tucci3cbb10a2018-04-10 17:52:40 +010078 uint32_t iterations = 0;
Lalit Magantidd95ef92018-03-23 09:42:48 +000079 for (auto _ : state) {
80 auto cname = "produced.and.committed." + std::to_string(iterations++);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000081 auto on_produced_and_committed = task_runner.CreateCheckpoint(cname);
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010082 producer->ProduceEventBatch(helper.WrapTask(on_produced_and_committed));
Lalit Maganti131b6e52018-03-29 18:29:31 +010083 task_runner.RunUntilCheckpoint(cname, time_for_messages_ms);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000084 }
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010085 uint64_t service_ns =
Lalit Maganti9782f492020-01-10 18:13:13 +000086 helper.service_thread()->GetThreadCPUTimeNsForTesting() -
87 service_start_ns;
Lalit Maganti131b6e52018-03-29 18:29:31 +010088 uint64_t producer_ns =
Lalit Maganti9782f492020-01-10 18:13:13 +000089 helper.producer_thread()->GetThreadCPUTimeNsForTesting() -
90 producer_start_ns;
Primiano Tucci3cbb10a2018-04-10 17:52:40 +010091 uint64_t wall_ns =
92 static_cast<uint64_t>(base::GetWallTimeNs().count()) - wall_start_ns;
Lalit Magantidd95ef92018-03-23 09:42:48 +000093
Florian Mayer39e40d42020-07-16 14:52:07 +020094 state.counters["Ser CPU"] = benchmark::Counter(
95 100.0 * static_cast<double>(service_ns) / static_cast<double>(wall_ns));
96 state.counters["Ser ns/m"] = benchmark::Counter(
97 static_cast<double>(service_ns) / static_cast<double>(message_count));
98 state.counters["Pro CPU"] = benchmark::Counter(
99 100.0 * static_cast<double>(producer_ns) / static_cast<double>(wall_ns));
Lalit Maganti36557d82018-04-11 14:36:17 +0100100 state.SetBytesProcessed(iterations * message_bytes * message_count);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +0000101
102 // Read back the buffer just to check correctness.
Lalit Maganti36557d82018-04-11 14:36:17 +0100103 helper.ReadData();
104 helper.WaitForReadData();
105
106 bool is_first_packet = true;
107 std::minstd_rand0 rnd_engine(kRandomSeed);
108 for (const auto& packet : helper.trace()) {
109 ASSERT_TRUE(packet.has_for_testing());
110 if (is_first_packet) {
111 rnd_engine = std::minstd_rand0(packet.for_testing().seq_value());
112 is_first_packet = false;
113 } else {
114 ASSERT_EQ(packet.for_testing().seq_value(), rnd_engine());
115 }
116 }
Lalit Magantibfc3d3e2018-03-22 20:28:38 +0000117}
118
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100119static void BenchmarkConsumer(benchmark::State& state) {
120 base::TestTaskRunner task_runner;
121
122 TestHelper helper(&task_runner);
123 helper.StartServiceIfRequired();
124
125 FakeProducer* producer = helper.ConnectFakeProducer();
126 helper.ConnectConsumer();
127 helper.WaitForConsumerConnect();
128
129 TraceConfig trace_config;
130
131 static const uint32_t kBufferSizeBytes =
132 IsBenchmarkFunctionalOnly() ? 16 * 1024 : 2 * 1024 * 1024;
133 trace_config.add_buffers()->set_size_kb(kBufferSizeBytes / 1024);
134
135 static constexpr uint32_t kRandomSeed = 42;
136 uint32_t message_bytes = static_cast<uint32_t>(state.range(0));
137 uint32_t mb_per_s = static_cast<uint32_t>(state.range(1));
138 bool is_saturated_producer = mb_per_s == 0;
139
140 uint32_t message_count = kBufferSizeBytes / message_bytes;
141 uint32_t messages_per_s = mb_per_s * 1024 * 1024 / message_bytes;
142 uint32_t number_of_batches =
143 is_saturated_producer ? 0 : std::max(1u, message_count / messages_per_s);
144
145 auto* ds_config = trace_config.add_data_sources()->mutable_config();
146 ds_config->set_name("android.perfetto.FakeProducer");
147 ds_config->set_target_buffer(0);
148 ds_config->mutable_for_testing()->set_seed(kRandomSeed);
149 ds_config->mutable_for_testing()->set_message_count(message_count);
150 ds_config->mutable_for_testing()->set_message_size(message_bytes);
151 ds_config->mutable_for_testing()->set_max_messages_per_second(messages_per_s);
152
153 helper.StartTracing(trace_config);
154 helper.WaitForProducerEnabled();
155
156 uint64_t wall_start_ns = static_cast<uint64_t>(base::GetWallTimeNs().count());
Lalit Maganti9782f492020-01-10 18:13:13 +0000157 uint64_t service_start_ns = static_cast<uint64_t>(
158 helper.service_thread()->GetThreadCPUTimeNsForTesting());
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100159 uint64_t consumer_start_ns =
160 static_cast<uint64_t>(base::GetThreadCPUTimeNs().count());
161 uint64_t read_time_taken_ns = 0;
162
163 uint64_t iterations = 0;
164 uint32_t counter = 0;
165 for (auto _ : state) {
166 auto cname = "produced.and.committed." + std::to_string(iterations++);
167 auto on_produced_and_committed = task_runner.CreateCheckpoint(cname);
168 producer->ProduceEventBatch(helper.WrapTask(on_produced_and_committed));
169
170 if (is_saturated_producer) {
171 // If the producer is running in saturated mode, wait until it flushes
172 // data.
173 task_runner.RunUntilCheckpoint(cname);
174
175 // Then time how long it takes to read back the data.
176 int64_t start = base::GetWallTimeNs().count();
177 helper.ReadData(counter);
178 helper.WaitForReadData(counter++);
179 read_time_taken_ns +=
180 static_cast<uint64_t>(base::GetWallTimeNs().count() - start);
181 } else {
182 // If the producer is not running in saturated mode, every second the
183 // producer will send a batch of data over. Wait for a second before
184 // performing readback; do this for each batch the producer sends.
185 for (uint32_t i = 0; i < number_of_batches; i++) {
186 auto batch_cname = "batch.checkpoint." + std::to_string(counter);
187 auto batch_checkpoint = task_runner.CreateCheckpoint(batch_cname);
188 task_runner.PostDelayedTask(batch_checkpoint, 1000);
189 task_runner.RunUntilCheckpoint(batch_cname);
190
191 int64_t start = base::GetWallTimeNs().count();
192 helper.ReadData(counter);
193 helper.WaitForReadData(counter++);
194 read_time_taken_ns +=
195 static_cast<uint64_t>(base::GetWallTimeNs().count() - start);
196 }
197 }
198 }
199 uint64_t service_ns =
Lalit Maganti9782f492020-01-10 18:13:13 +0000200 helper.service_thread()->GetThreadCPUTimeNsForTesting() -
201 service_start_ns;
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100202 uint64_t consumer_ns =
203 static_cast<uint64_t>(base::GetThreadCPUTimeNs().count()) -
204 consumer_start_ns;
205 uint64_t wall_ns =
206 static_cast<uint64_t>(base::GetWallTimeNs().count()) - wall_start_ns;
207
Florian Mayer39e40d42020-07-16 14:52:07 +0200208 state.counters["Ser CPU"] = benchmark::Counter(
209 100.0 * static_cast<double>(service_ns) / static_cast<double>(wall_ns));
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100210 state.counters["Ser ns/m"] =
Florian Mayer39e40d42020-07-16 14:52:07 +0200211 benchmark::Counter(1.0 * static_cast<double>(service_ns) /
212 static_cast<double>(message_count));
213 state.counters["Con CPU"] = benchmark::Counter(
214 100.0 * static_cast<double>(consumer_ns) / static_cast<double>(wall_ns));
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100215 state.counters["Con Speed"] =
Florian Mayer39e40d42020-07-16 14:52:07 +0200216 benchmark::Counter(static_cast<double>(iterations) * 1000.0 * 1000.0 *
217 1000.0 * static_cast<double>(kBufferSizeBytes) /
218 static_cast<double>(read_time_taken_ns));
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100219}
220
221void SaturateCpuProducerArgs(benchmark::internal::Benchmark* b) {
Lalit Maganti131b6e52018-03-29 18:29:31 +0100222 int min_message_count = 16;
Hector Dearman1dc11562019-12-13 16:06:10 +0000223 int max_message_count = IsBenchmarkFunctionalOnly() ? 16 : 1024 * 1024;
Lalit Maganti131b6e52018-03-29 18:29:31 +0100224 int min_payload = 8;
Hector Dearman1dc11562019-12-13 16:06:10 +0000225 int max_payload = IsBenchmarkFunctionalOnly() ? 8 : 2048;
Lalit Maganti131b6e52018-03-29 18:29:31 +0100226 for (int count = min_message_count; count <= max_message_count; count *= 2) {
227 for (int bytes = min_payload; bytes <= max_payload; bytes *= 2) {
228 b->Args({count, bytes, 0 /* speed */});
229 }
230 }
231}
232
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100233void ConstantRateProducerArgs(benchmark::internal::Benchmark* b) {
Lalit Maganti131b6e52018-03-29 18:29:31 +0100234 int message_count = IsBenchmarkFunctionalOnly() ? 2 * 1024 : 128 * 1024;
235 int min_speed = IsBenchmarkFunctionalOnly() ? 64 : 8;
Lalit Maganti92ac3842018-05-02 12:02:14 +0100236 int max_speed = 128;
Lalit Maganti131b6e52018-03-29 18:29:31 +0100237 for (int speed = min_speed; speed <= max_speed; speed *= 2) {
238 b->Args({message_count, 128, speed});
239 b->Args({message_count, 256, speed});
240 }
241}
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100242
243void SaturateCpuConsumerArgs(benchmark::internal::Benchmark* b) {
244 int min_payload = 8;
245 int max_payload = IsBenchmarkFunctionalOnly() ? 16 : 64 * 1024;
246 for (int bytes = min_payload; bytes <= max_payload; bytes *= 2) {
247 b->Args({bytes, 0 /* speed */});
248 }
249}
250
251void ConstantRateConsumerArgs(benchmark::internal::Benchmark* b) {
252 int min_speed = IsBenchmarkFunctionalOnly() ? 128 : 1;
253 int max_speed = IsBenchmarkFunctionalOnly() ? 128 : 2;
254 for (int speed = min_speed; speed <= max_speed; speed *= 2) {
255 b->Args({2, speed});
256 b->Args({4, speed});
257 }
258}
259
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100260} // namespace
Lalit Maganti131b6e52018-03-29 18:29:31 +0100261
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100262static void BM_EndToEnd_Producer_SaturateCpu(benchmark::State& state) {
263 BenchmarkProducer(state);
Lalit Maganti131b6e52018-03-29 18:29:31 +0100264}
265
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100266BENCHMARK(BM_EndToEnd_Producer_SaturateCpu)
Lalit Magantibfc3d3e2018-03-22 20:28:38 +0000267 ->Unit(benchmark::kMicrosecond)
268 ->UseRealTime()
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100269 ->Apply(SaturateCpuProducerArgs);
Lalit Maganti131b6e52018-03-29 18:29:31 +0100270
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100271static void BM_EndToEnd_Producer_ConstantRate(benchmark::State& state) {
272 BenchmarkProducer(state);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +0000273}
Lalit Maganti131b6e52018-03-29 18:29:31 +0100274
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100275BENCHMARK(BM_EndToEnd_Producer_ConstantRate)
Lalit Maganti131b6e52018-03-29 18:29:31 +0100276 ->Unit(benchmark::kMicrosecond)
277 ->UseRealTime()
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100278 ->Apply(ConstantRateProducerArgs);
279
280static void BM_EndToEnd_Consumer_SaturateCpu(benchmark::State& state) {
281 BenchmarkConsumer(state);
282}
283
284BENCHMARK(BM_EndToEnd_Consumer_SaturateCpu)
285 ->Unit(benchmark::kMicrosecond)
286 ->UseRealTime()
287 ->Apply(SaturateCpuConsumerArgs);
288
289static void BM_EndToEnd_Consumer_ConstantRate(benchmark::State& state) {
290 BenchmarkConsumer(state);
291}
292
293BENCHMARK(BM_EndToEnd_Consumer_ConstantRate)
294 ->Unit(benchmark::kMillisecond)
295 ->UseRealTime()
296 ->Apply(ConstantRateConsumerArgs);
297
Lalit Maganti131b6e52018-03-29 18:29:31 +0100298} // namespace perfetto