stress_interop_client.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. *is % allowed in string
  17. */
  18. #include "test/cpp/interop/stress_interop_client.h"
  19. #include <memory>
  20. #include <string>
  21. #include <vector>
  22. #include <grpc/support/log.h>
  23. #include <grpcpp/create_channel.h>
  24. #include "test/cpp/interop/interop_client.h"
  25. #include "test/cpp/util/metrics_server.h"
  26. namespace grpc {
  27. namespace testing {
  28. using std::pair;
  29. using std::vector;
  30. WeightedRandomTestSelector::WeightedRandomTestSelector(
  31. const vector<pair<TestCaseType, int>>& tests)
  32. : tests_(tests) {
  33. total_weight_ = 0;
  34. for (auto it = tests.begin(); it != tests.end(); it++) {
  35. total_weight_ += it->second;
  36. }
  37. }
  38. // Returns a weighted-randomly selected test case based on the test weights
  39. // passed in the constructror
  40. TestCaseType WeightedRandomTestSelector::GetNextTest() const {
  41. int random = 0;
  42. TestCaseType selected_test = UNKNOWN_TEST;
  43. // Get a random number from [0 to the total_weight - 1]
  44. random = rand() % total_weight_;
  45. int weight_sofar = 0;
  46. for (auto it = tests_.begin(); it != tests_.end(); it++) {
  47. weight_sofar += it->second;
  48. if (random < weight_sofar) {
  49. selected_test = it->first;
  50. break;
  51. }
  52. }
  53. // It is a bug in the logic if no test is selected at this point
  54. GPR_ASSERT(selected_test != UNKNOWN_TEST);
  55. return selected_test;
  56. }
  57. StressTestInteropClient::StressTestInteropClient(
  58. int test_id, const std::string& server_address,
  59. ChannelCreationFunc channel_creation_func,
  60. const WeightedRandomTestSelector& test_selector, long test_duration_secs,
  61. long sleep_duration_ms, bool do_not_abort_on_transient_failures)
  62. : test_id_(test_id),
  63. server_address_(server_address),
  64. channel_creation_func_(std::move(channel_creation_func)),
  65. interop_client_(new InteropClient(channel_creation_func_, false,
  66. do_not_abort_on_transient_failures)),
  67. test_selector_(test_selector),
  68. test_duration_secs_(test_duration_secs),
  69. sleep_duration_ms_(sleep_duration_ms) {}
  70. void StressTestInteropClient::MainLoop(
  71. const std::shared_ptr<QpsGauge>& qps_gauge) {
  72. gpr_log(GPR_INFO, "Running test %d. ServerAddr: %s", test_id_,
  73. server_address_.c_str());
  74. gpr_timespec test_end_time;
  75. if (test_duration_secs_ < 0) {
  76. test_end_time = gpr_inf_future(GPR_CLOCK_REALTIME);
  77. } else {
  78. test_end_time =
  79. gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
  80. gpr_time_from_seconds(test_duration_secs_, GPR_TIMESPAN));
  81. }
  82. qps_gauge->Reset();
  83. while (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), test_end_time) < 0) {
  84. // Select the test case to execute based on the weights and execute it
  85. TestCaseType test_case = test_selector_.GetNextTest();
  86. gpr_log(GPR_DEBUG, "%d - Executing the test case %d", test_id_, test_case);
  87. RunTest(test_case);
  88. qps_gauge->Incr();
  89. // Sleep between successive calls if needed
  90. if (sleep_duration_ms_ > 0) {
  91. gpr_timespec sleep_time =
  92. gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
  93. gpr_time_from_millis(sleep_duration_ms_, GPR_TIMESPAN));
  94. gpr_sleep_until(sleep_time);
  95. }
  96. }
  97. }
  98. bool StressTestInteropClient::RunTest(TestCaseType test_case) {
  99. bool is_success = false;
  100. switch (test_case) {
  101. case EMPTY_UNARY: {
  102. is_success = interop_client_->DoEmpty();
  103. break;
  104. }
  105. case LARGE_UNARY: {
  106. is_success = interop_client_->DoLargeUnary();
  107. break;
  108. }
  109. case CLIENT_COMPRESSED_UNARY: {
  110. is_success = interop_client_->DoClientCompressedUnary();
  111. break;
  112. }
  113. case CLIENT_COMPRESSED_STREAMING: {
  114. is_success = interop_client_->DoClientCompressedStreaming();
  115. break;
  116. }
  117. case CLIENT_STREAMING: {
  118. is_success = interop_client_->DoRequestStreaming();
  119. break;
  120. }
  121. case SERVER_STREAMING: {
  122. is_success = interop_client_->DoResponseStreaming();
  123. break;
  124. }
  125. case SERVER_COMPRESSED_UNARY: {
  126. is_success = interop_client_->DoServerCompressedUnary();
  127. break;
  128. }
  129. case SERVER_COMPRESSED_STREAMING: {
  130. is_success = interop_client_->DoServerCompressedStreaming();
  131. break;
  132. }
  133. case SLOW_CONSUMER: {
  134. is_success = interop_client_->DoResponseStreamingWithSlowConsumer();
  135. break;
  136. }
  137. case HALF_DUPLEX: {
  138. is_success = interop_client_->DoHalfDuplex();
  139. break;
  140. }
  141. case PING_PONG: {
  142. is_success = interop_client_->DoPingPong();
  143. break;
  144. }
  145. case CANCEL_AFTER_BEGIN: {
  146. is_success = interop_client_->DoCancelAfterBegin();
  147. break;
  148. }
  149. case CANCEL_AFTER_FIRST_RESPONSE: {
  150. is_success = interop_client_->DoCancelAfterFirstResponse();
  151. break;
  152. }
  153. case TIMEOUT_ON_SLEEPING_SERVER: {
  154. is_success = interop_client_->DoTimeoutOnSleepingServer();
  155. break;
  156. }
  157. case EMPTY_STREAM: {
  158. is_success = interop_client_->DoEmptyStream();
  159. break;
  160. }
  161. case STATUS_CODE_AND_MESSAGE: {
  162. is_success = interop_client_->DoStatusWithMessage();
  163. break;
  164. }
  165. case CUSTOM_METADATA: {
  166. is_success = interop_client_->DoCustomMetadata();
  167. break;
  168. }
  169. default: {
  170. gpr_log(GPR_ERROR, "Invalid test case (%d)", test_case);
  171. GPR_ASSERT(false);
  172. break;
  173. }
  174. }
  175. return is_success;
  176. }
  177. } // namespace testing
  178. } // namespace grpc