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" 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" 24 #include "test/task_runner_thread.h" 25 #include "test/task_runner_thread_delegates.h" 26 #include "test/test_helper.h" 27 28 #include "perfetto/trace/trace_packet.pb.h" 29 #include "perfetto/trace/trace_packet.pbzero.h" 30 31 namespace perfetto { 32 33 namespace { 34 35 bool IsBenchmarkFunctionalOnly() { 36 return getenv("BENCHMARK_FUNCTIONAL_TEST_ONLY") != nullptr; 37 } 38 39 void BenchmarkCommon(benchmark::State& state) { 40 base::TestTaskRunner task_runner; 41 42 TestHelper helper(&task_runner); 43 helper.StartServiceIfRequired(); 44 45 FakeProducer* producer = helper.ConnectFakeProducer(); 46 helper.ConnectConsumer(); 47 helper.WaitForConsumerConnect(); 48 49 TraceConfig trace_config; 50 trace_config.add_buffers()->set_size_kb(512); 51 52 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 56 static constexpr uint32_t kRandomSeed = 42; 57 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)); 60 61 uint32_t messages_per_s = mb_per_s * 1024 * 1024 / message_bytes; 62 uint32_t time_for_messages_ms = 63 10000 + (messages_per_s == 0 ? 0 : message_count * 1000 / messages_per_s); 64 65 ds_config->mutable_for_testing()->set_seed(kRandomSeed); 66 ds_config->mutable_for_testing()->set_message_count(message_count); 67 ds_config->mutable_for_testing()->set_message_size(message_bytes); 68 ds_config->mutable_for_testing()->set_max_messages_per_second(messages_per_s); 69 70 helper.StartTracing(trace_config); 71 helper.WaitForProducerEnabled(); 72 73 uint64_t wall_start_ns = static_cast<uint64_t>(base::GetWallTimeNs().count()); 74 uint64_t service_start_ns = helper.service_thread()->GetThreadCPUTimeNs(); 75 uint64_t producer_start_ns = helper.producer_thread()->GetThreadCPUTimeNs(); 76 uint32_t iterations = 0; 77 for (auto _ : state) { 78 auto cname = "produced.and.committed." + std::to_string(iterations++); 79 auto on_produced_and_committed = task_runner.CreateCheckpoint(cname); 80 producer->ProduceEventBatch(helper.WrapTask(on_produced_and_committed)); 81 task_runner.RunUntilCheckpoint(cname, time_for_messages_ms); 82 } 83 uint64_t service_ns = 84 helper.service_thread()->GetThreadCPUTimeNs() - service_start_ns; 85 uint64_t producer_ns = 86 helper.producer_thread()->GetThreadCPUTimeNs() - producer_start_ns; 87 uint64_t wall_ns = 88 static_cast<uint64_t>(base::GetWallTimeNs().count()) - wall_start_ns; 89 90 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); 92 state.counters["Ser ns/m"] = 93 benchmark::Counter(1.0 * service_ns / message_count); 94 state.SetBytesProcessed(iterations * message_bytes * message_count); 95 96 // Read back the buffer just to check correctness. 97 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 } 111 } 112 113 void 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 125 void 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 } 134 } // namespace 135 136 static void BM_EndToEnd_SaturateCpu(benchmark::State& state) { 137 BenchmarkCommon(state); 138 } 139 140 BENCHMARK(BM_EndToEnd_SaturateCpu) 141 ->Unit(benchmark::kMicrosecond) 142 ->UseRealTime() 143 ->Apply(SaturateCpuArgs); 144 145 static void BM_EndToEnd_ConstantRate(benchmark::State& state) { 146 BenchmarkCommon(state); 147 } 148 149 BENCHMARK(BM_EndToEnd_ConstantRate) 150 ->Unit(benchmark::kMicrosecond) 151 ->UseRealTime() 152 ->Apply(ConstantRateArgs); 153 } // namespace perfetto 154