aboutsummaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorGravatar Soheil Hassas Yeganeh <soheil@google.com>2018-12-21 21:35:30 -0500
committerGravatar GitHub <noreply@github.com>2018-12-21 21:35:30 -0500
commitc3eb206897dcfc9fa77391501b233d7a2b0c79f7 (patch)
tree95ab4bc4dc4f01a640527799bb1fd304759d1fd0 /test
parent0f5b1d6bdfab97fd3490142a837c48e768027111 (diff)
parent2e017da58aeb335e79f7bbf0797aad45a59d293b (diff)
Merge pull request #17582 from soheilhy/worktree-timer-bench
Add microbenchmarks for grpc_timer
Diffstat (limited to 'test')
-rw-r--r--test/cpp/microbenchmarks/BUILD7
-rw-r--r--test/cpp/microbenchmarks/bm_timer.cc118
2 files changed, 125 insertions, 0 deletions
diff --git a/test/cpp/microbenchmarks/BUILD b/test/cpp/microbenchmarks/BUILD
index b5890bece7..a29462f78f 100644
--- a/test/cpp/microbenchmarks/BUILD
+++ b/test/cpp/microbenchmarks/BUILD
@@ -189,3 +189,10 @@ grpc_cc_binary(
"//src/proto/grpc/testing:echo_proto",
],
)
+
+grpc_cc_binary(
+ name = "bm_timer",
+ testonly = 1,
+ srcs = ["bm_timer.cc"],
+ deps = [":helpers"],
+)
diff --git a/test/cpp/microbenchmarks/bm_timer.cc b/test/cpp/microbenchmarks/bm_timer.cc
new file mode 100644
index 0000000000..f5a411251b
--- /dev/null
+++ b/test/cpp/microbenchmarks/bm_timer.cc
@@ -0,0 +1,118 @@
+/*
+ *
+ * Copyright 2017 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <benchmark/benchmark.h>
+#include <string.h>
+#include <atomic>
+#include <vector>
+
+#include <grpc/grpc.h>
+#include <grpc/support/alloc.h>
+#include <grpc/support/log.h>
+#include "test/cpp/microbenchmarks/helpers.h"
+#include "test/cpp/util/test_config.h"
+
+#include "src/core/lib/iomgr/timer.h"
+
+namespace grpc {
+namespace testing {
+
+auto& force_library_initialization = Library::get();
+
+struct TimerClosure {
+ grpc_timer timer;
+ grpc_closure closure;
+};
+
+static void BM_InitCancelTimer(benchmark::State& state) {
+ constexpr int kTimerCount = 1024;
+ TrackCounters track_counters;
+ grpc_core::ExecCtx exec_ctx;
+ std::vector<TimerClosure> timer_closures(kTimerCount);
+ int i = 0;
+ while (state.KeepRunning()) {
+ TimerClosure* timer_closure = &timer_closures[i++ % kTimerCount];
+ GRPC_CLOSURE_INIT(&timer_closure->closure,
+ [](void* /*args*/, grpc_error* /*err*/) {}, nullptr,
+ grpc_schedule_on_exec_ctx);
+ grpc_timer_init(&timer_closure->timer, GRPC_MILLIS_INF_FUTURE,
+ &timer_closure->closure);
+ grpc_timer_cancel(&timer_closure->timer);
+ exec_ctx.Flush();
+ }
+ track_counters.Finish(state);
+}
+BENCHMARK(BM_InitCancelTimer);
+
+static void BM_TimerBatch(benchmark::State& state) {
+ constexpr int kTimerCount = 1024;
+ const bool check = state.range(0);
+ const bool reverse = state.range(1);
+
+ const grpc_millis start =
+ reverse ? GRPC_MILLIS_INF_FUTURE : GRPC_MILLIS_INF_FUTURE - kTimerCount;
+ const grpc_millis end =
+ reverse ? GRPC_MILLIS_INF_FUTURE - kTimerCount : GRPC_MILLIS_INF_FUTURE;
+ const grpc_millis increment = reverse ? -1 : 1;
+
+ TrackCounters track_counters;
+ grpc_core::ExecCtx exec_ctx;
+ std::vector<TimerClosure> timer_closures(kTimerCount);
+ while (state.KeepRunning()) {
+ for (grpc_millis deadline = start; deadline != end; deadline += increment) {
+ TimerClosure* timer_closure = &timer_closures[deadline % kTimerCount];
+ GRPC_CLOSURE_INIT(&timer_closure->closure,
+ [](void* /*args*/, grpc_error* /*err*/) {}, nullptr,
+ grpc_schedule_on_exec_ctx);
+
+ grpc_timer_init(&timer_closure->timer, deadline, &timer_closure->closure);
+ }
+ if (check) {
+ grpc_millis next;
+ grpc_timer_check(&next);
+ }
+ for (grpc_millis deadline = start; deadline != end; deadline += increment) {
+ TimerClosure* timer_closure = &timer_closures[deadline % kTimerCount];
+ grpc_timer_cancel(&timer_closure->timer);
+ }
+ exec_ctx.Flush();
+ }
+ track_counters.Finish(state);
+}
+BENCHMARK(BM_TimerBatch)
+ ->Args({/*check=*/false, /*reverse=*/false})
+ ->Args({/*check=*/false, /*reverse=*/true})
+ ->Args({/*check=*/true, /*reverse=*/false})
+ ->Args({/*check=*/true, /*reverse=*/true})
+ ->ThreadRange(1, 128);
+
+} // namespace testing
+} // namespace grpc
+
+// Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
+// and others do not. This allows us to support both modes.
+namespace benchmark {
+void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
+} // namespace benchmark
+
+int main(int argc, char** argv) {
+ ::benchmark::Initialize(&argc, argv);
+ ::grpc::testing::InitTest(&argc, &argv, false);
+ benchmark::RunTheBenchmarksNamespaced();
+ return 0;
+}