stress_interop_client.cc
Go to the documentation of this file.
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 
20 
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 #include <grpc/support/log.h>
26 #include <grpcpp/create_channel.h>
27 
30 
31 namespace grpc {
32 namespace testing {
33 
34 using std::pair;
35 using std::vector;
36 
38  const vector<pair<TestCaseType, int>>& tests)
39  : tests_(tests) {
40  total_weight_ = 0;
41  for (auto it = tests.begin(); it != tests.end(); it++) {
42  total_weight_ += it->second;
43  }
44 }
45 
46 // Returns a weighted-randomly selected test case based on the test weights
47 // passed in the constructror
49  int random = 0;
50  TestCaseType selected_test = UNKNOWN_TEST;
51 
52  // Get a random number from [0 to the total_weight - 1]
53  random = rand() % total_weight_;
54 
55  int weight_sofar = 0;
56  for (auto it = tests_.begin(); it != tests_.end(); it++) {
57  weight_sofar += it->second;
58  if (random < weight_sofar) {
59  selected_test = it->first;
60  break;
61  }
62  }
63 
64  // It is a bug in the logic if no test is selected at this point
65  GPR_ASSERT(selected_test != UNKNOWN_TEST);
66  return selected_test;
67 }
68 
70  int test_id, const std::string& server_address,
71  ChannelCreationFunc channel_creation_func,
72  const WeightedRandomTestSelector& test_selector, long test_duration_secs,
73  long sleep_duration_ms, bool do_not_abort_on_transient_failures)
74  : test_id_(test_id),
76  channel_creation_func_(std::move(channel_creation_func)),
77  interop_client_(new InteropClient(channel_creation_func_, false,
78  do_not_abort_on_transient_failures)),
79  test_selector_(test_selector),
80  test_duration_secs_(test_duration_secs),
81  sleep_duration_ms_(sleep_duration_ms) {}
82 
84  const std::shared_ptr<QpsGauge>& qps_gauge) {
85  gpr_log(GPR_INFO, "Running test %d. ServerAddr: %s", test_id_,
86  server_address_.c_str());
87 
88  gpr_timespec test_end_time;
89  if (test_duration_secs_ < 0) {
90  test_end_time = gpr_inf_future(GPR_CLOCK_REALTIME);
91  } else {
92  test_end_time =
95  }
96 
97  qps_gauge->Reset();
98 
99  while (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), test_end_time) < 0) {
100  // Select the test case to execute based on the weights and execute it
102  gpr_log(GPR_DEBUG, "%d - Executing the test case %d", test_id_, test_case);
103  RunTest(test_case);
104 
105  qps_gauge->Incr();
106 
107  // Sleep between successive calls if needed
108  if (sleep_duration_ms_ > 0) {
109  gpr_timespec sleep_time =
112  gpr_sleep_until(sleep_time);
113  }
114  }
115 }
116 
118  bool is_success = false;
119  switch (test_case) {
120  case EMPTY_UNARY: {
121  is_success = interop_client_->DoEmpty();
122  break;
123  }
124  case LARGE_UNARY: {
125  is_success = interop_client_->DoLargeUnary();
126  break;
127  }
129  is_success = interop_client_->DoClientCompressedUnary();
130  break;
131  }
133  is_success = interop_client_->DoClientCompressedStreaming();
134  break;
135  }
136  case CLIENT_STREAMING: {
137  is_success = interop_client_->DoRequestStreaming();
138  break;
139  }
140  case SERVER_STREAMING: {
141  is_success = interop_client_->DoResponseStreaming();
142  break;
143  }
145  is_success = interop_client_->DoServerCompressedUnary();
146  break;
147  }
149  is_success = interop_client_->DoServerCompressedStreaming();
150  break;
151  }
152  case SLOW_CONSUMER: {
153  is_success = interop_client_->DoResponseStreamingWithSlowConsumer();
154  break;
155  }
156  case HALF_DUPLEX: {
157  is_success = interop_client_->DoHalfDuplex();
158  break;
159  }
160  case PING_PONG: {
161  is_success = interop_client_->DoPingPong();
162  break;
163  }
164  case CANCEL_AFTER_BEGIN: {
165  is_success = interop_client_->DoCancelAfterBegin();
166  break;
167  }
169  is_success = interop_client_->DoCancelAfterFirstResponse();
170  break;
171  }
173  is_success = interop_client_->DoTimeoutOnSleepingServer();
174  break;
175  }
176  case EMPTY_STREAM: {
177  is_success = interop_client_->DoEmptyStream();
178  break;
179  }
181  is_success = interop_client_->DoStatusWithMessage();
182  break;
183  }
184  case CUSTOM_METADATA: {
185  is_success = interop_client_->DoCustomMetadata();
186  break;
187  }
188  default: {
189  gpr_log(GPR_ERROR, "Invalid test case (%d)", test_case);
190  GPR_ASSERT(false);
191  break;
192  }
193  }
194 
195  return is_success;
196 }
197 
198 } // namespace testing
199 } // namespace grpc
GPR_TIMESPAN
@ GPR_TIMESPAN
Definition: gpr_types.h:45
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
testing
Definition: aws_request_signer_test.cc:25
regen-readme.it
it
Definition: regen-readme.py:15
log.h
grpc
Definition: grpcpp/alarm.h:33
grpc::testing::WeightedRandomTestSelector::GetNextTest
TestCaseType GetNextTest() const
Definition: stress_interop_client.cc:48
grpc::testing::SERVER_COMPRESSED_STREAMING
@ SERVER_COMPRESSED_STREAMING
Definition: stress_interop_client.h:46
grpc::testing::EMPTY_STREAM
@ EMPTY_STREAM
Definition: stress_interop_client.h:53
false
#define false
Definition: setup_once.h:323
grpc::testing::SLOW_CONSUMER
@ SLOW_CONSUMER
Definition: stress_interop_client.h:47
stress_interop_client.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:55
grpc::testing::CLIENT_STREAMING
@ CLIENT_STREAMING
Definition: stress_interop_client.h:43
env.new
def new
Definition: env.py:51
grpc::testing::CLIENT_COMPRESSED_UNARY
@ CLIENT_COMPRESSED_UNARY
Definition: stress_interop_client.h:41
grpc::testing::TIMEOUT_ON_SLEEPING_SERVER
@ TIMEOUT_ON_SLEEPING_SERVER
Definition: stress_interop_client.h:52
grpc::testing::StressTestInteropClient::test_selector_
const WeightedRandomTestSelector & test_selector_
Definition: stress_interop_client.h:111
server_address
std::string server_address("0.0.0.0:10000")
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc::testing::StressTestInteropClient::test_duration_secs_
long test_duration_secs_
Definition: stress_interop_client.h:112
gpr_time_cmp
GPRAPI int gpr_time_cmp(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:30
grpc::testing::SERVER_STREAMING
@ SERVER_STREAMING
Definition: stress_interop_client.h:44
metrics_server.h
grpc::testing::StressTestInteropClient::interop_client_
std::unique_ptr< InteropClient > interop_client_
Definition: stress_interop_client.h:110
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
gpr_sleep_until
GPRAPI void gpr_sleep_until(gpr_timespec until)
grpc::testing::WeightedRandomTestSelector
Definition: stress_interop_client.h:77
grpc::testing::CUSTOM_METADATA
@ CUSTOM_METADATA
Definition: stress_interop_client.h:55
grpc::testing::EMPTY_UNARY
@ EMPTY_UNARY
Definition: stress_interop_client.h:39
grpc::testing::StressTestInteropClient::StressTestInteropClient
StressTestInteropClient(int test_id, const std::string &server_address, ChannelCreationFunc channel_creation_func, const WeightedRandomTestSelector &test_selector, long test_duration_secs, long sleep_duration_ms, bool do_not_abort_on_transient_failures)
Definition: stress_interop_client.cc:69
grpc::testing::PING_PONG
@ PING_PONG
Definition: stress_interop_client.h:49
grpc::testing::StressTestInteropClient::MainLoop
void MainLoop(const std::shared_ptr< QpsGauge > &qps_gauge)
Definition: stress_interop_client.cc:83
interop_client.h
grpc::testing::SERVER_COMPRESSED_UNARY
@ SERVER_COMPRESSED_UNARY
Definition: stress_interop_client.h:45
grpc::testing::InteropClient
Definition: interop_client.h:40
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
grpc::testing::WeightedRandomTestSelector::total_weight_
int total_weight_
Definition: stress_interop_client.h:89
grpc::testing::StressTestInteropClient::RunTest
bool RunTest(TestCaseType test_case)
Definition: stress_interop_client.cc:117
grpc::testing::ChannelCreationFunc
std::function< std::shared_ptr< Channel >void)> ChannelCreationFunc
Definition: interop_client.h:38
grpc::testing::STATUS_CODE_AND_MESSAGE
@ STATUS_CODE_AND_MESSAGE
Definition: stress_interop_client.h:54
grpc::testing::UNKNOWN_TEST
@ UNKNOWN_TEST
Definition: stress_interop_client.h:38
grpc::testing::StressTestInteropClient::test_id_
int test_id_
Definition: stress_interop_client.h:107
grpc::testing::WeightedRandomTestSelector::WeightedRandomTestSelector
WeightedRandomTestSelector(const vector< pair< TestCaseType, int >> &tests)
Definition: stress_interop_client.cc:37
grpc::testing::LARGE_UNARY
@ LARGE_UNARY
Definition: stress_interop_client.h:40
gpr_time_add
GPRAPI gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:135
tests
Definition: src/python/grpcio_tests/tests/__init__.py:1
grpc::testing::TestCaseType
TestCaseType
Definition: stress_interop_client.h:37
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc::testing::HALF_DUPLEX
@ HALF_DUPLEX
Definition: stress_interop_client.h:48
server_address_
const char * server_address_
Definition: settings_timeout_test.cc:231
grpc::testing::StressTestInteropClient::sleep_duration_ms_
long sleep_duration_ms_
Definition: stress_interop_client.h:113
gpr_time_from_millis
GPRAPI gpr_timespec gpr_time_from_millis(int64_t ms, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:119
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
grpc::testing::WeightedRandomTestSelector::tests_
const vector< pair< TestCaseType, int > > tests_
Definition: stress_interop_client.h:88
gpr_timespec
Definition: gpr_types.h:50
grpc::testing::CLIENT_COMPRESSED_STREAMING
@ CLIENT_COMPRESSED_STREAMING
Definition: stress_interop_client.h:42
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
pair
std::pair< std::string, std::string > pair
Definition: abseil-cpp/absl/container/internal/raw_hash_set_benchmark.cc:78
grpc::testing::CANCEL_AFTER_BEGIN
@ CANCEL_AFTER_BEGIN
Definition: stress_interop_client.h:50
create_channel.h
grpc::testing::CANCEL_AFTER_FIRST_RESPONSE
@ CANCEL_AFTER_FIRST_RESPONSE
Definition: stress_interop_client.h:51
grpc::testing::StressTestInteropClient::server_address_
const std::string & server_address_
Definition: stress_interop_client.h:108
gpr_time_from_seconds
GPRAPI gpr_timespec gpr_time_from_seconds(int64_t s, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:123


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:21