| 1 |
// Copyright 2022 The gRPC Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
#include <grpc/support/port_platform.h> |
| 15 |
|
| 16 |
#include <cmath> |
| 17 |
#include <deque> |
| 18 |
#include <sstream> |
| 19 |
|
| 20 |
// ensure assert() is enabled |
| 21 |
#undef NDEBUG |
| 22 |
#include <cassert> |
| 23 |
|
| 24 |
#include <benchmark/benchmark.h> |
| 25 |
|
| 26 |
#include <grpc/support/log.h> |
| 27 |
|
| 28 |
#include "src/core/lib/event_engine/common_closures.h" |
| 29 |
#include "src/core/lib/event_engine/work_queue.h" |
| 30 |
#include "test/core/util/test_config.h" |
| 31 |
|
| 32 |
namespace { |
| 33 |
|
| 34 |
using ::grpc_event_engine::experimental::AnyInvocableClosure; |
| 35 |
using ::grpc_event_engine::experimental::EventEngine; |
| 36 |
using ::grpc_event_engine::experimental::WorkQueue; |
| 37 |
|
| 38 |
grpc_core::Mutex globalMu; |
| 39 |
std::vector<WorkQueue*>* globalWorkQueueList; |
| 40 |
std::vector<std::deque<EventEngine::Closure*>*>* globalDequeList; |
| 41 |
std::vector<grpc_core::Mutex>* globalDequeMutexList; |
| 42 |
|
| 43 |
void GlobalSetup(const benchmark::State& state) { |
| 44 |
// called for every test, resets all state |
| 45 |
globalWorkQueueList = new std::vector<WorkQueue*>(); |
| 46 |
globalWorkQueueList->reserve(state.threads()); |
| 47 |
globalDequeList = new std::vector<std::deque<EventEngine::Closure*>*>(); |
| 48 |
globalDequeList->reserve(state.threads()); |
| 49 |
globalDequeMutexList = new std::vector<grpc_core::Mutex>( |
| 50 |
std::vector<grpc_core::Mutex>(state.threads())); |
| 51 |
} |
| 52 |
|
| 53 |
void GlobalTeardown(const benchmark::State& /* state */) { |
| 54 |
// called for every test, resets all state |
| 55 |
delete globalWorkQueueList; |
| 56 |
delete globalDequeList; |
| 57 |
delete globalDequeMutexList; |
| 58 |
} |
| 59 |
|
| 60 |
void BM_WorkQueueIntptrPopFront(benchmark::State& state) { |
| 61 |
WorkQueue queue; |
| 62 |
grpc_event_engine::experimental::AnyInvocableClosure closure([] {}); |
| 63 |
int element_count = state.range(0); |
| 64 |
for (auto _ : state) { |
| 65 |
int cnt = 0; |
| 66 |
for (int i = 0; i < element_count; i++) queue.Add(&closure); |
| 67 |
absl::optional<EventEngine::Closure*> popped; |
| 68 |
cnt = 0; |
| 69 |
do { |
| 70 |
popped = queue.PopFront(); |
| 71 |
if (popped.has_value()) ++cnt; |
| 72 |
} while (cnt < element_count); |
| 73 |
} |
| 74 |
state.counters["Added"] = element_count * state.iterations(); |
| 75 |
state.counters["Popped"] = state.counters["Added"]; |
| 76 |
state.counters["Steal Rate"] = |
| 77 |
benchmark::Counter(state.counters["Popped"], benchmark::Counter::kIsRate); |
| 78 |
} |
| 79 |
BENCHMARK(BM_WorkQueueIntptrPopFront) |
| 80 |
->Setup(GlobalSetup) |
| 81 |
->Teardown(GlobalTeardown) |
| 82 |
->Range(1, 512) |
| 83 |
->UseRealTime() |
| 84 |
->MeasureProcessCPUTime(); |
| 85 |
|
| 86 |
void BM_MultithreadedWorkQueuePopBack(benchmark::State& state) { |
| 87 |
if (state.thread_index() == 0) (*globalWorkQueueList)[0] = new WorkQueue(); |
| 88 |
AnyInvocableClosure closure([] {}); |
| 89 |
int element_count = state.range(0); |
| 90 |
for (auto _ : state) { |
| 91 |
int cnt = 0; |
| 92 |
auto* queue = (*globalWorkQueueList)[0]; |
| 93 |
for (int i = 0; i < element_count; i++) queue->Add(&closure); |
| 94 |
absl::optional<EventEngine::Closure*> popped; |
| 95 |
cnt = 0; |
| 96 |
do { |
| 97 |
popped = queue->PopBack(); |
| 98 |
if (popped.has_value()) ++cnt; |
| 99 |
} while (cnt < element_count); |
| 100 |
} |
| 101 |
state.counters["Added"] = element_count * state.iterations(); |
| 102 |
state.counters["Popped"] = state.counters["Added"]; |
| 103 |
state.counters["Steal Rate"] = |
| 104 |
benchmark::Counter(state.counters["Popped"], benchmark::Counter::kIsRate); |
| 105 |
if (state.thread_index() == 0) { |
| 106 |
delete (*globalWorkQueueList)[0]; |
| 107 |
} |
| 108 |
} |
| 109 |
BENCHMARK(BM_MultithreadedWorkQueuePopBack) |
| 110 |
->Setup(GlobalSetup) |
| 111 |
->Teardown(GlobalTeardown) |
| 112 |
->Range(1, 512) |
| 113 |
->UseRealTime() |
| 114 |
->MeasureProcessCPUTime() |
| 115 |
->Threads(1) |
| 116 |
->Threads(4) |
| 117 |
->ThreadPerCpu(); |
| 118 |
|
| 119 |
void BM_WorkQueueClosureExecution(benchmark::State& state) { |
| 120 |
WorkQueue queue; |
| 121 |
int element_count = state.range(0); |
| 122 |
int run_count = 0; |
| 123 |
grpc_event_engine::experimental::AnyInvocableClosure closure( |
| 124 |
[&run_count] { ++run_count; }); |
| 125 |
for (auto _ : state) { |
| 126 |
for (int i = 0; i < element_count; i++) queue.Add(&closure); |
| 127 |
do { |
| 128 |
queue.PopFront()->Run(); |
| 129 |
} while (run_count < element_count); |
| 130 |
run_count = 0; |
| 131 |
} |
| 132 |
state.counters["Added"] = element_count * state.iterations(); |
| 133 |
state.counters["Popped"] = state.counters["Added"]; |
| 134 |
state.counters["Steal Rate"] = |
| 135 |
benchmark::Counter(state.counters["Popped"], benchmark::Counter::kIsRate); |
| 136 |
} |
| 137 |
BENCHMARK(BM_WorkQueueClosureExecution) |
| 138 |
->Range(8, 128) |
| 139 |
->UseRealTime() |
| 140 |
->MeasureProcessCPUTime(); |
| 141 |
|
| 142 |
void BM_WorkQueueAnyInvocableExecution(benchmark::State& state) { |
| 143 |
WorkQueue queue; |
| 144 |
int element_count = state.range(0); |
| 145 |
int run_count = 0; |
| 146 |
for (auto _ : state) { |
| 147 |
for (int i = 0; i < element_count; i++) { |
| 148 |
queue.Add([&run_count] { ++run_count; }); |
| 149 |
} |
| 150 |
do { |
| 151 |
queue.PopFront()->Run(); |
| 152 |
} while (run_count < element_count); |
| 153 |
run_count = 0; |
| 154 |
} |
| 155 |
state.counters["Added"] = element_count * state.iterations(); |
| 156 |
state.counters["Popped"] = state.counters["Added"]; |
| 157 |
state.counters["Steal Rate"] = |
| 158 |
benchmark::Counter(state.counters["Popped"], benchmark::Counter::kIsRate); |
| 159 |
} |
| 160 |
BENCHMARK(BM_WorkQueueAnyInvocableExecution) |
| 161 |
->Range(8, 128) |
| 162 |
->UseRealTime() |
| 163 |
->MeasureProcessCPUTime(); |
| 164 |
|
| 165 |
void BM_StdDequeLIFO(benchmark::State& state) { |
| 166 |
if (state.thread_index() == 0) { |
| 167 |
(*globalDequeList)[0] = new std::deque<EventEngine::Closure*>(); |
| 168 |
} |
| 169 |
auto& mu = (*globalDequeMutexList)[0]; |
| 170 |
int element_count = state.range(0); |
| 171 |
AnyInvocableClosure closure([] {}); |
| 172 |
for (auto _ : state) { |
| 173 |
auto* queue = (*globalDequeList)[0]; |
| 174 |
for (int i = 0; i < element_count; i++) { |
| 175 |
grpc_core::MutexLock lock(&mu); |
| 176 |
queue->emplace_back(&closure); |
| 177 |
} |
| 178 |
for (int i = 0; i < element_count; i++) { |
| 179 |
grpc_core::MutexLock lock(&mu); |
| 180 |
EventEngine::Closure* popped = queue->back(); |
| 181 |
queue->pop_back(); |
| 182 |
assert(popped != nullptr); |
| 183 |
} |
| 184 |
} |
| 185 |
state.counters["Added"] = element_count * state.iterations(); |
| 186 |
state.counters["Popped"] = state.counters["Added"]; |
| 187 |
state.counters["Steal Rate"] = |
| 188 |
benchmark::Counter(state.counters["Popped"], benchmark::Counter::kIsRate); |
| 189 |
if (state.thread_index() == 0) { |
| 190 |
delete (*globalDequeList)[0]; |
| 191 |
} |
| 192 |
} |
| 193 |
BENCHMARK(BM_StdDequeLIFO) |
| 194 |
->Setup(GlobalSetup) |
| 195 |
->Teardown(GlobalTeardown) |
| 196 |
->Range(1, 512) |
| 197 |
->UseRealTime() |
| 198 |
->MeasureProcessCPUTime() |
| 199 |
->Threads(1) |
| 200 |
->Threads(4) |
| 201 |
->ThreadPerCpu(); |
| 202 |
|
| 203 |
void PerThreadArguments(benchmark::internal::Benchmark* b) { |
| 204 |
b->Setup(GlobalSetup) |
| 205 |
->Teardown(GlobalTeardown) |
| 206 |
->ArgsProduct({/*pop_attempts=*/{10, 50, 250}, |
| 207 |
/*pct_fill=*/{2, 10, 50}}) |
| 208 |
->UseRealTime() |
| 209 |
->MeasureProcessCPUTime() |
| 210 |
->Threads(10) |
| 211 |
->ThreadPerCpu(); |
| 212 |
} |
| 213 |
|
| 214 |
void BM_WorkQueuePerThread(benchmark::State& state) { |
| 215 |
WorkQueue local_queue; |
| 216 |
{ |
| 217 |
grpc_core::MutexLock lock(&globalMu); |
| 218 |
(*globalWorkQueueList)[state.thread_index()] = &local_queue; |
| 219 |
} |
| 220 |
AnyInvocableClosure closure([] {}); |
| 221 |
int element_count = state.range(0); |
| 222 |
float pct_fill = state.range(1) / 100.0; |
| 223 |
for (auto _ : state) { |
| 224 |
// sparsely populate a queue |
| 225 |
for (int i = 0; i < std::ceil(element_count * pct_fill); i++) { |
| 226 |
local_queue.Add(&closure); |
| 227 |
} |
| 228 |
// attempt to pop from all thread queues `element_count` times |
| 229 |
int pop_attempts = 0; |
| 230 |
auto iq = globalWorkQueueList->begin(); |
| 231 |
while (pop_attempts++ < element_count) { |
| 232 |
// may not get a value if the queue being looked at from another thread |
| 233 |
(*iq)->PopBack(); |
| 234 |
if (iq == globalWorkQueueList->end()) { |
| 235 |
iq = globalWorkQueueList->begin(); |
| 236 |
} else { |
| 237 |
iq++; |
| 238 |
}; |
| 239 |
} |
| 240 |
} |
| 241 |
state.counters["Added"] = |
| 242 |
std::ceil(element_count * pct_fill) * state.iterations(); |
| 243 |
state.counters["Steal Attempts"] = element_count * state.iterations(); |
| 244 |
state.counters["Steal Rate"] = benchmark::Counter( |
| 245 |
state.counters["Steal Attempts"], benchmark::Counter::kIsRate); |
| 246 |
if (state.thread_index() == 0) { |
| 247 |
for (auto* queue : *globalWorkQueueList) { |
| 248 |
assert(queue->Empty()); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
BENCHMARK(BM_WorkQueuePerThread)->Apply(PerThreadArguments); |
| 253 |
|
| 254 |
void BM_StdDequePerThread(benchmark::State& state) { |
| 255 |
std::deque<EventEngine::Closure*> local_queue; |
| 256 |
(*globalDequeList)[state.thread_index()] = &local_queue; |
| 257 |
int element_count = state.range(0); |
| 258 |
float pct_fill = state.range(1) / 100.0; |
| 259 |
AnyInvocableClosure closure([] {}); |
| 260 |
auto& local_mu = (*globalDequeMutexList)[state.thread_index()]; |
| 261 |
for (auto _ : state) { |
| 262 |
// sparsely populate a queue |
| 263 |
for (int i = 0; i < std::ceil(element_count * pct_fill); i++) { |
| 264 |
grpc_core::MutexLock lock(&local_mu); |
| 265 |
local_queue.emplace_back(&closure); |
| 266 |
} |
| 267 |
int pop_attempts = 0; |
| 268 |
auto iq = globalDequeList->begin(); |
| 269 |
auto mu = globalDequeMutexList->begin(); |
| 270 |
while (pop_attempts++ < element_count) { |
| 271 |
{ |
| 272 |
grpc_core::MutexLock lock(&*mu); |
| 273 |
if (!(*iq)->empty()) { |
| 274 |
assert((*iq)->back() != nullptr); |
| 275 |
(*iq)->pop_back(); |
| 276 |
} |
| 277 |
} |
| 278 |
if (iq == globalDequeList->end()) { |
| 279 |
iq = globalDequeList->begin(); |
| 280 |
mu = globalDequeMutexList->begin(); |
| 281 |
} else { |
| 282 |
++iq; |
| 283 |
++mu; |
| 284 |
}; |
| 285 |
} |
| 286 |
} |
| 287 |
state.counters["Added"] = |
| 288 |
std::ceil(element_count * pct_fill) * state.iterations(); |
| 289 |
state.counters["Steal Attempts"] = element_count * state.iterations(); |
| 290 |
state.counters["Steal Rate"] = benchmark::Counter( |
| 291 |
state.counters["Steal Attempts"], benchmark::Counter::kIsRate); |
| 292 |
if (state.thread_index() == 0) { |
| 293 |
for (auto* queue : *globalDequeList) { |
| 294 |
assert(queue->empty()); |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
BENCHMARK(BM_StdDequePerThread)->Apply(PerThreadArguments); |
| 299 |
|
| 300 |
} // namespace |
| 301 |
|
| 302 |
// Some distros have RunSpecifiedBenchmarks under the benchmark namespace, |
| 303 |
// and others do not. This allows us to support both modes. |
| 304 |
namespace benchmark { |
| 305 |
void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); } |
| 306 |
} // namespace benchmark |
| 307 |
|
| 308 |
int main(int argc, char** argv) { |
| 309 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 310 |
::benchmark::Initialize(&argc, argv); |
| 311 |
benchmark::RunTheBenchmarksNamespaced(); |
| 312 |
return 0; |
| 313 |
} |