server.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 <sys/signal.h>
  34. #include <thread>
  35. #include <unistd.h>
  36. #include <gflags/gflags.h>
  37. #include <grpc/support/alloc.h>
  38. #include <grpc/support/host_port.h>
  39. #include <grpc++/config.h>
  40. #include <grpc++/server.h>
  41. #include <grpc++/server_builder.h>
  42. #include <grpc++/server_context.h>
  43. #include <grpc++/status.h>
  44. #include <grpc++/stream.h>
  45. #include "src/cpp/server/thread_pool.h"
  46. #include "test/core/util/grpc_profiler.h"
  47. #include "test/cpp/qps/qpstest.pb.h"
  48. #include "test/cpp/qps/timer.h"
  49. #include <grpc/grpc.h>
  50. #include <grpc/support/log.h>
  51. DEFINE_int32(port, 0, "Server port.");
  52. DEFINE_int32(driver_port, 0, "Server driver port.");
  53. using grpc::Server;
  54. using grpc::ServerBuilder;
  55. using grpc::ServerContext;
  56. using grpc::ServerReaderWriter;
  57. using grpc::ThreadPool;
  58. using grpc::testing::Payload;
  59. using grpc::testing::PayloadType;
  60. using grpc::testing::ServerStats;
  61. using grpc::testing::SimpleRequest;
  62. using grpc::testing::SimpleResponse;
  63. using grpc::testing::StatsRequest;
  64. using grpc::testing::TestService;
  65. using grpc::testing::QpsServer;
  66. using grpc::testing::ServerArgs;
  67. using grpc::testing::ServerStats;
  68. using grpc::testing::ServerStatus;
  69. using grpc::Status;
  70. // In some distros, gflags is in the namespace google, and in some others,
  71. // in gflags. This hack is enabling us to find both.
  72. namespace google { }
  73. namespace gflags { }
  74. using namespace google;
  75. using namespace gflags;
  76. static bool got_sigint = false;
  77. static void sigint_handler(int x) { got_sigint = 1; }
  78. static bool SetPayload(PayloadType type, int size, Payload* payload) {
  79. PayloadType response_type = type;
  80. // TODO(yangg): Support UNCOMPRESSABLE payload.
  81. if (type != PayloadType::COMPRESSABLE) {
  82. return false;
  83. }
  84. payload->set_type(response_type);
  85. std::unique_ptr<char[]> body(new char[size]());
  86. payload->set_body(body.get(), size);
  87. return true;
  88. }
  89. namespace {
  90. class TestServiceImpl GRPC_FINAL : public TestService::Service {
  91. public:
  92. Status UnaryCall(ServerContext* context, const SimpleRequest* request,
  93. SimpleResponse* response) override {
  94. if (request->has_response_size() && request->response_size() > 0) {
  95. if (!SetPayload(request->response_type(), request->response_size(),
  96. response->mutable_payload())) {
  97. return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
  98. }
  99. }
  100. return Status::OK;
  101. }
  102. };
  103. } // namespace
  104. class ServerImpl : public QpsServer::Service {
  105. public:
  106. Status RunServer(ServerContext* ctx, ServerReaderWriter<ServerStatus, ServerArgs>* stream) {
  107. ServerArgs args;
  108. if (!stream->Read(&args)) return Status::OK;
  109. std::lock_guard<std::mutex> lock(server_mu_);
  110. char* server_address = NULL;
  111. gpr_join_host_port(&server_address, "::", FLAGS_port);
  112. TestServiceImpl service;
  113. ServerBuilder builder;
  114. builder.AddPort(server_address);
  115. builder.RegisterService(&service);
  116. std::unique_ptr<ThreadPool> pool(new ThreadPool(args.config().threads()));
  117. builder.SetThreadPool(pool.get());
  118. auto server = builder.BuildAndStart();
  119. gpr_log(GPR_INFO, "Server listening on %s\n", server_address);
  120. gpr_free(server_address);
  121. ServerStatus status;
  122. status.set_port(FLAGS_port);
  123. if (!stream->Write(status)) return Status(grpc::UNKNOWN);
  124. grpc_profiler_start("qps_server.prof");
  125. Timer timer;
  126. if (stream->Read(&args)) {
  127. gpr_log(GPR_ERROR, "Got a server request, but not expecting one");
  128. return Status(grpc::UNKNOWN);
  129. }
  130. auto timer_result = timer.Mark();
  131. grpc_profiler_stop();
  132. auto* stats = status.mutable_stats();
  133. stats->set_time_elapsed(timer_result.wall);
  134. stats->set_time_system(timer_result.system);
  135. stats->set_time_user(timer_result.user);
  136. stream->Write(status);
  137. return Status::OK;
  138. }
  139. private:
  140. std::mutex server_mu_;
  141. };
  142. static void RunServer() {
  143. char* server_address = NULL;
  144. gpr_join_host_port(&server_address, "::", FLAGS_driver_port);
  145. ServerImpl service;
  146. ServerBuilder builder;
  147. builder.AddPort(server_address);
  148. builder.RegisterService(&service);
  149. gpr_free(server_address);
  150. auto server = builder.BuildAndStart();
  151. while (!got_sigint) {
  152. sleep(5);
  153. }
  154. }
  155. int main(int argc, char** argv) {
  156. signal(SIGINT, sigint_handler);
  157. grpc_init();
  158. ParseCommandLineFlags(&argc, &argv, true);
  159. GPR_ASSERT(FLAGS_port != 0);
  160. RunServer();
  161. grpc_shutdown();
  162. return 0;
  163. }