bpf_benchmark.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #include <android-base/stringprintf.h>
  17. #include <benchmark/benchmark.h>
  18. #include "bpf/BpfMap.h"
  19. #include "bpf/BpfUtils.h"
  20. constexpr uint32_t TEST_MAP_SIZE = 10000;
  21. using android::base::StringPrintf;
  22. using android::bpf::BpfMap;
  23. class BpfBenchMark : public ::benchmark::Fixture {
  24. public:
  25. BpfBenchMark() : mBpfTestMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC) {}
  26. BpfMap<uint32_t, uint32_t> mBpfTestMap;
  27. };
  28. BENCHMARK_DEFINE_F(BpfBenchMark, MapWriteNewEntry)(benchmark::State& state) {
  29. for (auto _ : state) {
  30. expectOk(mBpfTestMap.writeValue(state.range(0), state.range(0), BPF_NOEXIST));
  31. }
  32. }
  33. BENCHMARK_DEFINE_F(BpfBenchMark, MapUpdateEntry)(benchmark::State& state) {
  34. for (uint32_t i = 0; i < TEST_MAP_SIZE; i++) {
  35. expectOk(mBpfTestMap.writeValue(i, i, BPF_NOEXIST));
  36. }
  37. for (auto _ : state) {
  38. expectOk(mBpfTestMap.writeValue(state.range(0), state.range(0) + 1, BPF_EXIST));
  39. }
  40. }
  41. BENCHMARK_DEFINE_F(BpfBenchMark, MapDeleteAddEntry)(benchmark::State& state) {
  42. for (uint32_t i = 0; i < TEST_MAP_SIZE; i++) {
  43. expectOk(mBpfTestMap.writeValue(i, i, BPF_NOEXIST));
  44. }
  45. for (auto _ : state) {
  46. expectOk(mBpfTestMap.deleteValue(state.range(0)));
  47. expectOk(mBpfTestMap.writeValue(state.range(0), state.range(0) + 1, BPF_NOEXIST));
  48. }
  49. }
  50. BENCHMARK_DEFINE_F(BpfBenchMark, WaitForRcu)(benchmark::State& state) {
  51. for (auto _ : state) {
  52. int ret = android::bpf::synchronizeKernelRCU();
  53. if (ret) {
  54. state.SkipWithError(
  55. StringPrintf("synchronizeKernelRCU() failed with errno=%d", -ret).c_str());
  56. }
  57. }
  58. }
  59. BENCHMARK_REGISTER_F(BpfBenchMark, MapUpdateEntry)->Arg(1);
  60. BENCHMARK_REGISTER_F(BpfBenchMark, MapWriteNewEntry)->Arg(1);
  61. BENCHMARK_REGISTER_F(BpfBenchMark, MapDeleteAddEntry)->Arg(1);
  62. BENCHMARK_REGISTER_F(BpfBenchMark, WaitForRcu)->Arg(1);
  63. BENCHMARK_MAIN();