1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/trace_processor/rpc/query_result_serializer.h"
18
19 #include <benchmark/benchmark.h>
20
21 #include "perfetto/trace_processor/basic_types.h"
22 #include "perfetto/trace_processor/trace_processor.h"
23
24 using perfetto::trace_processor::Config;
25 using perfetto::trace_processor::QueryResultSerializer;
26 using perfetto::trace_processor::TraceProcessor;
27 using VectorType = std::vector<uint8_t>;
28
29 namespace {
30
IsBenchmarkFunctionalOnly()31 bool IsBenchmarkFunctionalOnly() {
32 return getenv("BENCHMARK_FUNCTIONAL_TEST_ONLY") != nullptr;
33 }
34
BenchmarkArgs(benchmark::internal::Benchmark * b)35 void BenchmarkArgs(benchmark::internal::Benchmark* b) {
36 if (IsBenchmarkFunctionalOnly()) {
37 b->Ranges({{1024, 1024}, {4096, 4096}});
38 } else {
39 b->RangeMultiplier(8)->Ranges({{128, 8192}, {4096, 1024 * 512}});
40 }
41 }
42
RunQueryChecked(TraceProcessor * tp,const std::string & query)43 void RunQueryChecked(TraceProcessor* tp, const std::string& query) {
44 auto iter = tp->ExecuteQuery(query);
45 iter.Next();
46 PERFETTO_CHECK(iter.Status().ok());
47 }
48
49 } // namespace
50
BM_QueryResultSerializer_Mixed(benchmark::State & state)51 static void BM_QueryResultSerializer_Mixed(benchmark::State& state) {
52 auto tp = TraceProcessor::CreateInstance(Config());
53 RunQueryChecked(tp.get(), "create virtual table win using window;");
54 RunQueryChecked(tp.get(),
55 "update win set window_start=0, window_dur=50000, quantum=1 "
56 "where rowid = 0");
57 VectorType buf;
58 for (auto _ : state) {
59 auto iter = tp->ExecuteQuery(
60 "select dur || dur as x, ts, dur * 1.0 as dur, quantum_ts from win");
61 QueryResultSerializer serializer(std::move(iter));
62 serializer.set_batch_size_for_testing(
63 static_cast<uint32_t>(state.range(0)),
64 static_cast<uint32_t>(state.range(1)));
65 while (serializer.Serialize(&buf)) {
66 }
67 benchmark::DoNotOptimize(buf.data());
68 buf.clear();
69 }
70 benchmark::ClobberMemory();
71 }
72
BM_QueryResultSerializer_Strings(benchmark::State & state)73 static void BM_QueryResultSerializer_Strings(benchmark::State& state) {
74 auto tp = TraceProcessor::CreateInstance(Config());
75 RunQueryChecked(tp.get(), "create virtual table win using window;");
76 RunQueryChecked(tp.get(),
77 "update win set window_start=0, window_dur=100000, quantum=1 "
78 "where rowid = 0");
79 VectorType buf;
80 for (auto _ : state) {
81 auto iter = tp->ExecuteQuery(
82 "select ts || '-' || ts , (dur * 1.0) || dur from win");
83 QueryResultSerializer serializer(std::move(iter));
84 serializer.set_batch_size_for_testing(
85 static_cast<uint32_t>(state.range(0)),
86 static_cast<uint32_t>(state.range(1)));
87 while (serializer.Serialize(&buf)) {
88 }
89 benchmark::DoNotOptimize(buf.data());
90 buf.clear();
91 }
92 benchmark::ClobberMemory();
93 }
94
95 BENCHMARK(BM_QueryResultSerializer_Mixed)->Apply(BenchmarkArgs);
96 BENCHMARK(BM_QueryResultSerializer_Strings)->Apply(BenchmarkArgs);
97