blob: 6ee83cb8ff2e324824a8ed7da0bca67a7e574e45 [file] [log] [blame]
Chenbo Fengd3e29562018-08-24 15:05:01 -07001/*
2 * Copyright (C) 2018 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 <benchmark/benchmark.h>
18
19#include "bpf/BpfMap.h"
20#include "bpf/BpfUtils.h"
21constexpr uint32_t TEST_MAP_SIZE = 10000;
22
23using android::bpf::BpfMap;
24
25class BpfBenchMark : public ::benchmark::Fixture {
26 public:
27 BpfBenchMark() : mBpfTestMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC) {}
28 BpfMap<uint32_t, uint32_t> mBpfTestMap;
29};
30
31BENCHMARK_DEFINE_F(BpfBenchMark, MapWriteNewEntry)(benchmark::State& state) {
32 for (auto _ : state) mBpfTestMap.writeValue(state.range(0), state.range(0), BPF_NOEXIST);
33}
34
35BENCHMARK_DEFINE_F(BpfBenchMark, MapUpdateEntry)(benchmark::State& state) {
Bernie Innocenti6f9fd902018-10-11 20:50:23 +090036 for (int i = 0; i < TEST_MAP_SIZE; i++) {
37 expectOk(mBpfTestMap.writeValue(i, i, BPF_NOEXIST));
38 }
39 for (auto _ : state) {
40 expectOk(mBpfTestMap.writeValue(state.range(0), state.range(0) + 1, BPF_EXIST));
41 }
Chenbo Fengd3e29562018-08-24 15:05:01 -070042}
43
44BENCHMARK_DEFINE_F(BpfBenchMark, MapDeleteAddEntry)(benchmark::State& state) {
Bernie Innocenti6f9fd902018-10-11 20:50:23 +090045 for (int i = 0; i < TEST_MAP_SIZE; i++) {
46 expectOk(mBpfTestMap.writeValue(i, i, BPF_NOEXIST));
47 }
Chenbo Fengd3e29562018-08-24 15:05:01 -070048 for (auto _ : state) {
Bernie Innocenti6f9fd902018-10-11 20:50:23 +090049 expectOk(mBpfTestMap.deleteValue(state.range(0)));
50 expectOk(mBpfTestMap.writeValue(state.range(0), state.range(0) + 1, BPF_NOEXIST));
Chenbo Fengd3e29562018-08-24 15:05:01 -070051 }
52}
53
54BENCHMARK_REGISTER_F(BpfBenchMark, MapUpdateEntry)->Arg(1);
55BENCHMARK_REGISTER_F(BpfBenchMark, MapWriteNewEntry)->Arg(1);
56BENCHMARK_REGISTER_F(BpfBenchMark, MapDeleteAddEntry)->Arg(1);
57BENCHMARK_MAIN();