Debian Codegraph @

test/cpp/qps/driver.cc

raw
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 #include "test/cpp/qps/driver.h"
20
21 #include <cinttypes>
22 #include <deque>
23 #include <list>
24 #include <thread>
25 #include <unordered_map>
26 #include <vector>
27
28 #include "google/protobuf/timestamp.pb.h"
29
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/log.h>
32 #include <grpc/support/string_util.h>
33 #include <grpcpp/channel.h>
34 #include <grpcpp/client_context.h>
35 #include <grpcpp/create_channel.h>
36
37 #include "src/core/lib/gprpp/env.h"
38 #include "src/core/lib/gprpp/host_port.h"
39 #include "src/proto/grpc/testing/worker_service.grpc.pb.h"
40 #include "test/core/util/port.h"
41 #include "test/core/util/test_config.h"
42 #include "test/cpp/qps/client.h"
43 #include "test/cpp/qps/histogram.h"
44 #include "test/cpp/qps/qps_worker.h"
45 #include "test/cpp/qps/stats.h"
46 #include "test/cpp/util/test_credentials_provider.h"
47
48 using std::deque;
49 using std::list;
50 using std::unique_ptr;
51 using std::vector;
52
53 namespace grpc {
54 namespace testing {
55 static std::string get_host(const std::string& worker) {
56 absl::string_view host;
57 absl::string_view port;
58 grpc_core::SplitHostPort(worker.c_str(), &host, &port);
59 return std::string(host.data(), host.size());
60 }
61
62 static deque<string> get_workers(const string& env_name) {
63 deque<string> out;
64 auto env = grpc_core::GetEnv(env_name.c_str()).value_or("");
65 const char* p = env.c_str();
66 if (!env.empty()) {
67 for (;;) {
68 const char* comma = strchr(p, ',');
69 if (comma) {
70 out.emplace_back(p, comma);
71 p = comma + 1;
72 } else {
73 out.emplace_back(p);
74 break;
75 }
76 }
77 }
78 if (out.empty()) {
79 gpr_log(GPR_ERROR,
80 "Environment variable \"%s\" does not contain a list of QPS "
81 "workers to use. Set it to a comma-separated list of "
82 "hostname:port pairs, starting with hosts that should act as "
83 "servers. E.g. export "
84 "%s=\"serverhost1:1234,clienthost1:1234,clienthost2:1234\"",
85 env_name.c_str(), env_name.c_str());
86 }
87 return out;
88 }
89
90 std::string GetCredType(
91 const std::string& worker_addr,
92 const std::map<std::string, std::string>& per_worker_credential_types,
93 const std::string& credential_type) {
94 auto it = per_worker_credential_types.find(worker_addr);
95 if (it != per_worker_credential_types.end()) {
96 return it->second;
97 }
98 return credential_type;
99 }
100
101 // helpers for postprocess_scenario_result
102 static double WallTime(const ClientStats& s) { return s.time_elapsed(); }
103 static double SystemTime(const ClientStats& s) { return s.time_system(); }
104 static double UserTime(const ClientStats& s) { return s.time_user(); }
105 static double CliPollCount(const ClientStats& s) { return s.cq_poll_count(); }
106 static double SvrPollCount(const ServerStats& s) { return s.cq_poll_count(); }
107 static double ServerWallTime(const ServerStats& s) { return s.time_elapsed(); }
108 static double ServerSystemTime(const ServerStats& s) { return s.time_system(); }
109 static double ServerUserTime(const ServerStats& s) { return s.time_user(); }
110 static double ServerTotalCpuTime(const ServerStats& s) {
111 return s.total_cpu_time();
112 }
113 static double ServerIdleCpuTime(const ServerStats& s) {
114 return s.idle_cpu_time();
115 }
116 static int Cores(int n) { return n; }
117
118 static bool IsSuccess(const Status& s) {
119 if (s.ok()) return true;
120 // Since we shutdown servers and clients at the same time, they both can
121 // observe cancellation. Thus, we consider CANCELLED as good status.
122 if (static_cast<StatusCode>(s.error_code()) == StatusCode::CANCELLED) {
123 return true;
124 }
125 // Since we shutdown servers and clients at the same time, server can close
126 // the socket before the client attempts to do that, and vice versa. Thus
127 // receiving a "Socket closed" error is fine.
128 if (s.error_message() == "Socket closed") return true;
129 return false;
130 }
131
132 // Postprocess ScenarioResult and populate result summary.
133 static void postprocess_scenario_result(ScenarioResult* result) {
134 // Get latencies from ScenarioResult latencies histogram and populate to
135 // result summary.
136 Histogram histogram;
137 histogram.MergeProto(result->latencies());
138 result->mutable_summary()->set_latency_50(histogram.Percentile(50));
139 result->mutable_summary()->set_latency_90(histogram.Percentile(90));
140 result->mutable_summary()->set_latency_95(histogram.Percentile(95));
141 result->mutable_summary()->set_latency_99(histogram.Percentile(99));
142 result->mutable_summary()->set_latency_999(histogram.Percentile(99.9));
143
144 // Calculate qps and cpu load for each client and then aggregate results for
145 // all clients
146 double qps = 0;
147 double client_system_cpu_load = 0, client_user_cpu_load = 0;
148 for (int i = 0; i < result->client_stats_size(); i++) {
149 auto client_stat = result->client_stats(i);
150 qps += client_stat.latencies().count() / client_stat.time_elapsed();
151 client_system_cpu_load +=
152 client_stat.time_system() / client_stat.time_elapsed();
153 client_user_cpu_load +=
154 client_stat.time_user() / client_stat.time_elapsed();
155 }
156 // Calculate cpu load for each server and then aggregate results for all
157 // servers
158 double server_system_cpu_load = 0, server_user_cpu_load = 0;
159 for (int i = 0; i < result->server_stats_size(); i++) {
160 auto server_stat = result->server_stats(i);
161 server_system_cpu_load +=
162 server_stat.time_system() / server_stat.time_elapsed();
163 server_user_cpu_load +=
164 server_stat.time_user() / server_stat.time_elapsed();
165 }
166 result->mutable_summary()->set_qps(qps);
167 // Populate the percentage of cpu load to result summary.
168 result->mutable_summary()->set_server_system_time(100 *
169 server_system_cpu_load);
170 result->mutable_summary()->set_server_user_time(100 * server_user_cpu_load);
171 result->mutable_summary()->set_client_system_time(100 *
172 client_system_cpu_load);
173 result->mutable_summary()->set_client_user_time(100 * client_user_cpu_load);
174
175 // For Non-linux platform, get_cpu_usage() is not implemented. Thus,
176 // ServerTotalCpuTime and ServerIdleCpuTime are both 0.
177 if (average(result->server_stats(), ServerTotalCpuTime) == 0) {
178 result->mutable_summary()->set_server_cpu_usage(0);
179 } else {
180 auto server_cpu_usage =
181 100 - 100 * average(result->server_stats(), ServerIdleCpuTime) /
182 average(result->server_stats(), ServerTotalCpuTime);
183 result->mutable_summary()->set_server_cpu_usage(server_cpu_usage);
184 }
185
186 // Calculate and populate successful request per second and failed requests
187 // per seconds to result summary.
188 auto time_estimate = average(result->client_stats(), WallTime);
189 if (result->request_results_size() > 0) {
190 int64_t successes = 0;
191 int64_t failures = 0;
192 for (int i = 0; i < result->request_results_size(); i++) {
193 const RequestResultCount& rrc = result->request_results(i);
194 if (rrc.status_code() == 0) {
195 successes += rrc.count();
196 } else {
197 failures += rrc.count();
198 }
199 }
200 result->mutable_summary()->set_successful_requests_per_second(
201 successes / time_estimate);
202 result->mutable_summary()->set_failed_requests_per_second(failures /
203 time_estimate);
204 }
205
206 // Fill in data for other metrics required in result summary
207 auto qps_per_server_core = qps / sum(result->server_cores(), Cores);
208 result->mutable_summary()->set_qps_per_server_core(qps_per_server_core);
209 result->mutable_summary()->set_client_polls_per_request(
210 sum(result->client_stats(), CliPollCount) / histogram.Count());
211 result->mutable_summary()->set_server_polls_per_request(
212 sum(result->server_stats(), SvrPollCount) / histogram.Count());
213
214 auto server_queries_per_cpu_sec =
215 histogram.Count() / (sum(result->server_stats(), ServerSystemTime) +
216 sum(result->server_stats(), ServerUserTime));
217 auto client_queries_per_cpu_sec =
218 histogram.Count() / (sum(result->client_stats(), SystemTime) +
219 sum(result->client_stats(), UserTime));
220
221 result->mutable_summary()->set_server_queries_per_cpu_sec(
222 server_queries_per_cpu_sec);
223 result->mutable_summary()->set_client_queries_per_cpu_sec(
224 client_queries_per_cpu_sec);
225 }
226
227 struct ClientData {
228 unique_ptr<WorkerService::Stub> stub;
229 unique_ptr<ClientReaderWriter<ClientArgs, ClientStatus>> stream;
230 };
231
232 struct ServerData {
233 unique_ptr<WorkerService::Stub> stub;
234 unique_ptr<ClientReaderWriter<ServerArgs, ServerStatus>> stream;
235 };
236
237 static void FinishClients(const std::vector<ClientData>& clients,
238 const ClientArgs& client_mark) {
239 gpr_log(GPR_INFO, "Finishing clients");
240 for (size_t i = 0, i_end = clients.size(); i < i_end; i++) {
241 auto client = &clients[i];
242 if (!client->stream->Write(client_mark)) {
243 gpr_log(GPR_ERROR, "Couldn't write mark to client %zu", i);
244 GPR_ASSERT(false);
245 }
246 if (!client->stream->WritesDone()) {
247 gpr_log(GPR_ERROR, "Failed WritesDone for client %zu", i);
248 GPR_ASSERT(false);
249 }
250 }
251 }
252
253 static void ReceiveFinalStatusFromClients(
254 const std::vector<ClientData>& clients, Histogram& merged_latencies,
255 std::unordered_map<int, int64_t>& merged_statuses, ScenarioResult& result) {
256 gpr_log(GPR_INFO, "Receiving final status from clients");
257 ClientStatus client_status;
258 for (size_t i = 0, i_end = clients.size(); i < i_end; i++) {
259 auto client = &clients[i];
260 // Read the client final status
261 if (client->stream->Read(&client_status)) {
262 gpr_log(GPR_INFO, "Received final status from client %zu", i);
263 const auto& stats = client_status.stats();
264 merged_latencies.MergeProto(stats.latencies());
265 for (int i = 0; i < stats.request_results_size(); i++) {
266 merged_statuses[stats.request_results(i).status_code()] +=
267 stats.request_results(i).count();
268 }
269 result.add_client_stats()->CopyFrom(stats);
270 // Check that final status was should be the last message on the client
271 // stream.
272 // TODO(jtattermusch): note that that waiting for Read to return can take
273 // long on some scenarios (e.g. unconstrained streaming_from_server). See
274 // https://github.com/grpc/grpc/blob/3bd0cd208ea549760a2daf595f79b91b247fe240/test/cpp/qps/server_async.cc#L176
275 // where the shutdown delay pretty much determines the wait here.
276 GPR_ASSERT(!client->stream->Read(&client_status));
277 } else {
278 gpr_log(GPR_ERROR, "Couldn't get final status from client %zu", i);
279 GPR_ASSERT(false);
280 }
281 }
282 }
283
284 static void ShutdownClients(const std::vector<ClientData>& clients,
285 ScenarioResult& result) {
286 gpr_log(GPR_INFO, "Shutdown clients");
287 for (size_t i = 0, i_end = clients.size(); i < i_end; i++) {
288 auto client = &clients[i];
289 Status s = client->stream->Finish();
290 // Since we shutdown servers and clients at the same time, clients can
291 // observe cancellation. Thus, we consider both OK and CANCELLED as good
292 // status.
293 const bool success = IsSuccess(s);
294 result.add_client_success(success);
295 if (!success) {
296 gpr_log(GPR_ERROR, "Client %zu had an error %s", i,
297 s.error_message().c_str());
298 GPR_ASSERT(false);
299 }
300 }
301 }
302
303 static void FinishServers(const std::vector<ServerData>& servers,
304 const ServerArgs& server_mark) {
305 gpr_log(GPR_INFO, "Finishing servers");
306 for (size_t i = 0, i_end = servers.size(); i < i_end; i++) {
307 auto server = &servers[i];
308 if (!server->stream->Write(server_mark)) {
309 gpr_log(GPR_ERROR, "Couldn't write mark to server %zu", i);
310 GPR_ASSERT(false);
311 }
312 if (!server->stream->WritesDone()) {
313 gpr_log(GPR_ERROR, "Failed WritesDone for server %zu", i);
314 GPR_ASSERT(false);
315 }
316 }
317 }
318
319 static void ReceiveFinalStatusFromServer(const std::vector<ServerData>& servers,
320 ScenarioResult& result) {
321 gpr_log(GPR_INFO, "Receiving final status from servers");
322 ServerStatus server_status;
323 for (size_t i = 0, i_end = servers.size(); i < i_end; i++) {
324 auto server = &servers[i];
325 // Read the server final status
326 if (server->stream->Read(&server_status)) {
327 gpr_log(GPR_INFO, "Received final status from server %zu", i);
328 result.add_server_stats()->CopyFrom(server_status.stats());
329 result.add_server_cores(server_status.cores());
330 // That final status should be the last message on the server stream
331 GPR_ASSERT(!server->stream->Read(&server_status));
332 } else {
333 gpr_log(GPR_ERROR, "Couldn't get final status from server %zu", i);
334 GPR_ASSERT(false);
335 }
336 }
337 }
338
339 static void ShutdownServers(const std::vector<ServerData>& servers,
340 ScenarioResult& result) {
341 gpr_log(GPR_INFO, "Shutdown servers");
342 for (size_t i = 0, i_end = servers.size(); i < i_end; i++) {
343 auto server = &servers[i];
344 Status s = server->stream->Finish();
345 // Since we shutdown servers and clients at the same time, servers can
346 // observe cancellation. Thus, we consider both OK and CANCELLED as good
347 // status.
348 const bool success = IsSuccess(s);
349 result.add_server_success(success);
350 if (!success) {
351 gpr_log(GPR_ERROR, "Server %zu had an error %s", i,
352 s.error_message().c_str());
353 GPR_ASSERT(false);
354 }
355 }
356 }
357
358 std::vector<grpc::testing::Server*>* g_inproc_servers = nullptr;
359
360 std::unique_ptr<ScenarioResult> RunScenario(
361 const ClientConfig& initial_client_config, size_t num_clients,
362 const ServerConfig& initial_server_config, size_t num_servers,
363 int warmup_seconds, int benchmark_seconds, int spawn_local_worker_count,
364 const std::string& qps_server_target_override,
365 const std::string& credential_type,
366 const std::map<std::string, std::string>& per_worker_credential_types,
367 bool run_inproc, int32_t median_latency_collection_interval_millis) {
368 if (run_inproc) {
369 g_inproc_servers = new std::vector<grpc::testing::Server*>;
370 }
371 // Log everything from the driver
372 gpr_set_log_verbosity(GPR_LOG_SEVERITY_DEBUG);
373
374 // ClientContext allocations (all are destroyed at scope exit)
375 list<ClientContext> contexts;
376 auto alloc_context = [](list<ClientContext>* contexts) {
377 contexts->emplace_back();
378 auto context = &contexts->back();
379 context->set_wait_for_ready(true);
380 return context;
381 };
382
383 // To be added to the result, containing the final configuration used for
384 // client and config (including host, etc.)
385 ClientConfig result_client_config;
386
387 // Get client, server lists; ignore if inproc test
388 auto workers = (!run_inproc) ? get_workers("QPS_WORKERS") : deque<string>();
389 ClientConfig client_config = initial_client_config;
390
391 // Spawn some local workers if desired
392 vector<unique_ptr<QpsWorker>> local_workers;
393 for (int i = 0; i < abs(spawn_local_worker_count); i++) {
394 // act as if we're a new test -- gets a good rng seed
395 static bool called_init = false;
396 if (!called_init) {
397 char args_buf[100];
398 strcpy(args_buf, "some-benchmark");
399 char* args[] = {args_buf};
400 int argc = 1;
401 grpc_test_init(&argc, args);
402 called_init = true;
403 }
404
405 char addr[256];
406 // we use port # of -1 to indicate inproc
407 int driver_port = (!run_inproc) ? grpc_pick_unused_port_or_die() : -1;
408 local_workers.emplace_back(new QpsWorker(driver_port, 0, credential_type));
409 sprintf(addr, "localhost:%d", driver_port);
410 if (spawn_local_worker_count < 0) {
411 workers.push_front(addr);
412 } else {
413 workers.push_back(addr);
414 }
415 }
416 GPR_ASSERT(!workers.empty());
417
418 // if num_clients is set to <=0, do dynamic sizing: all workers
419 // except for servers are clients
420 if (num_clients <= 0) {
421 num_clients = workers.size() - num_servers;
422 }
423
424 // TODO(ctiller): support running multiple configurations, and binpack
425 // client/server pairs
426 // to available workers
427 GPR_ASSERT(workers.size() >= num_clients + num_servers);
428
429 // Trim to just what we need
430 workers.resize(num_clients + num_servers);
431
432 // Start servers
433 std::vector<ServerData> servers(num_servers);
434 std::unordered_map<string, std::deque<int>> hosts_cores;
435 ChannelArguments channel_args;
436
437 for (size_t i = 0; i < num_servers; i++) {
438 gpr_log(GPR_INFO, "Starting server on %s (worker #%" PRIuPTR ")",
439 workers[i].c_str(), i);
440 if (!run_inproc) {
441 servers[i].stub = WorkerService::NewStub(grpc::CreateTestChannel(
442 workers[i],
443 GetCredType(workers[i], per_worker_credential_types, credential_type),
444 nullptr /* call creds */, {} /* interceptor creators */));
445 } else {
446 servers[i].stub = WorkerService::NewStub(
447 local_workers[i]->InProcessChannel(channel_args));
448 }
449
450 const ServerConfig& server_config = initial_server_config;
451 if (server_config.core_limit() != 0) {
452 gpr_log(GPR_ERROR,
453 "server config core limit is set but ignored by driver");
454 GPR_ASSERT(false);
455 }
456
457 ServerArgs args;
458 *args.mutable_setup() = server_config;
459 servers[i].stream = servers[i].stub->RunServer(alloc_context(&contexts));
460 if (!servers[i].stream->Write(args)) {
461 gpr_log(GPR_ERROR, "Could not write args to server %zu", i);
462 GPR_ASSERT(false);
463 }
464 ServerStatus init_status;
465 if (!servers[i].stream->Read(&init_status)) {
466 gpr_log(GPR_ERROR, "Server %zu did not yield initial status", i);
467 GPR_ASSERT(false);
468 }
469 if (run_inproc) {
470 std::string cli_target(INPROC_NAME_PREFIX);
471 cli_target += std::to_string(i);
472 client_config.add_server_targets(cli_target);
473 } else {
474 std::string host = get_host(workers[i]);
475 std::string cli_target =
476 grpc_core::JoinHostPort(host.c_str(), init_status.port());
477 client_config.add_server_targets(cli_target.c_str());
478 }
479 }
480 if (qps_server_target_override.length() > 0) {
481 // overriding the qps server target only makes since if there is <= 1
482 // servers
483 GPR_ASSERT(num_servers <= 1);
484 client_config.clear_server_targets();
485 client_config.add_server_targets(qps_server_target_override);
486 }
487 client_config.set_median_latency_collection_interval_millis(
488 median_latency_collection_interval_millis);
489
490 // Targets are all set by now
491 result_client_config = client_config;
492 // Start clients
493 std::vector<ClientData> clients(num_clients);
494 size_t channels_allocated = 0;
495 for (size_t i = 0; i < num_clients; i++) {
496 const auto& worker = workers[i + num_servers];
497 gpr_log(GPR_INFO, "Starting client on %s (worker #%" PRIuPTR ")",
498 worker.c_str(), i + num_servers);
499 if (!run_inproc) {
500 clients[i].stub = WorkerService::NewStub(grpc::CreateTestChannel(
501 worker,
502 GetCredType(worker, per_worker_credential_types, credential_type),
503 nullptr /* call creds */, {} /* interceptor creators */));
504 } else {
505 clients[i].stub = WorkerService::NewStub(
506 local_workers[i + num_servers]->InProcessChannel(channel_args));
507 }
508 ClientConfig per_client_config = client_config;
509
510 if (initial_client_config.core_limit() != 0) {
511 gpr_log(GPR_ERROR, "client config core limit set but ignored");
512 GPR_ASSERT(false);
513 }
514
515 // Reduce channel count so that total channels specified is held regardless
516 // of the number of clients available
517 size_t num_channels =
518 (client_config.client_channels() - channels_allocated) /
519 (num_clients - i);
520 channels_allocated += num_channels;
521 gpr_log(GPR_DEBUG, "Client %" PRIdPTR " gets %" PRIdPTR " channels", i,
522 num_channels);
523 per_client_config.set_client_channels(num_channels);
524
525 ClientArgs args;
526 *args.mutable_setup() = per_client_config;
527 clients[i].stream = clients[i].stub->RunClient(alloc_context(&contexts));
528 if (!clients[i].stream->Write(args)) {
529 gpr_log(GPR_ERROR, "Could not write args to client %zu", i);
530 GPR_ASSERT(false);
531 }
532 }
533
534 for (size_t i = 0; i < num_clients; i++) {
535 ClientStatus init_status;
536 if (!clients[i].stream->Read(&init_status)) {
537 gpr_log(GPR_ERROR, "Client %zu did not yield initial status", i);
538 GPR_ASSERT(false);
539 }
540 }
541
542 // Send an initial mark: clients can use this to know that everything is ready
543 // to start
544 gpr_log(GPR_INFO, "Initiating");
545 ServerArgs server_mark;
546 server_mark.mutable_mark()->set_reset(true);
547 ClientArgs client_mark;
548 client_mark.mutable_mark()->set_reset(true);
549 ServerStatus server_status;
550 ClientStatus client_status;
551 for (size_t i = 0; i < num_clients; i++) {
552 auto client = &clients[i];
553 if (!client->stream->Write(client_mark)) {
554 gpr_log(GPR_ERROR, "Couldn't write mark to client %zu", i);
555 GPR_ASSERT(false);
556 }
557 }
558 for (size_t i = 0; i < num_clients; i++) {
559 auto client = &clients[i];
560 if (!client->stream->Read(&client_status)) {
561 gpr_log(GPR_ERROR, "Couldn't get status from client %zu", i);
562 GPR_ASSERT(false);
563 }
564 }
565
566 // Let everything warmup
567 gpr_log(GPR_INFO, "Warming up");
568 gpr_timespec start = gpr_now(GPR_CLOCK_REALTIME);
569 gpr_sleep_until(
570 gpr_time_add(start, gpr_time_from_seconds(warmup_seconds, GPR_TIMESPAN)));
571
572 // Start a run
573 gpr_log(GPR_INFO, "Starting");
574
575 auto start_time = time(nullptr);
576
577 for (size_t i = 0; i < num_servers; i++) {
578 auto server = &servers[i];
579 if (!server->stream->Write(server_mark)) {
580 gpr_log(GPR_ERROR, "Couldn't write mark to server %zu", i);
581 GPR_ASSERT(false);
582 }
583 }
584 for (size_t i = 0; i < num_clients; i++) {
585 auto client = &clients[i];
586 if (!client->stream->Write(client_mark)) {
587 gpr_log(GPR_ERROR, "Couldn't write mark to client %zu", i);
588 GPR_ASSERT(false);
589 }
590 }
591 for (size_t i = 0; i < num_servers; i++) {
592 auto server = &servers[i];
593 if (!server->stream->Read(&server_status)) {
594 gpr_log(GPR_ERROR, "Couldn't get status from server %zu", i);
595 GPR_ASSERT(false);
596 }
597 }
598 for (size_t i = 0; i < num_clients; i++) {
599 auto client = &clients[i];
600 if (!client->stream->Read(&client_status)) {
601 gpr_log(GPR_ERROR, "Couldn't get status from client %zu", i);
602 GPR_ASSERT(false);
603 }
604 }
605
606 // Wait some time
607 gpr_log(GPR_INFO, "Running");
608 // Use gpr_sleep_until rather than this_thread::sleep_until to support
609 // compilers that don't work with this_thread
610 gpr_sleep_until(gpr_time_add(
611 start,
612 gpr_time_from_seconds(warmup_seconds + benchmark_seconds, GPR_TIMESPAN)));
613
614 // Finish a run
615 std::unique_ptr<ScenarioResult> result(new ScenarioResult);
616 Histogram merged_latencies;
617 std::unordered_map<int, int64_t> merged_statuses;
618
619 // For the case where clients lead the test such as UNARY and
620 // STREAMING_FROM_CLIENT, clients need to finish completely while a server
621 // is running to prevent the clients from being stuck while waiting for
622 // the result.
623 bool client_finish_first =
624 (client_config.rpc_type() != STREAMING_FROM_SERVER);
625
626 auto end_time = time(nullptr);
627
628 FinishClients(clients, client_mark);
629
630 if (!client_finish_first) {
631 FinishServers(servers, server_mark);
632 }
633
634 ReceiveFinalStatusFromClients(clients, merged_latencies, merged_statuses,
635 *result);
636 ShutdownClients(clients, *result);
637
638 if (client_finish_first) {
639 FinishServers(servers, server_mark);
640 }
641
642 ReceiveFinalStatusFromServer(servers, *result);
643 ShutdownServers(servers, *result);
644
645 delete g_inproc_servers;
646
647 merged_latencies.FillProto(result->mutable_latencies());
648 for (std::unordered_map<int, int64_t>::iterator it = merged_statuses.begin();
649 it != merged_statuses.end(); ++it) {
650 RequestResultCount* rrc = result->add_request_results();
651 rrc->set_status_code(it->first);
652 rrc->set_count(it->second);
653 }
654
655 // Fill in start and end time for the test scenario
656 result->mutable_summary()->mutable_start_time()->set_seconds(start_time);
657 result->mutable_summary()->mutable_end_time()->set_seconds(end_time);
658
659 postprocess_scenario_result(result.get());
660 return result;
661 }
662
663 bool RunQuit(
664 const std::string& credential_type,
665 const std::map<std::string, std::string>& per_worker_credential_types) {
666 // Get client, server lists
667 bool result = true;
668 auto workers = get_workers("QPS_WORKERS");
669 if (workers.empty()) {
670 return false;
671 }
672
673 for (size_t i = 0; i < workers.size(); i++) {
674 auto stub = WorkerService::NewStub(grpc::CreateTestChannel(
675 workers[i],
676 GetCredType(workers[i], per_worker_credential_types, credential_type),
677 nullptr /* call creds */, {} /* interceptor creators */));
678 Void phony;
679 grpc::ClientContext ctx;
680 ctx.set_wait_for_ready(true);
681 Status s = stub->QuitWorker(&ctx, phony, &phony);
682 if (!s.ok()) {
683 gpr_log(GPR_ERROR, "Worker %zu could not be properly quit because %s", i,
684 s.error_message().c_str());
685 result = false;
686 }
687 }
688 return result;
689 }
690
691 } // namespace testing
692 } // namespace grpc