1 // Copyright (C) 2019 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 <random>
16
17 #include <benchmark/benchmark.h>
18
19 #include "src/trace_processor/containers/row_map.h"
20
21 using perfetto::trace_processor::BitVector;
22 using perfetto::trace_processor::RowMap;
23
24 namespace {
25
26 static constexpr uint32_t kPoolSize = 100000;
27 static constexpr uint32_t kSize = 123456;
28
CreateRange(uint32_t end)29 RowMap CreateRange(uint32_t end) {
30 static constexpr uint32_t kRandomSeed = 32;
31 std::minstd_rand0 rnd_engine(kRandomSeed);
32
33 uint32_t start = rnd_engine() % end;
34 uint32_t size = rnd_engine() % (end - start);
35 return RowMap(start, start + size);
36 }
37
CreateIndexVector(uint32_t size,uint32_t mod)38 std::vector<uint32_t> CreateIndexVector(uint32_t size, uint32_t mod) {
39 static constexpr uint32_t kRandomSeed = 476;
40 std::minstd_rand0 rnd_engine(kRandomSeed);
41 std::vector<uint32_t> rows(size);
42 for (uint32_t i = 0; i < size; ++i) {
43 rows[i] = rnd_engine() % mod;
44 }
45 return rows;
46 }
47
CreateBitVector(uint32_t size)48 BitVector CreateBitVector(uint32_t size) {
49 static constexpr uint32_t kRandomSeed = 42;
50 std::minstd_rand0 rnd_engine(kRandomSeed);
51 BitVector bv;
52 for (uint32_t i = 0; i < size; ++i) {
53 if (rnd_engine() % 2) {
54 bv.AppendTrue();
55 } else {
56 bv.AppendFalse();
57 }
58 }
59 return bv;
60 }
61
BenchRowMapGet(benchmark::State & state,RowMap rm)62 void BenchRowMapGet(benchmark::State& state, RowMap rm) {
63 auto pool_vec = CreateIndexVector(kPoolSize, rm.size());
64
65 uint32_t pool_idx = 0;
66 for (auto _ : state) {
67 benchmark::DoNotOptimize(rm.Get(pool_vec[pool_idx]));
68 pool_idx = (pool_idx + 1) % kPoolSize;
69 }
70 }
71
72 template <typename Factory>
BenchRowMapInsertIntoEmpty(benchmark::State & state,Factory factory)73 void BenchRowMapInsertIntoEmpty(benchmark::State& state, Factory factory) {
74 auto pool_vec = CreateIndexVector(kPoolSize, kSize);
75
76 uint32_t pool_idx = 0;
77 for (auto _ : state) {
78 RowMap rm = factory();
79
80 rm.Insert(pool_vec[pool_idx]);
81 pool_idx = (pool_idx + 1) % kPoolSize;
82
83 benchmark::ClobberMemory();
84 }
85 }
86
BenchRowMapSelect(benchmark::State & state,RowMap rm,const RowMap & selector)87 void BenchRowMapSelect(benchmark::State& state,
88 RowMap rm,
89 const RowMap& selector) {
90 for (auto _ : state) {
91 benchmark::DoNotOptimize(rm.SelectRows(selector));
92 }
93 }
94
95 template <typename Factory>
BenchRowMapFilterInto(benchmark::State & state,RowMap rm,Factory factory)96 void BenchRowMapFilterInto(benchmark::State& state,
97 RowMap rm,
98 Factory factory) {
99 auto pool_vec = CreateIndexVector(kPoolSize, kSize);
100
101 uint32_t pool_idx = 0;
102 for (auto _ : state) {
103 state.PauseTiming();
104 RowMap out = factory();
105 state.ResumeTiming();
106
107 auto fn = [&pool_vec, pool_idx](uint32_t row) {
108 return pool_vec[pool_idx] != 0 && (row % pool_vec[pool_idx]) != 0;
109 };
110 rm.FilterInto(&out, fn);
111 pool_idx = (pool_idx + 1) % kPoolSize;
112
113 benchmark::ClobberMemory();
114 }
115 }
116
117 } // namespace
118
BM_RowMapRangeGet(benchmark::State & state)119 static void BM_RowMapRangeGet(benchmark::State& state) {
120 BenchRowMapGet(state, RowMap(CreateRange(kSize)));
121 }
122 BENCHMARK(BM_RowMapRangeGet);
123
BM_RowMapBvGet(benchmark::State & state)124 static void BM_RowMapBvGet(benchmark::State& state) {
125 BenchRowMapGet(state, RowMap(CreateBitVector(kSize)));
126 }
127 BENCHMARK(BM_RowMapBvGet);
128
BM_RowMapIvGet(benchmark::State & state)129 static void BM_RowMapIvGet(benchmark::State& state) {
130 BenchRowMapGet(state, RowMap(CreateIndexVector(kSize, kSize)));
131 }
132 BENCHMARK(BM_RowMapIvGet);
133
134 // TODO(lalitm): add benchmarks for IndexOf after BitVector is made faster.
135 // We can't add them right now because they are just too slow to run.
136
BM_RowMapRangeInsertIntoEmpty(benchmark::State & state)137 static void BM_RowMapRangeInsertIntoEmpty(benchmark::State& state) {
138 BenchRowMapInsertIntoEmpty(state, []() { return RowMap(0, 0); });
139 }
140 BENCHMARK(BM_RowMapRangeInsertIntoEmpty);
141
BM_RowMapBvInsertIntoEmpty(benchmark::State & state)142 static void BM_RowMapBvInsertIntoEmpty(benchmark::State& state) {
143 BenchRowMapInsertIntoEmpty(state, []() { return RowMap(BitVector{}); });
144 }
145 BENCHMARK(BM_RowMapBvInsertIntoEmpty);
146
BM_RowMapIvInsertIntoEmpty(benchmark::State & state)147 static void BM_RowMapIvInsertIntoEmpty(benchmark::State& state) {
148 BenchRowMapInsertIntoEmpty(state,
149 []() { return RowMap(std::vector<uint32_t>{}); });
150 }
151 BENCHMARK(BM_RowMapIvInsertIntoEmpty);
152
BM_RowMapSelectRangeWithRange(benchmark::State & state)153 static void BM_RowMapSelectRangeWithRange(benchmark::State& state) {
154 RowMap rm(CreateRange(kSize));
155 RowMap selector(CreateRange(rm.size()));
156 BenchRowMapSelect(state, std::move(rm), std::move(selector));
157 }
158 BENCHMARK(BM_RowMapSelectRangeWithRange);
159
BM_RowMapSelectRangeWithBv(benchmark::State & state)160 static void BM_RowMapSelectRangeWithBv(benchmark::State& state) {
161 RowMap rm(CreateRange(kSize));
162 RowMap selector(CreateBitVector(rm.size()));
163 BenchRowMapSelect(state, std::move(rm), std::move(selector));
164 }
165 BENCHMARK(BM_RowMapSelectRangeWithBv);
166
BM_RowMapSelectRangeWithIv(benchmark::State & state)167 static void BM_RowMapSelectRangeWithIv(benchmark::State& state) {
168 RowMap rm(CreateRange(kSize));
169 RowMap selector(CreateIndexVector(rm.size(), rm.size()));
170 BenchRowMapSelect(state, std::move(rm), std::move(selector));
171 }
172 BENCHMARK(BM_RowMapSelectRangeWithIv);
173
BM_RowMapSelectBvWithRange(benchmark::State & state)174 static void BM_RowMapSelectBvWithRange(benchmark::State& state) {
175 RowMap rm(CreateBitVector(kSize));
176 RowMap selector(CreateRange(rm.size()));
177 BenchRowMapSelect(state, std::move(rm), std::move(selector));
178 }
179 BENCHMARK(BM_RowMapSelectBvWithRange);
180
BM_RowMapSelectBvWithBv(benchmark::State & state)181 static void BM_RowMapSelectBvWithBv(benchmark::State& state) {
182 RowMap rm(CreateBitVector(kSize));
183 RowMap selector(CreateBitVector(rm.size()));
184 BenchRowMapSelect(state, std::move(rm), std::move(selector));
185 }
186 BENCHMARK(BM_RowMapSelectBvWithBv);
187
BM_RowMapSelectBvWithIv(benchmark::State & state)188 static void BM_RowMapSelectBvWithIv(benchmark::State& state) {
189 RowMap rm(CreateBitVector(kSize));
190 RowMap selector(CreateIndexVector(rm.size(), rm.size()));
191 BenchRowMapSelect(state, std::move(rm), std::move(selector));
192 }
193 BENCHMARK(BM_RowMapSelectBvWithIv);
194
BM_RowMapSelectIvWithRange(benchmark::State & state)195 static void BM_RowMapSelectIvWithRange(benchmark::State& state) {
196 RowMap rm(CreateIndexVector(kSize, kSize));
197 RowMap selector(CreateRange(rm.size()));
198 BenchRowMapSelect(state, std::move(rm), std::move(selector));
199 }
200 BENCHMARK(BM_RowMapSelectIvWithRange);
201
BM_RowMapSelectIvWithBv(benchmark::State & state)202 static void BM_RowMapSelectIvWithBv(benchmark::State& state) {
203 RowMap rm(CreateIndexVector(kSize, kSize));
204 RowMap selector(CreateBitVector(rm.size()));
205 BenchRowMapSelect(state, std::move(rm), std::move(selector));
206 }
207 BENCHMARK(BM_RowMapSelectIvWithBv);
208
BM_RowMapSelectIvWithIv(benchmark::State & state)209 static void BM_RowMapSelectIvWithIv(benchmark::State& state) {
210 RowMap rm(CreateIndexVector(kSize, kSize));
211 RowMap selector(CreateIndexVector(rm.size(), rm.size()));
212 BenchRowMapSelect(state, std::move(rm), std::move(selector));
213 }
214 BENCHMARK(BM_RowMapSelectIvWithIv);
215
BM_RowMapFilterIntoRangeWithRange(benchmark::State & state)216 static void BM_RowMapFilterIntoRangeWithRange(benchmark::State& state) {
217 RowMap rm(CreateRange(kSize));
218 uint32_t rm_size = rm.size();
219 BenchRowMapFilterInto(state, std::move(rm),
220 [rm_size]() { return RowMap(CreateRange(rm_size)); });
221 }
222 BENCHMARK(BM_RowMapFilterIntoRangeWithRange);
223
BM_RowMapFilterIntoRangeWithBv(benchmark::State & state)224 static void BM_RowMapFilterIntoRangeWithBv(benchmark::State& state) {
225 RowMap rm(CreateRange(kSize));
226 uint32_t rm_size = rm.size();
227 BenchRowMapFilterInto(state, std::move(rm), [rm_size]() {
228 return RowMap(CreateBitVector(rm_size));
229 });
230 }
231 BENCHMARK(BM_RowMapFilterIntoRangeWithBv);
232
BM_RowMapFilterIntoBvWithRange(benchmark::State & state)233 static void BM_RowMapFilterIntoBvWithRange(benchmark::State& state) {
234 RowMap rm(CreateBitVector(kSize));
235 uint32_t rm_size = rm.size();
236 BenchRowMapFilterInto(state, std::move(rm),
237 [rm_size]() { return RowMap(CreateRange(rm_size)); });
238 }
239 BENCHMARK(BM_RowMapFilterIntoBvWithRange);
240
BM_RowMapFilterIntoBvWithBv(benchmark::State & state)241 static void BM_RowMapFilterIntoBvWithBv(benchmark::State& state) {
242 RowMap rm(CreateBitVector(kSize));
243 uint32_t rm_size = rm.size();
244 BenchRowMapFilterInto(state, std::move(rm), [rm_size]() {
245 return RowMap(CreateBitVector(rm_size));
246 });
247 }
248 BENCHMARK(BM_RowMapFilterIntoBvWithBv);
249
BM_RowMapFilterIntoIvWithRange(benchmark::State & state)250 static void BM_RowMapFilterIntoIvWithRange(benchmark::State& state) {
251 RowMap rm(CreateIndexVector(kSize, kSize));
252 uint32_t rm_size = rm.size();
253 BenchRowMapFilterInto(state, std::move(rm),
254 [rm_size]() { return RowMap(CreateRange(rm_size)); });
255 }
256 BENCHMARK(BM_RowMapFilterIntoIvWithRange);
257
BM_RowMapFilterIntoIvWithBv(benchmark::State & state)258 static void BM_RowMapFilterIntoIvWithBv(benchmark::State& state) {
259 RowMap rm(CreateIndexVector(kSize, kSize));
260 uint32_t rm_size = rm.size();
261 BenchRowMapFilterInto(state, std::move(rm), [rm_size]() {
262 return RowMap(CreateBitVector(rm_size));
263 });
264 }
265 BENCHMARK(BM_RowMapFilterIntoIvWithBv);
266