| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 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 |
/* This benchmark exists to ensure that the benchmark integration is |
| 20 |
* working */ |
| 21 |
|
| 22 |
#include <benchmark/benchmark.h> |
| 23 |
|
| 24 |
#include <grpc/grpc.h> |
| 25 |
#include <grpc/support/log.h> |
| 26 |
#include <grpcpp/completion_queue.h> |
| 27 |
#include <grpcpp/impl/grpc_library.h> |
| 28 |
|
| 29 |
#include "src/core/lib/surface/completion_queue.h" |
| 30 |
#include "test/core/util/test_config.h" |
| 31 |
#include "test/cpp/microbenchmarks/helpers.h" |
| 32 |
#include "test/cpp/util/test_config.h" |
| 33 |
|
| 34 |
namespace grpc { |
| 35 |
namespace testing { |
| 36 |
|
| 37 |
static void BM_CreateDestroyCpp(benchmark::State& state) { |
| 38 |
for (auto _ : state) { |
| 39 |
CompletionQueue cq; |
| 40 |
} |
| 41 |
} |
| 42 |
BENCHMARK(BM_CreateDestroyCpp); |
| 43 |
|
| 44 |
/* Create cq using a different constructor */ |
| 45 |
static void BM_CreateDestroyCpp2(benchmark::State& state) { |
| 46 |
for (auto _ : state) { |
| 47 |
grpc_completion_queue* core_cq = |
| 48 |
grpc_completion_queue_create_for_next(nullptr); |
| 49 |
CompletionQueue cq(core_cq); |
| 50 |
} |
| 51 |
} |
| 52 |
BENCHMARK(BM_CreateDestroyCpp2); |
| 53 |
|
| 54 |
static void BM_CreateDestroyCore(benchmark::State& state) { |
| 55 |
for (auto _ : state) { |
| 56 |
// TODO(sreek): Templatize this benchmark and pass completion type and |
| 57 |
// polling type as parameters |
| 58 |
grpc_completion_queue_destroy( |
| 59 |
grpc_completion_queue_create_for_next(nullptr)); |
| 60 |
} |
| 61 |
} |
| 62 |
BENCHMARK(BM_CreateDestroyCore); |
| 63 |
|
| 64 |
static void DoneWithCompletionOnStack(void* /*arg*/, |
| 65 |
grpc_cq_completion* /*completion*/) {} |
| 66 |
|
| 67 |
static void DoneWithCompletionOnHeap(void* /*arg*/, |
| 68 |
grpc_cq_completion* completion) { |
| 69 |
delete completion; |
| 70 |
} |
| 71 |
|
| 72 |
class PhonyTag final : public internal::CompletionQueueTag { |
| 73 |
public: |
| 74 |
bool FinalizeResult(void** /*tag*/, bool* /*status*/) override { |
| 75 |
return true; |
| 76 |
} |
| 77 |
}; |
| 78 |
|
| 79 |
static void BM_Pass1Cpp(benchmark::State& state) { |
| 80 |
CompletionQueue cq; |
| 81 |
grpc_completion_queue* c_cq = cq.cq(); |
| 82 |
for (auto _ : state) { |
| 83 |
grpc_cq_completion completion; |
| 84 |
PhonyTag phony_tag; |
| 85 |
grpc_core::ExecCtx exec_ctx; |
| 86 |
GPR_ASSERT(grpc_cq_begin_op(c_cq, &phony_tag)); |
| 87 |
grpc_cq_end_op(c_cq, &phony_tag, absl::OkStatus(), |
| 88 |
DoneWithCompletionOnStack, nullptr, &completion); |
| 89 |
|
| 90 |
void* tag; |
| 91 |
bool ok; |
| 92 |
cq.Next(&tag, &ok); |
| 93 |
} |
| 94 |
} |
| 95 |
BENCHMARK(BM_Pass1Cpp); |
| 96 |
|
| 97 |
static void BM_Pass1Core(benchmark::State& state) { |
| 98 |
// TODO(sreek): Templatize this benchmark and pass polling_type as a param |
| 99 |
grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr); |
| 100 |
gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC); |
| 101 |
for (auto _ : state) { |
| 102 |
grpc_cq_completion completion; |
| 103 |
grpc_core::ExecCtx exec_ctx; |
| 104 |
GPR_ASSERT(grpc_cq_begin_op(cq, nullptr)); |
| 105 |
grpc_cq_end_op(cq, nullptr, absl::OkStatus(), DoneWithCompletionOnStack, |
| 106 |
nullptr, &completion); |
| 107 |
|
| 108 |
grpc_completion_queue_next(cq, deadline, nullptr); |
| 109 |
} |
| 110 |
grpc_completion_queue_destroy(cq); |
| 111 |
} |
| 112 |
BENCHMARK(BM_Pass1Core); |
| 113 |
|
| 114 |
static void BM_Pluck1Core(benchmark::State& state) { |
| 115 |
// TODO(sreek): Templatize this benchmark and pass polling_type as a param |
| 116 |
grpc_completion_queue* cq = grpc_completion_queue_create_for_pluck(nullptr); |
| 117 |
gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC); |
| 118 |
for (auto _ : state) { |
| 119 |
grpc_cq_completion completion; |
| 120 |
grpc_core::ExecCtx exec_ctx; |
| 121 |
GPR_ASSERT(grpc_cq_begin_op(cq, nullptr)); |
| 122 |
grpc_cq_end_op(cq, nullptr, absl::OkStatus(), DoneWithCompletionOnStack, |
| 123 |
nullptr, &completion); |
| 124 |
|
| 125 |
grpc_completion_queue_pluck(cq, nullptr, deadline, nullptr); |
| 126 |
} |
| 127 |
grpc_completion_queue_destroy(cq); |
| 128 |
} |
| 129 |
BENCHMARK(BM_Pluck1Core); |
| 130 |
|
| 131 |
static void BM_EmptyCore(benchmark::State& state) { |
| 132 |
// TODO(sreek): Templatize this benchmark and pass polling_type as a param |
| 133 |
grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr); |
| 134 |
gpr_timespec deadline = gpr_inf_past(GPR_CLOCK_MONOTONIC); |
| 135 |
for (auto _ : state) { |
| 136 |
grpc_completion_queue_next(cq, deadline, nullptr); |
| 137 |
} |
| 138 |
grpc_completion_queue_destroy(cq); |
| 139 |
} |
| 140 |
BENCHMARK(BM_EmptyCore); |
| 141 |
|
| 142 |
// Helper for tests to shutdown correctly and tersely |
| 143 |
static void shutdown_and_destroy(grpc_completion_queue* cc) { |
| 144 |
grpc_completion_queue_shutdown(cc); |
| 145 |
grpc_completion_queue_destroy(cc); |
| 146 |
} |
| 147 |
|
| 148 |
static gpr_mu shutdown_mu, mu; |
| 149 |
static gpr_cv shutdown_cv, cv; |
| 150 |
|
| 151 |
// Tag completion queue iterate times |
| 152 |
class TagCallback : public grpc_completion_queue_functor { |
| 153 |
public: |
| 154 |
explicit TagCallback(int* iter) : iter_(iter) { |
| 155 |
functor_run = &TagCallback::Run; |
| 156 |
inlineable = false; |
| 157 |
} |
| 158 |
~TagCallback() {} |
| 159 |
static void Run(grpc_completion_queue_functor* cb, int ok) { |
| 160 |
gpr_mu_lock(&mu); |
| 161 |
GPR_ASSERT(static_cast<bool>(ok)); |
| 162 |
*static_cast<TagCallback*>(cb)->iter_ += 1; |
| 163 |
gpr_cv_signal(&cv); |
| 164 |
gpr_mu_unlock(&mu); |
| 165 |
}; |
| 166 |
|
| 167 |
private: |
| 168 |
int* iter_; |
| 169 |
}; |
| 170 |
|
| 171 |
// Check if completion queue is shut down |
| 172 |
class ShutdownCallback : public grpc_completion_queue_functor { |
| 173 |
public: |
| 174 |
explicit ShutdownCallback(bool* done) : done_(done) { |
| 175 |
functor_run = &ShutdownCallback::Run; |
| 176 |
inlineable = false; |
| 177 |
} |
| 178 |
~ShutdownCallback() {} |
| 179 |
static void Run(grpc_completion_queue_functor* cb, int ok) { |
| 180 |
gpr_mu_lock(&shutdown_mu); |
| 181 |
*static_cast<ShutdownCallback*>(cb)->done_ = static_cast<bool>(ok); |
| 182 |
gpr_cv_signal(&shutdown_cv); |
| 183 |
gpr_mu_unlock(&shutdown_mu); |
| 184 |
} |
| 185 |
|
| 186 |
private: |
| 187 |
bool* done_; |
| 188 |
}; |
| 189 |
|
| 190 |
static void BM_Callback_CQ_Pass1Core(benchmark::State& state) { |
| 191 |
int iteration = 0, current_iterations = 0; |
| 192 |
TagCallback tag_cb(&iteration); |
| 193 |
gpr_mu_init(&mu); |
| 194 |
gpr_cv_init(&cv); |
| 195 |
gpr_mu_init(&shutdown_mu); |
| 196 |
gpr_cv_init(&shutdown_cv); |
| 197 |
bool got_shutdown = false; |
| 198 |
ShutdownCallback shutdown_cb(&got_shutdown); |
| 199 |
// This test with stack-allocated completions only works for non-polling or |
| 200 |
// EM-polling callback core CQs because otherwise the callback could execute |
| 201 |
// on another thread after the stack objects here go out of scope. An |
| 202 |
// alternative would be to synchronize between the benchmark loop and the |
| 203 |
// callback, but then it would be measuring the overhead of synchronization |
| 204 |
// rather than the overhead of the completion queue. |
| 205 |
// For generality, test here with non-polling. |
| 206 |
grpc_completion_queue_attributes attr; |
| 207 |
attr.version = 2; |
| 208 |
attr.cq_completion_type = GRPC_CQ_CALLBACK; |
| 209 |
attr.cq_polling_type = GRPC_CQ_NON_POLLING; |
| 210 |
attr.cq_shutdown_cb = &shutdown_cb; |
| 211 |
grpc_completion_queue* cc = grpc_completion_queue_create( |
| 212 |
grpc_completion_queue_factory_lookup(&attr), &attr, nullptr); |
| 213 |
for (auto _ : state) { |
| 214 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 215 |
grpc_core::ExecCtx exec_ctx; |
| 216 |
grpc_cq_completion completion; |
| 217 |
GPR_ASSERT(grpc_cq_begin_op(cc, &tag_cb)); |
| 218 |
grpc_cq_end_op(cc, &tag_cb, absl::OkStatus(), DoneWithCompletionOnStack, |
| 219 |
nullptr, &completion); |
| 220 |
} |
| 221 |
shutdown_and_destroy(cc); |
| 222 |
|
| 223 |
gpr_mu_lock(&mu); |
| 224 |
current_iterations = static_cast<int>(state.iterations()); |
| 225 |
while (current_iterations != iteration) { |
| 226 |
// Wait for all the callbacks to complete. |
| 227 |
gpr_cv_wait(&cv, &mu, gpr_inf_future(GPR_CLOCK_REALTIME)); |
| 228 |
} |
| 229 |
gpr_mu_unlock(&mu); |
| 230 |
|
| 231 |
gpr_mu_lock(&shutdown_mu); |
| 232 |
while (!got_shutdown) { |
| 233 |
// Wait for the shutdown callback to complete. |
| 234 |
gpr_cv_wait(&shutdown_cv, &shutdown_mu, gpr_inf_future(GPR_CLOCK_REALTIME)); |
| 235 |
} |
| 236 |
gpr_mu_unlock(&shutdown_mu); |
| 237 |
|
| 238 |
GPR_ASSERT(got_shutdown); |
| 239 |
GPR_ASSERT(iteration == static_cast<int>(state.iterations())); |
| 240 |
gpr_cv_destroy(&cv); |
| 241 |
gpr_mu_destroy(&mu); |
| 242 |
gpr_cv_destroy(&shutdown_cv); |
| 243 |
gpr_mu_destroy(&shutdown_mu); |
| 244 |
} |
| 245 |
static void BM_Callback_CQ_Pass1CoreHeapCompletion(benchmark::State& state) { |
| 246 |
int iteration = 0, current_iterations = 0; |
| 247 |
TagCallback tag_cb(&iteration); |
| 248 |
gpr_mu_init(&mu); |
| 249 |
gpr_cv_init(&cv); |
| 250 |
gpr_mu_init(&shutdown_mu); |
| 251 |
gpr_cv_init(&shutdown_cv); |
| 252 |
bool got_shutdown = false; |
| 253 |
ShutdownCallback shutdown_cb(&got_shutdown); |
| 254 |
grpc_completion_queue* cc = |
| 255 |
grpc_completion_queue_create_for_callback(&shutdown_cb, nullptr); |
| 256 |
for (auto _ : state) { |
| 257 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 258 |
grpc_core::ExecCtx exec_ctx; |
| 259 |
grpc_cq_completion* completion = new grpc_cq_completion; |
| 260 |
GPR_ASSERT(grpc_cq_begin_op(cc, &tag_cb)); |
| 261 |
grpc_cq_end_op(cc, &tag_cb, absl::OkStatus(), DoneWithCompletionOnHeap, |
| 262 |
nullptr, completion); |
| 263 |
} |
| 264 |
shutdown_and_destroy(cc); |
| 265 |
|
| 266 |
gpr_mu_lock(&mu); |
| 267 |
current_iterations = static_cast<int>(state.iterations()); |
| 268 |
while (current_iterations != iteration) { |
| 269 |
// Wait for all the callbacks to complete. |
| 270 |
gpr_cv_wait(&cv, &mu, gpr_inf_future(GPR_CLOCK_REALTIME)); |
| 271 |
} |
| 272 |
gpr_mu_unlock(&mu); |
| 273 |
|
| 274 |
gpr_mu_lock(&shutdown_mu); |
| 275 |
while (!got_shutdown) { |
| 276 |
// Wait for the shutdown callback to complete. |
| 277 |
gpr_cv_wait(&shutdown_cv, &shutdown_mu, gpr_inf_future(GPR_CLOCK_REALTIME)); |
| 278 |
} |
| 279 |
gpr_mu_unlock(&shutdown_mu); |
| 280 |
|
| 281 |
GPR_ASSERT(got_shutdown); |
| 282 |
GPR_ASSERT(iteration == static_cast<int>(state.iterations())); |
| 283 |
gpr_cv_destroy(&cv); |
| 284 |
gpr_mu_destroy(&mu); |
| 285 |
gpr_cv_destroy(&shutdown_cv); |
| 286 |
gpr_mu_destroy(&shutdown_mu); |
| 287 |
} |
| 288 |
BENCHMARK(BM_Callback_CQ_Pass1Core); |
| 289 |
BENCHMARK(BM_Callback_CQ_Pass1CoreHeapCompletion); |
| 290 |
|
| 291 |
} // namespace testing |
| 292 |
} // namespace grpc |
| 293 |
|
| 294 |
// Some distros have RunSpecifiedBenchmarks under the benchmark namespace, |
| 295 |
// and others do not. This allows us to support both modes. |
| 296 |
namespace benchmark { |
| 297 |
void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); } |
| 298 |
} // namespace benchmark |
| 299 |
|
| 300 |
int main(int argc, char** argv) { |
| 301 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 302 |
LibraryInitializer libInit; |
| 303 |
::benchmark::Initialize(&argc, argv); |
| 304 |
grpc::testing::InitTest(&argc, &argv, false); |
| 305 |
benchmark::RunTheBenchmarksNamespaced(); |
| 306 |
return 0; |
| 307 |
} |