stress_test.cc 13 KB

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