Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 1 | // 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 Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 20 | #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 Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 24 | #include "test/task_runner_thread.h" |
| 25 | #include "test/task_runner_thread_delegates.h" |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 26 | #include "test/test_helper.h" |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 27 | |
Primiano Tucci | 07e104d | 2018-04-03 20:45:35 +0200 | [diff] [blame^] | 28 | #include "perfetto/trace/trace_packet.pb.h" |
| 29 | #include "perfetto/trace/trace_packet.pbzero.h" |
| 30 | |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 31 | namespace perfetto { |
| 32 | |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 33 | namespace { |
| 34 | |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 35 | bool IsBenchmarkFunctionalOnly() { |
| 36 | return getenv("BENCHMARK_FUNCTIONAL_TEST_ONLY") != nullptr; |
| 37 | } |
| 38 | |
| 39 | void BenchmarkCommon(benchmark::State& state) { |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 40 | base::TestTaskRunner task_runner; |
| 41 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 42 | TestHelper helper(&task_runner); |
| 43 | helper.StartServiceIfRequired(); |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 44 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 45 | FakeProducer* producer = helper.ConnectFakeProducer(); |
| 46 | helper.ConnectConsumer(); |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 47 | |
Lalit Maganti | dd95ef9 | 2018-03-23 09:42:48 +0000 | [diff] [blame] | 48 | // Setup the TraceConfig for the consumer. |
Lalit Maganti | dd95ef9 | 2018-03-23 09:42:48 +0000 | [diff] [blame] | 49 | 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 Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 59 | 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 Maganti | dd95ef9 | 2018-03-23 09:42:48 +0000 | [diff] [blame] | 66 | |
| 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 Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 70 | 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 Maganti | dd95ef9 | 2018-03-23 09:42:48 +0000 | [diff] [blame] | 72 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 73 | helper.StartTracing(trace_config); |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 74 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 75 | bool is_first_packet = true; |
| 76 | std::minstd_rand0 rnd_engine(kRandomSeed); |
Primiano Tucci | 07e104d | 2018-04-03 20:45:35 +0200 | [diff] [blame^] | 77 | auto on_consumer_data = [&is_first_packet, |
| 78 | &rnd_engine](const protos::TracePacket& packet) { |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 79 | 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 Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 85 | } |
| 86 | }; |
| 87 | |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 88 | uint64_t wall_start_ns = base::GetWallTimeNs().count(); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 89 | uint64_t service_start_ns = helper.service_thread()->GetThreadCPUTimeNs(); |
| 90 | uint64_t producer_start_ns = helper.producer_thread()->GetThreadCPUTimeNs(); |
Lalit Maganti | dd95ef9 | 2018-03-23 09:42:48 +0000 | [diff] [blame] | 91 | uint64_t iterations = 0; |
| 92 | for (auto _ : state) { |
| 93 | auto cname = "produced.and.committed." + std::to_string(iterations++); |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 94 | auto on_produced_and_committed = task_runner.CreateCheckpoint(cname); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 95 | producer->ProduceEventBatch(helper.WrapTask(on_produced_and_committed)); |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 96 | task_runner.RunUntilCheckpoint(cname, time_for_messages_ms); |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 97 | } |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 98 | uint64_t service_ns = |
| 99 | helper.service_thread()->GetThreadCPUTimeNs() - service_start_ns; |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 100 | uint64_t producer_ns = |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 101 | helper.producer_thread()->GetThreadCPUTimeNs() - producer_start_ns; |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 102 | uint64_t wall_ns = base::GetWallTimeNs().count() - wall_start_ns; |
Lalit Maganti | dd95ef9 | 2018-03-23 09:42:48 +0000 | [diff] [blame] | 103 | |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 104 | 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 Maganti | dd95ef9 | 2018-03-23 09:42:48 +0000 | [diff] [blame] | 106 | state.counters["Ser ns/m"] = |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 107 | benchmark::Counter(1.0 * service_ns / message_count); |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 108 | |
| 109 | // Read back the buffer just to check correctness. |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 110 | auto on_readback_complete = task_runner.CreateCheckpoint("readback.complete"); |
| 111 | helper.ReadData(on_consumer_data, on_readback_complete); |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 112 | task_runner.RunUntilCheckpoint("readback.complete"); |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 113 | state.SetBytesProcessed(iterations * message_bytes * message_count); |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 116 | void 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 | |
| 128 | void 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 Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 137 | } // namespace |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 138 | |
| 139 | static void BM_EndToEnd_SaturateCpu(benchmark::State& state) { |
| 140 | BenchmarkCommon(state); |
| 141 | } |
| 142 | |
| 143 | BENCHMARK(BM_EndToEnd_SaturateCpu) |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 144 | ->Unit(benchmark::kMicrosecond) |
| 145 | ->UseRealTime() |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 146 | ->Apply(SaturateCpuArgs); |
| 147 | |
| 148 | static void BM_EndToEnd_ConstantRate(benchmark::State& state) { |
| 149 | BenchmarkCommon(state); |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 150 | } |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 151 | |
| 152 | BENCHMARK(BM_EndToEnd_ConstantRate) |
| 153 | ->Unit(benchmark::kMicrosecond) |
| 154 | ->UseRealTime() |
| 155 | ->Apply(ConstantRateArgs); |
| 156 | } // namespace perfetto |