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/nullable_vector.h"
20 
21 namespace {
22 
23 static constexpr uint32_t kPoolSize = 100000;
24 static constexpr uint32_t kSize = 123456;
25 
26 }  // namespace
27 
BM_NullableVectorAppendNonNull(benchmark::State & state)28 static void BM_NullableVectorAppendNonNull(benchmark::State& state) {
29   std::vector<uint8_t> data_pool(kPoolSize);
30 
31   static constexpr uint32_t kRandomSeed = 42;
32   std::minstd_rand0 rnd_engine(kRandomSeed);
33   for (uint32_t i = 0; i < kPoolSize; ++i) {
34     data_pool[i] = rnd_engine() % std::numeric_limits<uint8_t>::max();
35   }
36 
37   perfetto::trace_processor::NullableVector<uint8_t> sv;
38   uint32_t pool_idx = 0;
39   for (auto _ : state) {
40     sv.Append(data_pool[pool_idx]);
41     pool_idx = (pool_idx + 1) % kPoolSize;
42     benchmark::ClobberMemory();
43   }
44 }
45 BENCHMARK(BM_NullableVectorAppendNonNull);
46 
BM_NullableVectorGetNonNull(benchmark::State & state)47 static void BM_NullableVectorGetNonNull(benchmark::State& state) {
48   std::vector<uint32_t> idx_pool(kPoolSize);
49 
50   perfetto::trace_processor::NullableVector<uint8_t> sv;
51   static constexpr uint32_t kRandomSeed = 42;
52   std::minstd_rand0 rnd_engine(kRandomSeed);
53   for (uint32_t i = 0; i < kSize; ++i) {
54     sv.Append(rnd_engine() % std::numeric_limits<uint8_t>::max());
55   }
56   for (uint32_t i = 0; i < kPoolSize; ++i) {
57     idx_pool[i] = rnd_engine() % kSize;
58   }
59 
60   uint32_t pool_idx = 0;
61   for (auto _ : state) {
62     benchmark::DoNotOptimize(sv.Get(idx_pool[pool_idx]));
63     pool_idx = (pool_idx + 1) % kPoolSize;
64   }
65 }
66 BENCHMARK(BM_NullableVectorGetNonNull);
67