client_sync.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <cassert>
  34. #include <chrono>
  35. #include <memory>
  36. #include <mutex>
  37. #include <string>
  38. #include <thread>
  39. #include <vector>
  40. #include <sstream>
  41. #include <sys/signal.h>
  42. #include <grpc/grpc.h>
  43. #include <grpc/support/alloc.h>
  44. #include <grpc/support/histogram.h>
  45. #include <grpc/support/log.h>
  46. #include <grpc/support/host_port.h>
  47. #include <gflags/gflags.h>
  48. #include <grpc++/client_context.h>
  49. #include <grpc++/server.h>
  50. #include <grpc++/server_builder.h>
  51. #include <grpc++/status.h>
  52. #include <grpc++/stream.h>
  53. #include <gtest/gtest.h>
  54. #include "test/cpp/util/create_test_channel.h"
  55. #include "test/cpp/qps/client.h"
  56. #include "test/cpp/qps/qpstest.grpc.pb.h"
  57. #include "test/cpp/qps/histogram.h"
  58. #include "test/cpp/qps/interarrival.h"
  59. #include "test/cpp/qps/timer.h"
  60. namespace grpc {
  61. namespace testing {
  62. class SynchronousClient : public Client {
  63. public:
  64. SynchronousClient(const ClientConfig& config) : Client(config) {
  65. num_threads_ =
  66. config.outstanding_rpcs_per_channel() * config.client_channels();
  67. responses_.resize(num_threads_);
  68. SetupLoadTest(config, num_threads_);
  69. }
  70. virtual ~SynchronousClient(){};
  71. protected:
  72. void WaitToIssue(int thread_idx) {
  73. std::chrono::time_point<std::chrono::high_resolution_clock> next_time;
  74. if (NextIssueTime(thread_idx, &next_time)) {
  75. std::this_thread::sleep_until(next_time);
  76. }
  77. }
  78. size_t num_threads_;
  79. std::vector<SimpleResponse> responses_;
  80. };
  81. class SynchronousUnaryClient GRPC_FINAL : public SynchronousClient {
  82. public:
  83. SynchronousUnaryClient(const ClientConfig& config)
  84. : SynchronousClient(config) {
  85. StartThreads(num_threads_);
  86. }
  87. ~SynchronousUnaryClient() { EndThreads(); }
  88. bool ThreadFunc(Histogram* histogram, size_t thread_idx) GRPC_OVERRIDE {
  89. WaitToIssue(thread_idx);
  90. auto* stub = channels_[thread_idx % channels_.size()].get_stub();
  91. double start = Timer::Now();
  92. grpc::ClientContext context;
  93. grpc::Status s =
  94. stub->UnaryCall(&context, request_, &responses_[thread_idx]);
  95. histogram->Add((Timer::Now() - start) * 1e9);
  96. return s.IsOk();
  97. }
  98. };
  99. class SynchronousStreamingClient GRPC_FINAL : public SynchronousClient {
  100. public:
  101. SynchronousStreamingClient(const ClientConfig& config)
  102. : SynchronousClient(config), context_(num_threads_), stream_(num_threads_) {
  103. for (size_t thread_idx = 0; thread_idx < num_threads_; thread_idx++) {
  104. auto* stub = channels_[thread_idx % channels_.size()].get_stub();
  105. stream_[thread_idx] = stub->StreamingCall(&context_[thread_idx]);
  106. }
  107. StartThreads(num_threads_);
  108. }
  109. ~SynchronousStreamingClient() {
  110. EndThreads();
  111. for (auto stream = stream_.begin(); stream != stream_.end(); stream++) {
  112. if (*stream) {
  113. (*stream)->WritesDone();
  114. EXPECT_TRUE((*stream)->Finish().IsOk());
  115. }
  116. }
  117. }
  118. bool ThreadFunc(Histogram* histogram, size_t thread_idx) GRPC_OVERRIDE {
  119. WaitToIssue(thread_idx);
  120. double start = Timer::Now();
  121. if (stream_[thread_idx]->Write(request_) &&
  122. stream_[thread_idx]->Read(&responses_[thread_idx])) {
  123. histogram->Add((Timer::Now() - start) * 1e9);
  124. return true;
  125. }
  126. return false;
  127. }
  128. private:
  129. std::vector<grpc::ClientContext> context_;
  130. std::vector<std::unique_ptr<grpc::ClientReaderWriter<
  131. SimpleRequest, SimpleResponse>>> stream_;
  132. };
  133. std::unique_ptr<Client> CreateSynchronousUnaryClient(
  134. const ClientConfig& config) {
  135. return std::unique_ptr<Client>(new SynchronousUnaryClient(config));
  136. }
  137. std::unique_ptr<Client> CreateSynchronousStreamingClient(
  138. const ClientConfig& config) {
  139. return std::unique_ptr<Client>(new SynchronousStreamingClient(config));
  140. }
  141. } // namespace testing
  142. } // namespace grpc