aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/cpp/microbenchmarks/bm_closure.cc
diff options
context:
space:
mode:
authorGravatar Craig Tiller <ctiller@google.com>2017-03-03 08:50:25 -0800
committerGravatar Craig Tiller <ctiller@google.com>2017-03-03 08:50:25 -0800
commit5e3215338f3f69c626cced758e9c49e03252aa24 (patch)
tree2b52ffd9a834878bc54c56b8dd855be539c2fe7f /test/cpp/microbenchmarks/bm_closure.cc
parentc9bf9ba7a7e8c8227ca2594eab85b5ca1abfbccd (diff)
Move helpers to a common place, use them everywhere
Diffstat (limited to 'test/cpp/microbenchmarks/bm_closure.cc')
-rw-r--r--test/cpp/microbenchmarks/bm_closure.cc110
1 files changed, 46 insertions, 64 deletions
diff --git a/test/cpp/microbenchmarks/bm_closure.cc b/test/cpp/microbenchmarks/bm_closure.cc
index 1f54e8c8b1..8111c9c9fa 100644
--- a/test/cpp/microbenchmarks/bm_closure.cc
+++ b/test/cpp/microbenchmarks/bm_closure.cc
@@ -1,6 +1,6 @@
/*
*
- * Copyright 2015, Google Inc.
+ * Copyright 2017, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -42,86 +42,49 @@ extern "C" {
#include "src/core/lib/support/spinlock.h"
}
+#include "test/cpp/microbenchmarks/helpers.h"
#include "third_party/benchmark/include/benchmark/benchmark.h"
-#include <sstream>
-
#ifdef GPR_LOW_LEVEL_COUNTERS
extern "C" gpr_atm gpr_mu_locks;
#endif
-static class InitializeStuff {
- public:
- InitializeStuff() { grpc_init(); }
- ~InitializeStuff() { grpc_shutdown(); }
-} initialize_stuff;
-
-class TrackCounters {
- public:
- TrackCounters(benchmark::State& state) : state_(state) {}
-
- ~TrackCounters() {
- std::ostringstream out;
-#ifdef GPR_LOW_LEVEL_COUNTERS
- out << " locks/iter:" << ((double)(gpr_atm_no_barrier_load(&gpr_mu_locks) -
- mu_locks_at_start_) /
- (double)state_.iterations())
- << " atm_cas/iter:"
- << ((double)(gpr_atm_no_barrier_load(&gpr_counter_atm_cas) -
- atm_cas_at_start_) /
- (double)state_.iterations())
- << " atm_add/iter:"
- << ((double)(gpr_atm_no_barrier_load(&gpr_counter_atm_add) -
- atm_add_at_start_) /
- (double)state_.iterations());
-#endif
- state_.SetLabel(out.str());
- }
-
- private:
- benchmark::State& state_;
-#ifdef GPR_LOW_LEVEL_COUNTERS
- const size_t mu_locks_at_start_ = gpr_atm_no_barrier_load(&gpr_mu_locks);
- const size_t atm_cas_at_start_ =
- gpr_atm_no_barrier_load(&gpr_counter_atm_cas);
- const size_t atm_add_at_start_ =
- gpr_atm_no_barrier_load(&gpr_counter_atm_add);
-#endif
-};
-
static void BM_NoOpExecCtx(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
while (state.KeepRunning()) {
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
grpc_exec_ctx_finish(&exec_ctx);
}
+ track_counters.Finish(state);
}
BENCHMARK(BM_NoOpExecCtx);
static void BM_WellFlushed(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
while (state.KeepRunning()) {
grpc_exec_ctx_flush(&exec_ctx);
}
grpc_exec_ctx_finish(&exec_ctx);
+ track_counters.Finish(state);
}
BENCHMARK(BM_WellFlushed);
static void DoNothing(grpc_exec_ctx* exec_ctx, void* arg, grpc_error* error) {}
static void BM_ClosureInitAgainstExecCtx(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
grpc_closure c;
while (state.KeepRunning()) {
benchmark::DoNotOptimize(
grpc_closure_init(&c, DoNothing, NULL, grpc_schedule_on_exec_ctx));
}
+ track_counters.Finish(state);
}
BENCHMARK(BM_ClosureInitAgainstExecCtx);
static void BM_ClosureInitAgainstCombiner(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
grpc_combiner* combiner = grpc_combiner_create(NULL);
grpc_closure c;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
@@ -131,11 +94,12 @@ static void BM_ClosureInitAgainstCombiner(benchmark::State& state) {
}
GRPC_COMBINER_UNREF(&exec_ctx, combiner, "finished");
grpc_exec_ctx_finish(&exec_ctx);
+ track_counters.Finish(state);
}
BENCHMARK(BM_ClosureInitAgainstCombiner);
static void BM_ClosureRunOnExecCtx(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
grpc_closure c;
grpc_closure_init(&c, DoNothing, NULL, grpc_schedule_on_exec_ctx);
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
@@ -144,11 +108,12 @@ static void BM_ClosureRunOnExecCtx(benchmark::State& state) {
grpc_exec_ctx_flush(&exec_ctx);
}
grpc_exec_ctx_finish(&exec_ctx);
+ track_counters.Finish(state);
}
BENCHMARK(BM_ClosureRunOnExecCtx);
static void BM_ClosureCreateAndRun(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
while (state.KeepRunning()) {
grpc_closure_run(&exec_ctx, grpc_closure_create(DoNothing, NULL,
@@ -156,11 +121,12 @@ static void BM_ClosureCreateAndRun(benchmark::State& state) {
GRPC_ERROR_NONE);
}
grpc_exec_ctx_finish(&exec_ctx);
+ track_counters.Finish(state);
}
BENCHMARK(BM_ClosureCreateAndRun);
static void BM_ClosureInitAndRun(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
grpc_closure c;
while (state.KeepRunning()) {
@@ -169,11 +135,12 @@ static void BM_ClosureInitAndRun(benchmark::State& state) {
GRPC_ERROR_NONE);
}
grpc_exec_ctx_finish(&exec_ctx);
+ track_counters.Finish(state);
}
BENCHMARK(BM_ClosureInitAndRun);
static void BM_ClosureSchedOnExecCtx(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
grpc_closure c;
grpc_closure_init(&c, DoNothing, NULL, grpc_schedule_on_exec_ctx);
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
@@ -182,11 +149,12 @@ static void BM_ClosureSchedOnExecCtx(benchmark::State& state) {
grpc_exec_ctx_flush(&exec_ctx);
}
grpc_exec_ctx_finish(&exec_ctx);
+ track_counters.Finish(state);
}
BENCHMARK(BM_ClosureSchedOnExecCtx);
static void BM_ClosureSched2OnExecCtx(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
grpc_closure c1;
grpc_closure c2;
grpc_closure_init(&c1, DoNothing, NULL, grpc_schedule_on_exec_ctx);
@@ -198,11 +166,12 @@ static void BM_ClosureSched2OnExecCtx(benchmark::State& state) {
grpc_exec_ctx_flush(&exec_ctx);
}
grpc_exec_ctx_finish(&exec_ctx);
+ track_counters.Finish(state);
}
BENCHMARK(BM_ClosureSched2OnExecCtx);
static void BM_ClosureSched3OnExecCtx(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
grpc_closure c1;
grpc_closure c2;
grpc_closure c3;
@@ -217,11 +186,12 @@ static void BM_ClosureSched3OnExecCtx(benchmark::State& state) {
grpc_exec_ctx_flush(&exec_ctx);
}
grpc_exec_ctx_finish(&exec_ctx);
+ track_counters.Finish(state);
}
BENCHMARK(BM_ClosureSched3OnExecCtx);
static void BM_AcquireMutex(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
// for comparison with the combiner stuff below
gpr_mu mu;
gpr_mu_init(&mu);
@@ -232,11 +202,12 @@ static void BM_AcquireMutex(benchmark::State& state) {
gpr_mu_unlock(&mu);
}
grpc_exec_ctx_finish(&exec_ctx);
+ track_counters.Finish(state);
}
BENCHMARK(BM_AcquireMutex);
static void BM_TryAcquireMutex(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
// for comparison with the combiner stuff below
gpr_mu mu;
gpr_mu_init(&mu);
@@ -250,11 +221,12 @@ static void BM_TryAcquireMutex(benchmark::State& state) {
}
}
grpc_exec_ctx_finish(&exec_ctx);
+ track_counters.Finish(state);
}
BENCHMARK(BM_TryAcquireMutex);
static void BM_AcquireSpinlock(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
// for comparison with the combiner stuff below
gpr_spinlock mu = GPR_SPINLOCK_INITIALIZER;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
@@ -264,11 +236,12 @@ static void BM_AcquireSpinlock(benchmark::State& state) {
gpr_spinlock_unlock(&mu);
}
grpc_exec_ctx_finish(&exec_ctx);
+ track_counters.Finish(state);
}
BENCHMARK(BM_AcquireSpinlock);
static void BM_TryAcquireSpinlock(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
// for comparison with the combiner stuff below
gpr_spinlock mu = GPR_SPINLOCK_INITIALIZER;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
@@ -281,11 +254,12 @@ static void BM_TryAcquireSpinlock(benchmark::State& state) {
}
}
grpc_exec_ctx_finish(&exec_ctx);
+ track_counters.Finish(state);
}
BENCHMARK(BM_TryAcquireSpinlock);
static void BM_ClosureSchedOnCombiner(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
grpc_combiner* combiner = grpc_combiner_create(NULL);
grpc_closure c;
grpc_closure_init(&c, DoNothing, NULL,
@@ -297,11 +271,12 @@ static void BM_ClosureSchedOnCombiner(benchmark::State& state) {
}
GRPC_COMBINER_UNREF(&exec_ctx, combiner, "finished");
grpc_exec_ctx_finish(&exec_ctx);
+ track_counters.Finish(state);
}
BENCHMARK(BM_ClosureSchedOnCombiner);
static void BM_ClosureSched2OnCombiner(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
grpc_combiner* combiner = grpc_combiner_create(NULL);
grpc_closure c1;
grpc_closure c2;
@@ -317,11 +292,12 @@ static void BM_ClosureSched2OnCombiner(benchmark::State& state) {
}
GRPC_COMBINER_UNREF(&exec_ctx, combiner, "finished");
grpc_exec_ctx_finish(&exec_ctx);
+ track_counters.Finish(state);
}
BENCHMARK(BM_ClosureSched2OnCombiner);
static void BM_ClosureSched3OnCombiner(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
grpc_combiner* combiner = grpc_combiner_create(NULL);
grpc_closure c1;
grpc_closure c2;
@@ -341,11 +317,12 @@ static void BM_ClosureSched3OnCombiner(benchmark::State& state) {
}
GRPC_COMBINER_UNREF(&exec_ctx, combiner, "finished");
grpc_exec_ctx_finish(&exec_ctx);
+ track_counters.Finish(state);
}
BENCHMARK(BM_ClosureSched3OnCombiner);
static void BM_ClosureSched2OnTwoCombiners(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
grpc_combiner* combiner1 = grpc_combiner_create(NULL);
grpc_combiner* combiner2 = grpc_combiner_create(NULL);
grpc_closure c1;
@@ -363,11 +340,12 @@ static void BM_ClosureSched2OnTwoCombiners(benchmark::State& state) {
GRPC_COMBINER_UNREF(&exec_ctx, combiner1, "finished");
GRPC_COMBINER_UNREF(&exec_ctx, combiner2, "finished");
grpc_exec_ctx_finish(&exec_ctx);
+ track_counters.Finish(state);
}
BENCHMARK(BM_ClosureSched2OnTwoCombiners);
static void BM_ClosureSched4OnTwoCombiners(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
grpc_combiner* combiner1 = grpc_combiner_create(NULL);
grpc_combiner* combiner2 = grpc_combiner_create(NULL);
grpc_closure c1;
@@ -393,6 +371,7 @@ static void BM_ClosureSched4OnTwoCombiners(benchmark::State& state) {
GRPC_COMBINER_UNREF(&exec_ctx, combiner1, "finished");
GRPC_COMBINER_UNREF(&exec_ctx, combiner2, "finished");
grpc_exec_ctx_finish(&exec_ctx);
+ track_counters.Finish(state);
}
BENCHMARK(BM_ClosureSched4OnTwoCombiners);
@@ -428,16 +407,17 @@ class Rescheduler {
};
static void BM_ClosureReschedOnExecCtx(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Rescheduler r(state, grpc_schedule_on_exec_ctx);
r.ScheduleFirst(&exec_ctx);
grpc_exec_ctx_finish(&exec_ctx);
+ track_counters.Finish(state);
}
BENCHMARK(BM_ClosureReschedOnExecCtx);
static void BM_ClosureReschedOnCombiner(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
grpc_combiner* combiner = grpc_combiner_create(NULL);
Rescheduler r(state, grpc_combiner_scheduler(combiner, false));
@@ -445,11 +425,12 @@ static void BM_ClosureReschedOnCombiner(benchmark::State& state) {
grpc_exec_ctx_flush(&exec_ctx);
GRPC_COMBINER_UNREF(&exec_ctx, combiner, "finished");
grpc_exec_ctx_finish(&exec_ctx);
+ track_counters.Finish(state);
}
BENCHMARK(BM_ClosureReschedOnCombiner);
static void BM_ClosureReschedOnCombinerFinally(benchmark::State& state) {
- TrackCounters track_counters(state);
+ TrackCounters track_counters;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
grpc_combiner* combiner = grpc_combiner_create(NULL);
Rescheduler r(state, grpc_combiner_finally_scheduler(combiner, false));
@@ -458,6 +439,7 @@ static void BM_ClosureReschedOnCombinerFinally(benchmark::State& state) {
grpc_exec_ctx_flush(&exec_ctx);
GRPC_COMBINER_UNREF(&exec_ctx, combiner, "finished");
grpc_exec_ctx_finish(&exec_ctx);
+ track_counters.Finish(state);
}
BENCHMARK(BM_ClosureReschedOnCombinerFinally);