driver.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 <vector>
  45. using std::list;
  46. using std::thread;
  47. using std::unique_ptr;
  48. using std::vector;
  49. using grpc::string;
  50. using grpc::ChannelArguments;
  51. using grpc::ClientContext;
  52. using grpc::ClientReaderWriter;
  53. using grpc::CreateChannelDeprecated;
  54. using grpc::Status;
  55. using grpc::testing::ClientArgs;
  56. using grpc::testing::ClientConfig;
  57. using grpc::testing::ClientResult;
  58. using grpc::testing::Worker;
  59. using grpc::testing::ServerArgs;
  60. using grpc::testing::ServerConfig;
  61. using grpc::testing::ServerStatus;
  62. #if 0
  63. static vector<string> get_hosts(const string& name) {
  64. char* env = gpr_getenv(name.c_str());
  65. if (!env) return vector<string>();
  66. vector<string> out;
  67. char* p = env;
  68. for (;;) {
  69. char* comma = strchr(p, ',');
  70. if (comma) {
  71. out.emplace_back(p, comma);
  72. p = comma + 1;
  73. } else {
  74. out.emplace_back(p);
  75. gpr_free(env);
  76. return out;
  77. }
  78. }
  79. }
  80. void RunScenario(const ClientConfig& client_config, size_t num_clients,
  81. const ServerConfig& server_config, size_t num_servers) {
  82. // ClientContext allocator (all are destroyed at scope exit)
  83. list<ClientContext> contexts;
  84. auto alloc_context = [&contexts]() {
  85. contexts.emplace_back();
  86. return &contexts.back();
  87. };
  88. // Get client, server lists
  89. auto workers = get_hosts("QPS_WORKERS");
  90. GPR_ASSERT(clients.size() >= num_clients);
  91. GPR_ASSERT(servers.size() >= num_servers);
  92. // Trim to just what we need
  93. clients.resize(num_clients);
  94. servers.resize(num_servers);
  95. // Start servers
  96. vector<unique_ptr<QpsServer::Stub>> server_stubs;
  97. vector<unique_ptr<ClientReaderWriter<ServerArgs, ServerStatus>>> server_streams;
  98. vector<string> server_targets;
  99. for (const auto& target : servers) {
  100. server_stubs.push_back(QpsServer::NewStub(CreateChannelDeprecated(target, ChannelArguments())));
  101. auto* stub = server_stubs.back().get();
  102. ServerArgs args;
  103. *args.mutable_config() = server_config;
  104. server_streams.push_back(stub->RunServer(alloc_context()));
  105. auto* stream = server_streams.back().get();
  106. if (!stream->Write(args)) {
  107. gpr_log(GPR_ERROR, "Failed starting server");
  108. return;
  109. }
  110. ServerStatus init_status;
  111. if (!stream->Read(&init_status)) {
  112. gpr_log(GPR_ERROR, "Failed starting server");
  113. return;
  114. }
  115. char* host;
  116. char* driver_port;
  117. char* cli_target;
  118. gpr_split_host_port(target.c_str(), &host, &driver_port);
  119. gpr_join_host_port(&cli_target, host, init_status.port());
  120. server_targets.push_back(cli_target);
  121. gpr_free(host);
  122. gpr_free(driver_port);
  123. gpr_free(cli_target);
  124. }
  125. // Start clients
  126. class Client {
  127. public:
  128. Client(ClientContext* ctx, const string& target, const ClientArgs& args)
  129. : thread_([ctx, target, args, this]() {
  130. auto stub = QpsClient::NewStub(CreateChannelDeprecated(target, ChannelArguments()));
  131. status_ = stub->RunTest(ctx, args, &result_);
  132. }) {}
  133. ~Client() { join(); }
  134. void join() { if (!joined_) { thread_.join(); joined_ = true; } }
  135. const Status& status() const { return status_; }
  136. const ClientResult& result() const { return result_; }
  137. private:
  138. bool joined_ = false;
  139. Status status_;
  140. ClientResult result_;
  141. thread thread_;
  142. };
  143. list<Client> running_clients;
  144. size_t svr_idx = 0;
  145. for (const auto& target : clients) {
  146. ClientArgs args;
  147. *args.mutable_config() = client_config;
  148. for (size_t i = 0; i < num_servers; i++) {
  149. args.add_server_targets(server_targets[svr_idx]);
  150. svr_idx = (svr_idx + 1) % num_servers;
  151. }
  152. running_clients.emplace_back(alloc_context(), target, args);
  153. }
  154. // Finish clients
  155. for (auto& client : running_clients) {
  156. client.join();
  157. if (!client.status().IsOk()) {
  158. gpr_log(GPR_ERROR, "Client failed");
  159. return;
  160. }
  161. }
  162. // Finish servers
  163. for (auto& stream : server_streams) {
  164. ServerStatus final_status;
  165. ServerStatus dummy;
  166. if (!stream->WritesDone() || !stream->Read(&final_status) || stream->Read(&dummy) || !stream->Finish().IsOk()) {
  167. gpr_log(GPR_ERROR, "Server protocol error");
  168. }
  169. }
  170. }
  171. #endif