driver.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include "test/cpp/qps/driver.h"
  34. #include "src/core/support/env.h"
  35. #include <grpc/support/alloc.h>
  36. #include <grpc/support/log.h>
  37. #include <grpc/support/host_port.h>
  38. #include <grpc++/channel_arguments.h>
  39. #include <grpc++/client_context.h>
  40. #include <grpc++/create_channel.h>
  41. #include <grpc++/stream.h>
  42. #include <list>
  43. #include <thread>
  44. #include <deque>
  45. #include <vector>
  46. #include <unistd.h>
  47. #include "test/cpp/qps/histogram.h"
  48. #include "test/cpp/qps/qps_worker.h"
  49. #include "test/core/util/port.h"
  50. using std::list;
  51. using std::thread;
  52. using std::unique_ptr;
  53. using std::deque;
  54. using std::vector;
  55. namespace grpc {
  56. namespace testing {
  57. static deque<string> get_hosts(const string& name) {
  58. char* env = gpr_getenv(name.c_str());
  59. if (!env) return deque<string>();
  60. deque<string> out;
  61. char* p = env;
  62. for (;;) {
  63. char* comma = strchr(p, ',');
  64. if (comma) {
  65. out.emplace_back(p, comma);
  66. p = comma + 1;
  67. } else {
  68. out.emplace_back(p);
  69. gpr_free(env);
  70. return out;
  71. }
  72. }
  73. }
  74. ScenarioResult RunScenario(const ClientConfig& initial_client_config,
  75. size_t num_clients,
  76. const ServerConfig& server_config,
  77. size_t num_servers,
  78. int warmup_seconds,
  79. int benchmark_seconds,
  80. int spawn_local_worker_count) {
  81. // ClientContext allocator (all are destroyed at scope exit)
  82. list<ClientContext> contexts;
  83. auto alloc_context = [&contexts]() {
  84. contexts.emplace_back();
  85. return &contexts.back();
  86. };
  87. srand(time(NULL) ^ getpid());
  88. // Get client, server lists
  89. auto workers = get_hosts("QPS_WORKERS");
  90. ClientConfig client_config = initial_client_config;
  91. // Spawn some local workers if desired
  92. vector<unique_ptr<QpsWorker>> local_workers;
  93. for (int i = 0; i < abs(spawn_local_worker_count); i++) {
  94. int driver_port = grpc_pick_unused_port_or_die();
  95. int benchmark_port = grpc_pick_unused_port_or_die();
  96. local_workers.emplace_back(new QpsWorker(driver_port, benchmark_port));
  97. char addr[256];
  98. sprintf(addr, "localhost:%d", driver_port);
  99. if (spawn_local_worker_count < 0) {
  100. workers.push_front(addr);
  101. } else {
  102. workers.push_back(addr);
  103. }
  104. }
  105. // TODO(ctiller): support running multiple configurations, and binpack
  106. // client/server pairs
  107. // to available workers
  108. GPR_ASSERT(workers.size() >= num_clients + num_servers);
  109. // Trim to just what we need
  110. workers.resize(num_clients + num_servers);
  111. // Start servers
  112. struct ServerData {
  113. unique_ptr<Worker::Stub> stub;
  114. unique_ptr<ClientReaderWriter<ServerArgs, ServerStatus>> stream;
  115. };
  116. vector<ServerData> servers;
  117. for (size_t i = 0; i < num_servers; i++) {
  118. ServerData sd;
  119. sd.stub = std::move(Worker::NewStub(
  120. CreateChannel(workers[i], InsecureCredentials(), ChannelArguments())));
  121. ServerArgs args;
  122. *args.mutable_setup() = server_config;
  123. sd.stream = std::move(sd.stub->RunServer(alloc_context()));
  124. GPR_ASSERT(sd.stream->Write(args));
  125. ServerStatus init_status;
  126. GPR_ASSERT(sd.stream->Read(&init_status));
  127. char* host;
  128. char* driver_port;
  129. char* cli_target;
  130. gpr_split_host_port(workers[i].c_str(), &host, &driver_port);
  131. gpr_join_host_port(&cli_target, host, init_status.port());
  132. client_config.add_server_targets(cli_target);
  133. gpr_free(host);
  134. gpr_free(driver_port);
  135. gpr_free(cli_target);
  136. servers.push_back(std::move(sd));
  137. }
  138. // Start clients
  139. struct ClientData {
  140. unique_ptr<Worker::Stub> stub;
  141. unique_ptr<ClientReaderWriter<ClientArgs, ClientStatus>> stream;
  142. };
  143. vector<ClientData> clients;
  144. for (size_t i = 0; i < num_clients; i++) {
  145. ClientData cd;
  146. cd.stub = std::move(Worker::NewStub(CreateChannel(
  147. workers[i + num_servers], InsecureCredentials(), ChannelArguments())));
  148. ClientArgs args;
  149. *args.mutable_setup() = client_config;
  150. cd.stream = std::move(cd.stub->RunTest(alloc_context()));
  151. GPR_ASSERT(cd.stream->Write(args));
  152. ClientStatus init_status;
  153. GPR_ASSERT(cd.stream->Read(&init_status));
  154. clients.push_back(std::move(cd));
  155. }
  156. // Let everything warmup
  157. gpr_log(GPR_INFO, "Warming up");
  158. gpr_timespec start = gpr_now();
  159. gpr_sleep_until(gpr_time_add(start, gpr_time_from_seconds(warmup_seconds)));
  160. // Start a run
  161. gpr_log(GPR_INFO, "Starting");
  162. ServerArgs server_mark;
  163. server_mark.mutable_mark();
  164. ClientArgs client_mark;
  165. client_mark.mutable_mark();
  166. for (auto server = servers.begin(); server != servers.end(); server++) {
  167. GPR_ASSERT(server->stream->Write(server_mark));
  168. }
  169. for (auto client = clients.begin(); client != clients.end(); client++) {
  170. GPR_ASSERT(client->stream->Write(client_mark));
  171. }
  172. ServerStatus server_status;
  173. ClientStatus client_status;
  174. for (auto server = servers.begin(); server != servers.end(); server++) {
  175. GPR_ASSERT(server->stream->Read(&server_status));
  176. }
  177. for (auto client = clients.begin(); client != clients.end(); client++) {
  178. GPR_ASSERT(client->stream->Read(&client_status));
  179. }
  180. // Wait some time
  181. gpr_log(GPR_INFO, "Running");
  182. gpr_sleep_until(gpr_time_add(start, gpr_time_from_seconds(benchmark_seconds)));
  183. // Finish a run
  184. ScenarioResult result;
  185. gpr_log(GPR_INFO, "Finishing");
  186. for (auto server = servers.begin(); server != servers.end(); server++) {
  187. GPR_ASSERT(server->stream->Write(server_mark));
  188. }
  189. for (auto client = clients.begin(); client != clients.end(); client++) {
  190. GPR_ASSERT(client->stream->Write(client_mark));
  191. }
  192. for (auto server = servers.begin(); server != servers.end(); server++) {
  193. GPR_ASSERT(server->stream->Read(&server_status));
  194. const auto& stats = server_status.stats();
  195. result.server_resources.push_back(ResourceUsage{
  196. stats.time_elapsed(), stats.time_user(), stats.time_system()});
  197. }
  198. for (auto client = clients.begin(); client != clients.end(); client++) {
  199. GPR_ASSERT(client->stream->Read(&client_status));
  200. const auto& stats = client_status.stats();
  201. result.latencies.MergeProto(stats.latencies());
  202. result.client_resources.push_back(ResourceUsage{
  203. stats.time_elapsed(), stats.time_user(), stats.time_system()});
  204. }
  205. for (auto client = clients.begin(); client != clients.end(); client++) {
  206. GPR_ASSERT(client->stream->WritesDone());
  207. GPR_ASSERT(client->stream->Finish().IsOk());
  208. }
  209. for (auto server = servers.begin(); server != servers.end(); server++) {
  210. GPR_ASSERT(server->stream->WritesDone());
  211. GPR_ASSERT(server->stream->Finish().IsOk());
  212. }
  213. return result;
  214. }
  215. } // namespace testing
  216. } // namespace grpc