Chenbo Feng | d3e2956 | 2018-08-24 15:05:01 -0700 | [diff] [blame] | 1 | /* |
| 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" |
| 21 | constexpr uint32_t TEST_MAP_SIZE = 10000; |
| 22 | |
| 23 | using android::bpf::BpfMap; |
| 24 | |
| 25 | class 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 | |
| 31 | BENCHMARK_DEFINE_F(BpfBenchMark, MapWriteNewEntry)(benchmark::State& state) { |
| 32 | for (auto _ : state) mBpfTestMap.writeValue(state.range(0), state.range(0), BPF_NOEXIST); |
| 33 | } |
| 34 | |
| 35 | BENCHMARK_DEFINE_F(BpfBenchMark, MapUpdateEntry)(benchmark::State& state) { |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 36 | 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 Feng | d3e2956 | 2018-08-24 15:05:01 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | BENCHMARK_DEFINE_F(BpfBenchMark, MapDeleteAddEntry)(benchmark::State& state) { |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 45 | for (int i = 0; i < TEST_MAP_SIZE; i++) { |
| 46 | expectOk(mBpfTestMap.writeValue(i, i, BPF_NOEXIST)); |
| 47 | } |
Chenbo Feng | d3e2956 | 2018-08-24 15:05:01 -0700 | [diff] [blame] | 48 | for (auto _ : state) { |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 49 | expectOk(mBpfTestMap.deleteValue(state.range(0))); |
| 50 | expectOk(mBpfTestMap.writeValue(state.range(0), state.range(0) + 1, BPF_NOEXIST)); |
Chenbo Feng | d3e2956 | 2018-08-24 15:05:01 -0700 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | BENCHMARK_REGISTER_F(BpfBenchMark, MapUpdateEntry)->Arg(1); |
| 55 | BENCHMARK_REGISTER_F(BpfBenchMark, MapWriteNewEntry)->Arg(1); |
| 56 | BENCHMARK_REGISTER_F(BpfBenchMark, MapDeleteAddEntry)->Arg(1); |
| 57 | BENCHMARK_MAIN(); |