| 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 |
/* Test various closure related operations */ |
| 20 |
|
| 21 |
#include <sstream> |
| 22 |
|
| 23 |
#include <benchmark/benchmark.h> |
| 24 |
|
| 25 |
#include <grpc/grpc.h> |
| 26 |
|
| 27 |
#include "src/core/lib/gpr/spinlock.h" |
| 28 |
#include "src/core/lib/iomgr/closure.h" |
| 29 |
#include "src/core/lib/iomgr/combiner.h" |
| 30 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 31 |
#include "test/core/util/test_config.h" |
| 32 |
#include "test/cpp/microbenchmarks/helpers.h" |
| 33 |
#include "test/cpp/util/test_config.h" |
| 34 |
|
| 35 |
static void BM_NoOpExecCtx(benchmark::State& state) { |
| 36 |
for (auto _ : state) { |
| 37 |
grpc_core::ExecCtx exec_ctx; |
| 38 |
} |
| 39 |
} |
| 40 |
BENCHMARK(BM_NoOpExecCtx); |
| 41 |
|
| 42 |
static void BM_WellFlushed(benchmark::State& state) { |
| 43 |
grpc_core::ExecCtx exec_ctx; |
| 44 |
for (auto _ : state) { |
| 45 |
grpc_core::ExecCtx::Get()->Flush(); |
| 46 |
} |
| 47 |
} |
| 48 |
BENCHMARK(BM_WellFlushed); |
| 49 |
|
| 50 |
static void DoNothing(void* /*arg*/, grpc_error_handle /*error*/) {} |
| 51 |
|
| 52 |
static void BM_ClosureInitAgainstExecCtx(benchmark::State& state) { |
| 53 |
grpc_closure c; |
| 54 |
for (auto _ : state) { |
| 55 |
benchmark::DoNotOptimize( |
| 56 |
GRPC_CLOSURE_INIT(&c, DoNothing, nullptr, grpc_schedule_on_exec_ctx)); |
| 57 |
} |
| 58 |
} |
| 59 |
BENCHMARK(BM_ClosureInitAgainstExecCtx); |
| 60 |
|
| 61 |
static void BM_ClosureInitAgainstCombiner(benchmark::State& state) { |
| 62 |
grpc_core::Combiner* combiner = grpc_combiner_create(); |
| 63 |
grpc_closure c; |
| 64 |
grpc_core::ExecCtx exec_ctx; |
| 65 |
for (auto _ : state) { |
| 66 |
benchmark::DoNotOptimize( |
| 67 |
GRPC_CLOSURE_INIT(&c, DoNothing, nullptr, nullptr)); |
| 68 |
} |
| 69 |
GRPC_COMBINER_UNREF(combiner, "finished"); |
| 70 |
} |
| 71 |
BENCHMARK(BM_ClosureInitAgainstCombiner); |
| 72 |
|
| 73 |
static void BM_ClosureRun(benchmark::State& state) { |
| 74 |
grpc_closure c; |
| 75 |
GRPC_CLOSURE_INIT(&c, DoNothing, nullptr, grpc_schedule_on_exec_ctx); |
| 76 |
grpc_core::ExecCtx exec_ctx; |
| 77 |
for (auto _ : state) { |
| 78 |
grpc_core::Closure::Run(DEBUG_LOCATION, &c, absl::OkStatus()); |
| 79 |
} |
| 80 |
} |
| 81 |
BENCHMARK(BM_ClosureRun); |
| 82 |
|
| 83 |
static void BM_ClosureCreateAndRun(benchmark::State& state) { |
| 84 |
grpc_core::ExecCtx exec_ctx; |
| 85 |
for (auto _ : state) { |
| 86 |
grpc_core::Closure::Run( |
| 87 |
DEBUG_LOCATION, |
| 88 |
GRPC_CLOSURE_CREATE(DoNothing, nullptr, grpc_schedule_on_exec_ctx), |
| 89 |
absl::OkStatus()); |
| 90 |
} |
| 91 |
} |
| 92 |
BENCHMARK(BM_ClosureCreateAndRun); |
| 93 |
|
| 94 |
static void BM_ClosureInitAndRun(benchmark::State& state) { |
| 95 |
grpc_core::ExecCtx exec_ctx; |
| 96 |
grpc_closure c; |
| 97 |
for (auto _ : state) { |
| 98 |
grpc_core::Closure::Run( |
| 99 |
DEBUG_LOCATION, |
| 100 |
GRPC_CLOSURE_INIT(&c, DoNothing, nullptr, grpc_schedule_on_exec_ctx), |
| 101 |
absl::OkStatus()); |
| 102 |
} |
| 103 |
} |
| 104 |
BENCHMARK(BM_ClosureInitAndRun); |
| 105 |
|
| 106 |
static void BM_ClosureSchedOnExecCtx(benchmark::State& state) { |
| 107 |
grpc_closure c; |
| 108 |
GRPC_CLOSURE_INIT(&c, DoNothing, nullptr, grpc_schedule_on_exec_ctx); |
| 109 |
grpc_core::ExecCtx exec_ctx; |
| 110 |
for (auto _ : state) { |
| 111 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, &c, absl::OkStatus()); |
| 112 |
grpc_core::ExecCtx::Get()->Flush(); |
| 113 |
} |
| 114 |
} |
| 115 |
BENCHMARK(BM_ClosureSchedOnExecCtx); |
| 116 |
|
| 117 |
static void BM_ClosureSched2OnExecCtx(benchmark::State& state) { |
| 118 |
grpc_closure c1; |
| 119 |
grpc_closure c2; |
| 120 |
GRPC_CLOSURE_INIT(&c1, DoNothing, nullptr, grpc_schedule_on_exec_ctx); |
| 121 |
GRPC_CLOSURE_INIT(&c2, DoNothing, nullptr, grpc_schedule_on_exec_ctx); |
| 122 |
grpc_core::ExecCtx exec_ctx; |
| 123 |
for (auto _ : state) { |
| 124 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, &c1, absl::OkStatus()); |
| 125 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, &c2, absl::OkStatus()); |
| 126 |
grpc_core::ExecCtx::Get()->Flush(); |
| 127 |
} |
| 128 |
} |
| 129 |
BENCHMARK(BM_ClosureSched2OnExecCtx); |
| 130 |
|
| 131 |
static void BM_ClosureSched3OnExecCtx(benchmark::State& state) { |
| 132 |
grpc_closure c1; |
| 133 |
grpc_closure c2; |
| 134 |
grpc_closure c3; |
| 135 |
GRPC_CLOSURE_INIT(&c1, DoNothing, nullptr, grpc_schedule_on_exec_ctx); |
| 136 |
GRPC_CLOSURE_INIT(&c2, DoNothing, nullptr, grpc_schedule_on_exec_ctx); |
| 137 |
GRPC_CLOSURE_INIT(&c3, DoNothing, nullptr, grpc_schedule_on_exec_ctx); |
| 138 |
grpc_core::ExecCtx exec_ctx; |
| 139 |
for (auto _ : state) { |
| 140 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, &c1, absl::OkStatus()); |
| 141 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, &c2, absl::OkStatus()); |
| 142 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, &c3, absl::OkStatus()); |
| 143 |
grpc_core::ExecCtx::Get()->Flush(); |
| 144 |
} |
| 145 |
} |
| 146 |
BENCHMARK(BM_ClosureSched3OnExecCtx); |
| 147 |
|
| 148 |
static void BM_AcquireMutex(benchmark::State& state) { |
| 149 |
// for comparison with the combiner stuff below |
| 150 |
gpr_mu mu; |
| 151 |
gpr_mu_init(&mu); |
| 152 |
grpc_core::ExecCtx exec_ctx; |
| 153 |
for (auto _ : state) { |
| 154 |
gpr_mu_lock(&mu); |
| 155 |
DoNothing(nullptr, absl::OkStatus()); |
| 156 |
gpr_mu_unlock(&mu); |
| 157 |
} |
| 158 |
gpr_mu_destroy(&mu); |
| 159 |
} |
| 160 |
BENCHMARK(BM_AcquireMutex); |
| 161 |
|
| 162 |
static void BM_TryAcquireMutex(benchmark::State& state) { |
| 163 |
// for comparison with the combiner stuff below |
| 164 |
gpr_mu mu; |
| 165 |
gpr_mu_init(&mu); |
| 166 |
grpc_core::ExecCtx exec_ctx; |
| 167 |
for (auto _ : state) { |
| 168 |
if (gpr_mu_trylock(&mu)) { |
| 169 |
DoNothing(nullptr, absl::OkStatus()); |
| 170 |
gpr_mu_unlock(&mu); |
| 171 |
} else { |
| 172 |
abort(); |
| 173 |
} |
| 174 |
} |
| 175 |
gpr_mu_destroy(&mu); |
| 176 |
} |
| 177 |
BENCHMARK(BM_TryAcquireMutex); |
| 178 |
|
| 179 |
static void BM_AcquireSpinlock(benchmark::State& state) { |
| 180 |
// for comparison with the combiner stuff below |
| 181 |
gpr_spinlock mu = GPR_SPINLOCK_INITIALIZER; |
| 182 |
grpc_core::ExecCtx exec_ctx; |
| 183 |
for (auto _ : state) { |
| 184 |
gpr_spinlock_lock(&mu); |
| 185 |
DoNothing(nullptr, absl::OkStatus()); |
| 186 |
gpr_spinlock_unlock(&mu); |
| 187 |
} |
| 188 |
} |
| 189 |
BENCHMARK(BM_AcquireSpinlock); |
| 190 |
|
| 191 |
static void BM_TryAcquireSpinlock(benchmark::State& state) { |
| 192 |
// for comparison with the combiner stuff below |
| 193 |
gpr_spinlock mu = GPR_SPINLOCK_INITIALIZER; |
| 194 |
grpc_core::ExecCtx exec_ctx; |
| 195 |
for (auto _ : state) { |
| 196 |
if (gpr_spinlock_trylock(&mu)) { |
| 197 |
DoNothing(nullptr, absl::OkStatus()); |
| 198 |
gpr_spinlock_unlock(&mu); |
| 199 |
} else { |
| 200 |
abort(); |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
BENCHMARK(BM_TryAcquireSpinlock); |
| 205 |
|
| 206 |
static void BM_ClosureSchedOnCombiner(benchmark::State& state) { |
| 207 |
grpc_core::Combiner* combiner = grpc_combiner_create(); |
| 208 |
grpc_closure c; |
| 209 |
GRPC_CLOSURE_INIT(&c, DoNothing, nullptr, nullptr); |
| 210 |
grpc_core::ExecCtx exec_ctx; |
| 211 |
for (auto _ : state) { |
| 212 |
combiner->Run(&c, absl::OkStatus()); |
| 213 |
grpc_core::ExecCtx::Get()->Flush(); |
| 214 |
} |
| 215 |
GRPC_COMBINER_UNREF(combiner, "finished"); |
| 216 |
} |
| 217 |
BENCHMARK(BM_ClosureSchedOnCombiner); |
| 218 |
|
| 219 |
static void BM_ClosureSched2OnCombiner(benchmark::State& state) { |
| 220 |
grpc_core::Combiner* combiner = grpc_combiner_create(); |
| 221 |
grpc_closure c1; |
| 222 |
grpc_closure c2; |
| 223 |
GRPC_CLOSURE_INIT(&c1, DoNothing, nullptr, nullptr); |
| 224 |
GRPC_CLOSURE_INIT(&c2, DoNothing, nullptr, nullptr); |
| 225 |
grpc_core::ExecCtx exec_ctx; |
| 226 |
for (auto _ : state) { |
| 227 |
combiner->Run(&c1, absl::OkStatus()); |
| 228 |
combiner->Run(&c2, absl::OkStatus()); |
| 229 |
grpc_core::ExecCtx::Get()->Flush(); |
| 230 |
} |
| 231 |
GRPC_COMBINER_UNREF(combiner, "finished"); |
| 232 |
} |
| 233 |
BENCHMARK(BM_ClosureSched2OnCombiner); |
| 234 |
|
| 235 |
static void BM_ClosureSched3OnCombiner(benchmark::State& state) { |
| 236 |
grpc_core::Combiner* combiner = grpc_combiner_create(); |
| 237 |
grpc_closure c1; |
| 238 |
grpc_closure c2; |
| 239 |
grpc_closure c3; |
| 240 |
GRPC_CLOSURE_INIT(&c1, DoNothing, nullptr, nullptr); |
| 241 |
GRPC_CLOSURE_INIT(&c2, DoNothing, nullptr, nullptr); |
| 242 |
GRPC_CLOSURE_INIT(&c3, DoNothing, nullptr, nullptr); |
| 243 |
grpc_core::ExecCtx exec_ctx; |
| 244 |
for (auto _ : state) { |
| 245 |
combiner->Run(&c1, absl::OkStatus()); |
| 246 |
combiner->Run(&c2, absl::OkStatus()); |
| 247 |
combiner->Run(&c3, absl::OkStatus()); |
| 248 |
grpc_core::ExecCtx::Get()->Flush(); |
| 249 |
} |
| 250 |
GRPC_COMBINER_UNREF(combiner, "finished"); |
| 251 |
} |
| 252 |
BENCHMARK(BM_ClosureSched3OnCombiner); |
| 253 |
|
| 254 |
static void BM_ClosureSched2OnTwoCombiners(benchmark::State& state) { |
| 255 |
grpc_core::Combiner* combiner1 = grpc_combiner_create(); |
| 256 |
grpc_core::Combiner* combiner2 = grpc_combiner_create(); |
| 257 |
grpc_closure c1; |
| 258 |
grpc_closure c2; |
| 259 |
GRPC_CLOSURE_INIT(&c1, DoNothing, nullptr, nullptr); |
| 260 |
GRPC_CLOSURE_INIT(&c2, DoNothing, nullptr, nullptr); |
| 261 |
grpc_core::ExecCtx exec_ctx; |
| 262 |
for (auto _ : state) { |
| 263 |
combiner1->Run(&c1, absl::OkStatus()); |
| 264 |
combiner2->Run(&c2, absl::OkStatus()); |
| 265 |
grpc_core::ExecCtx::Get()->Flush(); |
| 266 |
} |
| 267 |
GRPC_COMBINER_UNREF(combiner1, "finished"); |
| 268 |
GRPC_COMBINER_UNREF(combiner2, "finished"); |
| 269 |
} |
| 270 |
BENCHMARK(BM_ClosureSched2OnTwoCombiners); |
| 271 |
|
| 272 |
static void BM_ClosureSched4OnTwoCombiners(benchmark::State& state) { |
| 273 |
grpc_core::Combiner* combiner1 = grpc_combiner_create(); |
| 274 |
grpc_core::Combiner* combiner2 = grpc_combiner_create(); |
| 275 |
grpc_closure c1; |
| 276 |
grpc_closure c2; |
| 277 |
grpc_closure c3; |
| 278 |
grpc_closure c4; |
| 279 |
GRPC_CLOSURE_INIT(&c1, DoNothing, nullptr, nullptr); |
| 280 |
GRPC_CLOSURE_INIT(&c2, DoNothing, nullptr, nullptr); |
| 281 |
GRPC_CLOSURE_INIT(&c3, DoNothing, nullptr, nullptr); |
| 282 |
GRPC_CLOSURE_INIT(&c4, DoNothing, nullptr, nullptr); |
| 283 |
grpc_core::ExecCtx exec_ctx; |
| 284 |
for (auto _ : state) { |
| 285 |
combiner1->Run(&c1, absl::OkStatus()); |
| 286 |
combiner2->Run(&c2, absl::OkStatus()); |
| 287 |
combiner1->Run(&c3, absl::OkStatus()); |
| 288 |
combiner2->Run(&c4, absl::OkStatus()); |
| 289 |
grpc_core::ExecCtx::Get()->Flush(); |
| 290 |
} |
| 291 |
GRPC_COMBINER_UNREF(combiner1, "finished"); |
| 292 |
GRPC_COMBINER_UNREF(combiner2, "finished"); |
| 293 |
} |
| 294 |
BENCHMARK(BM_ClosureSched4OnTwoCombiners); |
| 295 |
|
| 296 |
// Helper that continuously reschedules the same closure against something until |
| 297 |
// the benchmark is complete |
| 298 |
class Rescheduler { |
| 299 |
public: |
| 300 |
explicit Rescheduler(benchmark::State& state) : state_(state) { |
| 301 |
GRPC_CLOSURE_INIT(&closure_, Step, this, nullptr); |
| 302 |
} |
| 303 |
|
| 304 |
void ScheduleFirst() { |
| 305 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, &closure_, absl::OkStatus()); |
| 306 |
} |
| 307 |
|
| 308 |
void ScheduleFirstAgainstDifferentScheduler() { |
| 309 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, |
| 310 |
GRPC_CLOSURE_CREATE(Step, this, nullptr), |
| 311 |
absl::OkStatus()); |
| 312 |
} |
| 313 |
|
| 314 |
private: |
| 315 |
benchmark::State& state_; |
| 316 |
grpc_closure closure_; |
| 317 |
|
| 318 |
static void Step(void* arg, grpc_error_handle /*error*/) { |
| 319 |
Rescheduler* self = static_cast<Rescheduler*>(arg); |
| 320 |
if (self->state_.KeepRunning()) { |
| 321 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, &self->closure_, |
| 322 |
absl::OkStatus()); |
| 323 |
} |
| 324 |
} |
| 325 |
}; |
| 326 |
|
| 327 |
static void BM_ClosureReschedOnExecCtx(benchmark::State& state) { |
| 328 |
grpc_core::ExecCtx exec_ctx; |
| 329 |
Rescheduler r(state); |
| 330 |
r.ScheduleFirst(); |
| 331 |
grpc_core::ExecCtx::Get()->Flush(); |
| 332 |
} |
| 333 |
BENCHMARK(BM_ClosureReschedOnExecCtx); |
| 334 |
|
| 335 |
// Some distros have RunSpecifiedBenchmarks under the benchmark namespace, |
| 336 |
// and others do not. This allows us to support both modes. |
| 337 |
namespace benchmark { |
| 338 |
void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); } |
| 339 |
} // namespace benchmark |
| 340 |
|
| 341 |
int main(int argc, char** argv) { |
| 342 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 343 |
LibraryInitializer libInit; |
| 344 |
::benchmark::Initialize(&argc, argv); |
| 345 |
grpc::testing::InitTest(&argc, &argv, false); |
| 346 |
benchmark::RunTheBenchmarksNamespaced(); |
| 347 |
return 0; |
| 348 |
} |