xref: /aosp_15_r20/external/grpc-grpc/test/cpp/interop/stress_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 
19 #include <memory>
20 #include <string>
21 #include <thread>
22 #include <utility>
23 #include <vector>
24 
25 #include "absl/flags/flag.h"
26 
27 #include <grpc/support/log.h>
28 #include <grpc/support/time.h>
29 #include <grpcpp/create_channel.h>
30 #include <grpcpp/grpcpp.h>
31 
32 #include "src/core/lib/gprpp/crash.h"
33 #include "src/proto/grpc/testing/metrics.grpc.pb.h"
34 #include "src/proto/grpc/testing/metrics.pb.h"
35 #include "test/cpp/interop/interop_client.h"
36 #include "test/cpp/interop/stress_interop_client.h"
37 #include "test/cpp/util/create_test_channel.h"
38 #include "test/cpp/util/metrics_server.h"
39 #include "test/cpp/util/test_config.h"
40 
41 extern void gpr_default_log(gpr_log_func_args* args);
42 
43 ABSL_FLAG(int32_t, metrics_port, 8081, "The metrics server port.");
44 
45 // TODO(Capstan): Consider using absl::Duration
46 ABSL_FLAG(int32_t, sleep_duration_ms, 0,
47           "The duration (in millisec) between two"
48           " consecutive test calls (per server) issued by the server.");
49 
50 // TODO(Capstan): Consider using absl::Duration
51 ABSL_FLAG(int32_t, test_duration_secs, -1,
52           "The length of time (in seconds) to run"
53           " the test. Enter -1 if the test should run continuously until"
54           " forcefully terminated.");
55 
56 ABSL_FLAG(std::string, server_addresses, "localhost:8080",
57           "The list of server addresses. The format is: \n"
58           " \"<name_1>:<port_1>,<name_2>:<port_1>...<name_N>:<port_N>\"\n"
59           " Note: <name> can be servername or IP address.");
60 
61 ABSL_FLAG(int32_t, num_channels_per_server, 1,
62           "Number of channels for each server");
63 
64 ABSL_FLAG(int32_t, num_stubs_per_channel, 1,
65           "Number of stubs per each channels to server. This number also "
66           "indicates the max number of parallel RPC calls on each channel "
67           "at any given time.");
68 
69 // TODO(sreek): Add more test cases here in future
70 ABSL_FLAG(std::string, test_cases, "",
71           "List of test cases to call along with the"
72           " relative weights in the following format:\n"
73           " \"<testcase_1:w_1>,<testcase_2:w_2>...<testcase_n:w_n>\"\n"
74           " The following testcases are currently supported:\n"
75           "   empty_unary\n"
76           "   large_unary\n"
77           "   large_compressed_unary\n"
78           "   client_streaming\n"
79           "   server_streaming\n"
80           "   server_compressed_streaming\n"
81           "   slow_consumer\n"
82           "   half_duplex\n"
83           "   ping_pong\n"
84           "   cancel_after_begin\n"
85           "   cancel_after_first_response\n"
86           "   timeout_on_sleeping_server\n"
87           "   empty_stream\n"
88           "   status_code_and_message\n"
89           "   custom_metadata\n"
90           " Example: \"empty_unary:20,large_unary:10,empty_stream:70\"\n"
91           " The above will execute 'empty_unary', 20% of the time,"
92           " 'large_unary', 10% of the time and 'empty_stream' the remaining"
93           " 70% of the time");
94 
95 ABSL_FLAG(int32_t, log_level, GPR_LOG_SEVERITY_INFO,
96           "Severity level of messages that should be logged. Any messages "
97           "greater than or equal to the level set here will be logged. "
98           "The choices are: 0 (GPR_LOG_SEVERITY_DEBUG), 1 "
99           "(GPR_LOG_SEVERITY_INFO) and 2 (GPR_LOG_SEVERITY_ERROR)");
100 
101 ABSL_FLAG(bool, do_not_abort_on_transient_failures, true,
102           "If set to 'true', abort() is not called in case of transient "
103           "failures like temporary connection failures.");
104 
105 // Options from client.cc (for compatibility with interop test).
106 // TODO(sreek): Consolidate overlapping options
107 ABSL_FLAG(bool, use_alts, false,
108           "Whether to use alts. Enable alts will disable tls.");
109 ABSL_FLAG(bool, use_tls, false, "Whether to use tls.");
110 ABSL_FLAG(bool, use_test_ca, false, "False to use SSL roots for google");
111 ABSL_FLAG(std::string, server_host_override, "",
112           "Override the server host which is sent in HTTP header");
113 
114 using grpc::testing::ALTS;
115 using grpc::testing::INSECURE;
116 using grpc::testing::kTestCaseList;
117 using grpc::testing::MetricsServiceImpl;
118 using grpc::testing::StressTestInteropClient;
119 using grpc::testing::TestCaseType;
120 using grpc::testing::TLS;
121 using grpc::testing::transport_security;
122 using grpc::testing::UNKNOWN_TEST;
123 using grpc::testing::WeightedRandomTestSelector;
124 
125 static int log_level = GPR_LOG_SEVERITY_DEBUG;
126 
127 // A simple wrapper to grp_default_log() function. This only logs messages at or
128 // above the current log level (set in 'log_level' variable)
TestLogFunction(gpr_log_func_args * args)129 void TestLogFunction(gpr_log_func_args* args) {
130   if (args->severity >= log_level) {
131     gpr_default_log(args);
132   }
133 }
134 
GetTestTypeFromName(const std::string & test_name)135 TestCaseType GetTestTypeFromName(const std::string& test_name) {
136   TestCaseType test_case = UNKNOWN_TEST;
137 
138   for (auto it = kTestCaseList.begin(); it != kTestCaseList.end(); it++) {
139     if (test_name == it->second) {
140       test_case = it->first;
141       break;
142     }
143   }
144 
145   return test_case;
146 }
147 
148 // Converts a string of comma delimited tokens to a vector of tokens
ParseCommaDelimitedString(const std::string & comma_delimited_str,std::vector<std::string> & tokens)149 bool ParseCommaDelimitedString(const std::string& comma_delimited_str,
150                                std::vector<std::string>& tokens) {
151   size_t bpos = 0;
152   size_t epos = std::string::npos;
153 
154   while ((epos = comma_delimited_str.find(',', bpos)) != std::string::npos) {
155     tokens.emplace_back(comma_delimited_str.substr(bpos, epos - bpos));
156     bpos = epos + 1;
157   }
158 
159   tokens.emplace_back(comma_delimited_str.substr(bpos));  // Last token
160   return true;
161 }
162 
163 // Input: Test case string "<testcase_name:weight>,<testcase_name:weight>...."
164 // Output:
165 //   - Whether parsing was successful (return value)
166 //   - Vector of (test_type_enum, weight) pairs returned via 'tests' parameter
ParseTestCasesString(const std::string & test_cases,std::vector<std::pair<TestCaseType,int>> & tests)167 bool ParseTestCasesString(const std::string& test_cases,
168                           std::vector<std::pair<TestCaseType, int>>& tests) {
169   bool is_success = true;
170 
171   std::vector<std::string> tokens;
172   ParseCommaDelimitedString(test_cases, tokens);
173 
174   for (auto it = tokens.begin(); it != tokens.end(); it++) {
175     // Token is in the form <test_name>:<test_weight>
176     size_t colon_pos = it->find(':');
177     if (colon_pos == std::string::npos) {
178       gpr_log(GPR_ERROR, "Error in parsing test case string: %s", it->c_str());
179       is_success = false;
180       break;
181     }
182 
183     std::string test_name = it->substr(0, colon_pos);
184     int weight = std::stoi(it->substr(colon_pos + 1));
185     TestCaseType test_case = GetTestTypeFromName(test_name);
186     if (test_case == UNKNOWN_TEST) {
187       gpr_log(GPR_ERROR, "Unknown test case: %s", test_name.c_str());
188       is_success = false;
189       break;
190     }
191 
192     tests.emplace_back(std::make_pair(test_case, weight));
193   }
194 
195   return is_success;
196 }
197 
198 // For debugging purposes
LogParameterInfo(const std::vector<std::string> & addresses,const std::vector<std::pair<TestCaseType,int>> & tests)199 void LogParameterInfo(const std::vector<std::string>& addresses,
200                       const std::vector<std::pair<TestCaseType, int>>& tests) {
201   gpr_log(GPR_INFO, "server_addresses: %s",
202           absl::GetFlag(FLAGS_server_addresses).c_str());
203   gpr_log(GPR_INFO, "test_cases : %s", absl::GetFlag(FLAGS_test_cases).c_str());
204   gpr_log(GPR_INFO, "sleep_duration_ms: %d",
205           absl::GetFlag(FLAGS_sleep_duration_ms));
206   gpr_log(GPR_INFO, "test_duration_secs: %d",
207           absl::GetFlag(FLAGS_test_duration_secs));
208   gpr_log(GPR_INFO, "num_channels_per_server: %d",
209           absl::GetFlag(FLAGS_num_channels_per_server));
210   gpr_log(GPR_INFO, "num_stubs_per_channel: %d",
211           absl::GetFlag(FLAGS_num_stubs_per_channel));
212   gpr_log(GPR_INFO, "log_level: %d", absl::GetFlag(FLAGS_log_level));
213   gpr_log(GPR_INFO, "do_not_abort_on_transient_failures: %s",
214           absl::GetFlag(FLAGS_do_not_abort_on_transient_failures) ? "true"
215                                                                   : "false");
216 
217   int num = 0;
218   for (auto it = addresses.begin(); it != addresses.end(); it++) {
219     gpr_log(GPR_INFO, "%d:%s", ++num, it->c_str());
220   }
221 
222   num = 0;
223   for (auto it = tests.begin(); it != tests.end(); it++) {
224     TestCaseType test_case = it->first;
225     int weight = it->second;
226     gpr_log(GPR_INFO, "%d. TestCaseType: %d, Weight: %d", ++num, test_case,
227             weight);
228   }
229 }
230 
main(int argc,char ** argv)231 int main(int argc, char** argv) {
232   grpc::testing::InitTest(&argc, &argv, true);
233 
234   if (absl::GetFlag(FLAGS_log_level) > GPR_LOG_SEVERITY_ERROR ||
235       absl::GetFlag(FLAGS_log_level) < GPR_LOG_SEVERITY_DEBUG) {
236     gpr_log(GPR_ERROR, "log_level should be an integer between %d and %d",
237             GPR_LOG_SEVERITY_DEBUG, GPR_LOG_SEVERITY_ERROR);
238     return 1;
239   }
240 
241   // Change the default log function to TestLogFunction which respects the
242   // log_level setting.
243   log_level = absl::GetFlag(FLAGS_log_level);
244   gpr_set_log_function(TestLogFunction);
245 
246   srand(time(nullptr));
247 
248   // Parse the server addresses
249   std::vector<std::string> server_addresses;
250   ParseCommaDelimitedString(absl::GetFlag(FLAGS_server_addresses),
251                             server_addresses);
252 
253   // Parse test cases and weights
254   if (absl::GetFlag(FLAGS_test_cases).length() == 0) {
255     gpr_log(GPR_ERROR, "No test cases supplied");
256     return 1;
257   }
258 
259   std::vector<std::pair<TestCaseType, int>> tests;
260   if (!ParseTestCasesString(absl::GetFlag(FLAGS_test_cases), tests)) {
261     gpr_log(GPR_ERROR, "Error in parsing test cases string %s ",
262             absl::GetFlag(FLAGS_test_cases).c_str());
263     return 1;
264   }
265 
266   LogParameterInfo(server_addresses, tests);
267 
268   WeightedRandomTestSelector test_selector(tests);
269   MetricsServiceImpl metrics_service;
270 
271   gpr_log(GPR_INFO, "Starting test(s)..");
272 
273   std::vector<std::thread> test_threads;
274   std::vector<std::unique_ptr<StressTestInteropClient>> clients;
275 
276   // Create and start the test threads.
277   // Note that:
278   // - Each server can have multiple channels (as configured by
279   // FLAGS_num_channels_per_server).
280   //
281   // - Each channel can have multiple stubs (as configured by
282   // FLAGS_num_stubs_per_channel). This is to test calling multiple RPCs in
283   // parallel on the same channel.
284   int thread_idx = 0;
285   int server_idx = -1;
286   char buffer[256];
287   transport_security security_type =
288       absl::GetFlag(FLAGS_use_alts)
289           ? ALTS
290           : (absl::GetFlag(FLAGS_use_tls) ? TLS : INSECURE);
291   for (auto it = server_addresses.begin(); it != server_addresses.end(); it++) {
292     ++server_idx;
293     // Create channel(s) for each server
294     for (int channel_idx = 0;
295          channel_idx < absl::GetFlag(FLAGS_num_channels_per_server);
296          channel_idx++) {
297       gpr_log(GPR_INFO, "Starting test with %s channel_idx=%d..", it->c_str(),
298               channel_idx);
299       grpc::testing::ChannelCreationFunc channel_creation_func =
300           std::bind(static_cast<std::shared_ptr<grpc::Channel> (*)(
301                         const std::string&, const std::string&,
302                         grpc::testing::transport_security, bool)>(
303                         grpc::CreateTestChannel),
304                     *it, absl::GetFlag(FLAGS_server_host_override),
305                     security_type, !absl::GetFlag(FLAGS_use_test_ca));
306 
307       // Create stub(s) for each channel
308       for (int stub_idx = 0;
309            stub_idx < absl::GetFlag(FLAGS_num_stubs_per_channel); stub_idx++) {
310         clients.emplace_back(new StressTestInteropClient(
311             ++thread_idx, *it, channel_creation_func, test_selector,
312             absl::GetFlag(FLAGS_test_duration_secs),
313             absl::GetFlag(FLAGS_sleep_duration_ms),
314             absl::GetFlag(FLAGS_do_not_abort_on_transient_failures)));
315 
316         bool is_already_created = false;
317         // QpsGauge name
318         std::snprintf(buffer, sizeof(buffer),
319                       "/stress_test/server_%d/channel_%d/stub_%d/qps",
320                       server_idx, channel_idx, stub_idx);
321 
322         test_threads.emplace_back(std::thread(
323             &StressTestInteropClient::MainLoop, clients.back().get(),
324             metrics_service.CreateQpsGauge(buffer, &is_already_created)));
325 
326         // The QpsGauge should not have been already created
327         GPR_ASSERT(!is_already_created);
328       }
329     }
330   }
331 
332   // Start metrics server before waiting for the stress test threads
333   std::unique_ptr<grpc::Server> metrics_server;
334   if (absl::GetFlag(FLAGS_metrics_port) > 0) {
335     metrics_server =
336         metrics_service.StartServer(absl::GetFlag(FLAGS_metrics_port));
337   }
338 
339   // Wait for the stress test threads to complete
340   for (auto it = test_threads.begin(); it != test_threads.end(); it++) {
341     it->join();
342   }
343 
344   return 0;
345 }
346