bm_arena.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. *
  3. * Copyright 2017 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. /* Benchmark arenas */
  19. #include <benchmark/benchmark.h>
  20. #include "src/core/lib/resource_quota/arena.h"
  21. #include "src/core/lib/resource_quota/resource_quota.h"
  22. #include "test/core/util/test_config.h"
  23. #include "test/cpp/microbenchmarks/helpers.h"
  24. #include "test/cpp/util/test_config.h"
  25. using grpc_core::Arena;
  26. static auto* g_memory_allocator = new grpc_core::MemoryAllocator(
  27. grpc_core::ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator(
  28. "test"));
  29. static void BM_Arena_NoOp(benchmark::State& state) {
  30. for (auto _ : state) {
  31. Arena::Create(state.range(0), g_memory_allocator)->Destroy();
  32. }
  33. }
  34. BENCHMARK(BM_Arena_NoOp)->Range(1, 1024 * 1024);
  35. static void BM_Arena_ManyAlloc(benchmark::State& state) {
  36. Arena* a = Arena::Create(state.range(0), g_memory_allocator);
  37. const size_t realloc_after =
  38. 1024 * 1024 * 1024 / ((state.range(1) + 15) & 0xffffff0u);
  39. while (state.KeepRunning()) {
  40. a->Alloc(state.range(1));
  41. // periodically recreate arena to avoid OOM
  42. if (state.iterations() % realloc_after == 0) {
  43. a->Destroy();
  44. a = Arena::Create(state.range(0), g_memory_allocator);
  45. }
  46. }
  47. a->Destroy();
  48. }
  49. BENCHMARK(BM_Arena_ManyAlloc)->Ranges({{1, 1024 * 1024}, {1, 32 * 1024}});
  50. static void BM_Arena_Batch(benchmark::State& state) {
  51. for (auto _ : state) {
  52. Arena* a = Arena::Create(state.range(0), g_memory_allocator);
  53. for (int i = 0; i < state.range(1); i++) {
  54. a->Alloc(state.range(2));
  55. }
  56. a->Destroy();
  57. }
  58. }
  59. BENCHMARK(BM_Arena_Batch)->Ranges({{1, 64 * 1024}, {1, 64}, {1, 1024}});
  60. // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
  61. // and others do not. This allows us to support both modes.
  62. namespace benchmark {
  63. void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
  64. } // namespace benchmark
  65. int main(int argc, char** argv) {
  66. grpc::testing::TestEnvironment env(argc, argv);
  67. ::benchmark::Initialize(&argc, argv);
  68. grpc::testing::InitTest(&argc, &argv, false);
  69. benchmark::RunTheBenchmarksNamespaced();
  70. return 0;
  71. }