blob: 100674e980459254fabc93b7f9f74497f0d0df3f [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
15#include <gtest/gtest.h>
16#include <random>
17
18#include "benchmark/benchmark.h"
19#include "perfetto/base/time.h"
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000020#include "perfetto/traced/traced.h"
21#include "perfetto/tracing/core/trace_config.h"
22#include "perfetto/tracing/core/trace_packet.h"
23#include "src/base/test/test_task_runner.h"
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000024#include "test/task_runner_thread.h"
25#include "test/task_runner_thread_delegates.h"
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010026#include "test/test_helper.h"
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000027
Primiano Tucci07e104d2018-04-03 20:45:35 +020028#include "perfetto/trace/trace_packet.pb.h"
29#include "perfetto/trace/trace_packet.pbzero.h"
30
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
39void BenchmarkCommon(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 Magantic4c3ceb2018-03-29 20:38:13 +010074 uint64_t service_start_ns = helper.service_thread()->GetThreadCPUTimeNs();
75 uint64_t producer_start_ns = helper.producer_thread()->GetThreadCPUTimeNs();
Primiano Tucci3cbb10a2018-04-10 17:52:40 +010076 uint32_t iterations = 0;
Lalit Magantidd95ef92018-03-23 09:42:48 +000077 for (auto _ : state) {
78 auto cname = "produced.and.committed." + std::to_string(iterations++);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000079 auto on_produced_and_committed = task_runner.CreateCheckpoint(cname);
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010080 producer->ProduceEventBatch(helper.WrapTask(on_produced_and_committed));
Lalit Maganti131b6e52018-03-29 18:29:31 +010081 task_runner.RunUntilCheckpoint(cname, time_for_messages_ms);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000082 }
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010083 uint64_t service_ns =
84 helper.service_thread()->GetThreadCPUTimeNs() - service_start_ns;
Lalit Maganti131b6e52018-03-29 18:29:31 +010085 uint64_t producer_ns =
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010086 helper.producer_thread()->GetThreadCPUTimeNs() - producer_start_ns;
Primiano Tucci3cbb10a2018-04-10 17:52:40 +010087 uint64_t wall_ns =
88 static_cast<uint64_t>(base::GetWallTimeNs().count()) - wall_start_ns;
Lalit Magantidd95ef92018-03-23 09:42:48 +000089
Lalit Maganti131b6e52018-03-29 18:29:31 +010090 state.counters["Pro CPU"] = benchmark::Counter(100.0 * producer_ns / wall_ns);
91 state.counters["Ser CPU"] = benchmark::Counter(100.0 * service_ns / wall_ns);
Lalit Magantidd95ef92018-03-23 09:42:48 +000092 state.counters["Ser ns/m"] =
Lalit Maganti131b6e52018-03-29 18:29:31 +010093 benchmark::Counter(1.0 * service_ns / message_count);
Lalit Maganti36557d82018-04-11 14:36:17 +010094 state.SetBytesProcessed(iterations * message_bytes * message_count);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000095
96 // Read back the buffer just to check correctness.
Lalit Maganti36557d82018-04-11 14:36:17 +010097 helper.ReadData();
98 helper.WaitForReadData();
99
100 bool is_first_packet = true;
101 std::minstd_rand0 rnd_engine(kRandomSeed);
102 for (const auto& packet : helper.trace()) {
103 ASSERT_TRUE(packet.has_for_testing());
104 if (is_first_packet) {
105 rnd_engine = std::minstd_rand0(packet.for_testing().seq_value());
106 is_first_packet = false;
107 } else {
108 ASSERT_EQ(packet.for_testing().seq_value(), rnd_engine());
109 }
110 }
Lalit Magantibfc3d3e2018-03-22 20:28:38 +0000111}
112
Lalit Maganti131b6e52018-03-29 18:29:31 +0100113void SaturateCpuArgs(benchmark::internal::Benchmark* b) {
114 int min_message_count = 16;
115 int max_message_count = IsBenchmarkFunctionalOnly() ? 1024 : 1024 * 1024;
116 int min_payload = 8;
117 int max_payload = IsBenchmarkFunctionalOnly() ? 256 : 2048;
118 for (int count = min_message_count; count <= max_message_count; count *= 2) {
119 for (int bytes = min_payload; bytes <= max_payload; bytes *= 2) {
120 b->Args({count, bytes, 0 /* speed */});
121 }
122 }
123}
124
125void ConstantRateArgs(benchmark::internal::Benchmark* b) {
126 int message_count = IsBenchmarkFunctionalOnly() ? 2 * 1024 : 128 * 1024;
127 int min_speed = IsBenchmarkFunctionalOnly() ? 64 : 8;
128 int max_speed = IsBenchmarkFunctionalOnly() ? 128 : 128;
129 for (int speed = min_speed; speed <= max_speed; speed *= 2) {
130 b->Args({message_count, 128, speed});
131 b->Args({message_count, 256, speed});
132 }
133}
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100134} // namespace
Lalit Maganti131b6e52018-03-29 18:29:31 +0100135
136static void BM_EndToEnd_SaturateCpu(benchmark::State& state) {
137 BenchmarkCommon(state);
138}
139
140BENCHMARK(BM_EndToEnd_SaturateCpu)
Lalit Magantibfc3d3e2018-03-22 20:28:38 +0000141 ->Unit(benchmark::kMicrosecond)
142 ->UseRealTime()
Lalit Maganti131b6e52018-03-29 18:29:31 +0100143 ->Apply(SaturateCpuArgs);
144
145static void BM_EndToEnd_ConstantRate(benchmark::State& state) {
146 BenchmarkCommon(state);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +0000147}
Lalit Maganti131b6e52018-03-29 18:29:31 +0100148
149BENCHMARK(BM_EndToEnd_ConstantRate)
150 ->Unit(benchmark::kMicrosecond)
151 ->UseRealTime()
152 ->Apply(ConstantRateArgs);
153} // namespace perfetto