blob: cc76ae8e70343c9d582491d2e0e624a8f7bfb443 [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) {
36 for (int i = 0; i < TEST_MAP_SIZE; i++) mBpfTestMap.writeValue(i, i, BPF_NOEXIST);
37 for (auto _ : state) mBpfTestMap.writeValue(state.range(0), state.range(0) + 1, BPF_EXIST);
38}
39
40BENCHMARK_DEFINE_F(BpfBenchMark, MapDeleteAddEntry)(benchmark::State& state) {
41 for (int i = 0; i < TEST_MAP_SIZE; i++) mBpfTestMap.writeValue(i, i, BPF_NOEXIST);
42 for (auto _ : state) {
43 mBpfTestMap.deleteValue(state.range(0));
44 mBpfTestMap.writeValue(state.range(0), state.range(0) + 1, BPF_NOEXIST);
45 }
46}
47
48BENCHMARK_REGISTER_F(BpfBenchMark, MapUpdateEntry)->Arg(1);
49BENCHMARK_REGISTER_F(BpfBenchMark, MapWriteNewEntry)->Arg(1);
50BENCHMARK_REGISTER_F(BpfBenchMark, MapDeleteAddEntry)->Arg(1);
51BENCHMARK_MAIN();