Home | History | Annotate | Download | only in microbenchmarks
      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 
     19 /* Benchmark arenas */
     20 
     21 #include <benchmark/benchmark.h>
     22 #include "src/core/lib/gpr/arena.h"
     23 #include "test/cpp/microbenchmarks/helpers.h"
     24 #include "test/cpp/util/test_config.h"
     25 
     26 static void BM_Arena_NoOp(benchmark::State& state) {
     27   while (state.KeepRunning()) {
     28     gpr_arena_destroy(gpr_arena_create(state.range(0)));
     29   }
     30 }
     31 BENCHMARK(BM_Arena_NoOp)->Range(1, 1024 * 1024);
     32 
     33 static void BM_Arena_ManyAlloc(benchmark::State& state) {
     34   gpr_arena* a = gpr_arena_create(state.range(0));
     35   const size_t realloc_after =
     36       1024 * 1024 * 1024 / ((state.range(1) + 15) & 0xffffff0u);
     37   while (state.KeepRunning()) {
     38     gpr_arena_alloc(a, state.range(1));
     39     // periodically recreate arena to avoid OOM
     40     if (state.iterations() % realloc_after == 0) {
     41       gpr_arena_destroy(a);
     42       a = gpr_arena_create(state.range(0));
     43     }
     44   }
     45   gpr_arena_destroy(a);
     46 }
     47 BENCHMARK(BM_Arena_ManyAlloc)->Ranges({{1, 1024 * 1024}, {1, 32 * 1024}});
     48 
     49 static void BM_Arena_Batch(benchmark::State& state) {
     50   while (state.KeepRunning()) {
     51     gpr_arena* a = gpr_arena_create(state.range(0));
     52     for (int i = 0; i < state.range(1); i++) {
     53       gpr_arena_alloc(a, state.range(2));
     54     }
     55     gpr_arena_destroy(a);
     56   }
     57 }
     58 BENCHMARK(BM_Arena_Batch)->Ranges({{1, 64 * 1024}, {1, 64}, {1, 1024}});
     59 
     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 
     66 int main(int argc, char** argv) {
     67   ::benchmark::Initialize(&argc, argv);
     68   ::grpc::testing::InitTest(&argc, &argv, false);
     69   benchmark::RunTheBenchmarksNamespaced();
     70   return 0;
     71 }
     72