blob: e173356503f174aa300ea338c034cffcab802a86 [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 Maganti131b6e52018-03-29 18:29:31 +010047
Lalit Magantidd95ef92018-03-23 09:42:48 +000048 // Setup the TraceConfig for the consumer.
Lalit Magantidd95ef92018-03-23 09:42:48 +000049 TraceConfig trace_config;
50 trace_config.add_buffers()->set_size_kb(512);
51
52 // Create the buffer for ftrace.
53 auto* ds_config = trace_config.add_data_sources()->mutable_config();
54 ds_config->set_name("android.perfetto.FakeProducer");
55 ds_config->set_target_buffer(0);
56
57 // The parameters for the producer.
58 static constexpr uint32_t kRandomSeed = 42;
Lalit Maganti131b6e52018-03-29 18:29:31 +010059 size_t message_count = state.range(0);
60 size_t message_bytes = state.range(1);
61 size_t mb_per_s = state.range(2);
62
63 size_t messages_per_s = mb_per_s * 1024 * 1024 / message_bytes;
64 size_t time_for_messages_ms =
65 10000 + (messages_per_s == 0 ? 0 : message_count * 1000 / messages_per_s);
Lalit Magantidd95ef92018-03-23 09:42:48 +000066
67 // Setup the test to use a random number generator.
68 ds_config->mutable_for_testing()->set_seed(kRandomSeed);
69 ds_config->mutable_for_testing()->set_message_count(message_count);
Lalit Maganti131b6e52018-03-29 18:29:31 +010070 ds_config->mutable_for_testing()->set_message_size(message_bytes);
71 ds_config->mutable_for_testing()->set_max_messages_per_second(messages_per_s);
Lalit Magantidd95ef92018-03-23 09:42:48 +000072
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010073 helper.StartTracing(trace_config);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000074
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010075 bool is_first_packet = true;
76 std::minstd_rand0 rnd_engine(kRandomSeed);
Primiano Tucci07e104d2018-04-03 20:45:35 +020077 auto on_consumer_data = [&is_first_packet,
78 &rnd_engine](const protos::TracePacket& packet) {
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010079 ASSERT_TRUE(packet.has_for_testing());
80 if (is_first_packet) {
81 rnd_engine = std::minstd_rand0(packet.for_testing().seq_value());
82 is_first_packet = false;
83 } else {
84 ASSERT_EQ(packet.for_testing().seq_value(), rnd_engine());
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000085 }
86 };
87
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000088 uint64_t wall_start_ns = base::GetWallTimeNs().count();
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010089 uint64_t service_start_ns = helper.service_thread()->GetThreadCPUTimeNs();
90 uint64_t producer_start_ns = helper.producer_thread()->GetThreadCPUTimeNs();
Lalit Magantidd95ef92018-03-23 09:42:48 +000091 uint64_t iterations = 0;
92 for (auto _ : state) {
93 auto cname = "produced.and.committed." + std::to_string(iterations++);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000094 auto on_produced_and_committed = task_runner.CreateCheckpoint(cname);
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010095 producer->ProduceEventBatch(helper.WrapTask(on_produced_and_committed));
Lalit Maganti131b6e52018-03-29 18:29:31 +010096 task_runner.RunUntilCheckpoint(cname, time_for_messages_ms);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000097 }
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010098 uint64_t service_ns =
99 helper.service_thread()->GetThreadCPUTimeNs() - service_start_ns;
Lalit Maganti131b6e52018-03-29 18:29:31 +0100100 uint64_t producer_ns =
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100101 helper.producer_thread()->GetThreadCPUTimeNs() - producer_start_ns;
Lalit Magantibfc3d3e2018-03-22 20:28:38 +0000102 uint64_t wall_ns = base::GetWallTimeNs().count() - wall_start_ns;
Lalit Magantidd95ef92018-03-23 09:42:48 +0000103
Lalit Maganti131b6e52018-03-29 18:29:31 +0100104 state.counters["Pro CPU"] = benchmark::Counter(100.0 * producer_ns / wall_ns);
105 state.counters["Ser CPU"] = benchmark::Counter(100.0 * service_ns / wall_ns);
Lalit Magantidd95ef92018-03-23 09:42:48 +0000106 state.counters["Ser ns/m"] =
Lalit Maganti131b6e52018-03-29 18:29:31 +0100107 benchmark::Counter(1.0 * service_ns / message_count);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +0000108
109 // Read back the buffer just to check correctness.
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100110 auto on_readback_complete = task_runner.CreateCheckpoint("readback.complete");
111 helper.ReadData(on_consumer_data, on_readback_complete);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +0000112 task_runner.RunUntilCheckpoint("readback.complete");
Lalit Maganti131b6e52018-03-29 18:29:31 +0100113 state.SetBytesProcessed(iterations * message_bytes * message_count);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +0000114}
115
Lalit Maganti131b6e52018-03-29 18:29:31 +0100116void SaturateCpuArgs(benchmark::internal::Benchmark* b) {
117 int min_message_count = 16;
118 int max_message_count = IsBenchmarkFunctionalOnly() ? 1024 : 1024 * 1024;
119 int min_payload = 8;
120 int max_payload = IsBenchmarkFunctionalOnly() ? 256 : 2048;
121 for (int count = min_message_count; count <= max_message_count; count *= 2) {
122 for (int bytes = min_payload; bytes <= max_payload; bytes *= 2) {
123 b->Args({count, bytes, 0 /* speed */});
124 }
125 }
126}
127
128void ConstantRateArgs(benchmark::internal::Benchmark* b) {
129 int message_count = IsBenchmarkFunctionalOnly() ? 2 * 1024 : 128 * 1024;
130 int min_speed = IsBenchmarkFunctionalOnly() ? 64 : 8;
131 int max_speed = IsBenchmarkFunctionalOnly() ? 128 : 128;
132 for (int speed = min_speed; speed <= max_speed; speed *= 2) {
133 b->Args({message_count, 128, speed});
134 b->Args({message_count, 256, speed});
135 }
136}
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100137} // namespace
Lalit Maganti131b6e52018-03-29 18:29:31 +0100138
139static void BM_EndToEnd_SaturateCpu(benchmark::State& state) {
140 BenchmarkCommon(state);
141}
142
143BENCHMARK(BM_EndToEnd_SaturateCpu)
Lalit Magantibfc3d3e2018-03-22 20:28:38 +0000144 ->Unit(benchmark::kMicrosecond)
145 ->UseRealTime()
Lalit Maganti131b6e52018-03-29 18:29:31 +0100146 ->Apply(SaturateCpuArgs);
147
148static void BM_EndToEnd_ConstantRate(benchmark::State& state) {
149 BenchmarkCommon(state);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +0000150}
Lalit Maganti131b6e52018-03-29 18:29:31 +0100151
152BENCHMARK(BM_EndToEnd_ConstantRate)
153 ->Unit(benchmark::kMicrosecond)
154 ->UseRealTime()
155 ->Apply(ConstantRateArgs);
156} // namespace perfetto