blob: 77879319003f281692ef05a56fb9efbcc174e820 [file] [log] [blame]
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -07004 * 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 *is % allowed in string
32 */
33
34#include <memory>
35#include <string>
36#include <thread>
37#include <utility>
38#include <vector>
39
40#include <gflags/gflags.h>
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -070041#include <grpc++/create_channel.h>
42#include <grpc++/grpc++.h>
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -080043#include <grpc++/impl/thd.h>
Sree Kuchibhotlae1330ff2015-11-20 09:53:44 -080044#include <grpc/support/time.h>
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -070045
Craig Tillerf40df232016-03-25 13:38:14 -070046#include "src/proto/grpc/testing/metrics.grpc.pb.h"
47#include "src/proto/grpc/testing/metrics.pb.h"
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -070048#include "test/cpp/interop/interop_client.h"
49#include "test/cpp/interop/stress_interop_client.h"
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070050#include "test/cpp/util/metrics_server.h"
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -070051#include "test/cpp/util/test_config.h"
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070052
Sree Kuchibhotla0fa95ea2016-01-06 07:58:10 -080053extern "C" {
54extern void gpr_default_log(gpr_log_func_args* args);
55}
56
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070057DEFINE_int32(metrics_port, 8081, "The metrics server port.");
58
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -070059DEFINE_int32(sleep_duration_ms, 0,
60 "The duration (in millisec) between two"
61 " consecutive test calls (per server) issued by the server.");
62
63DEFINE_int32(test_duration_secs, -1,
64 "The length of time (in seconds) to run"
65 " the test. Enter -1 if the test should run continuously until"
66 " forcefully terminated.");
67
68DEFINE_string(server_addresses, "localhost:8080",
69 "The list of server"
70 " addresses in the format:\n"
71 " \"<name_1>:<port_1>,<name_2>:<port_1>...<name_N>:<port_N>\"\n"
72 " Note: <name> can be servername or IP address.");
73
Sree Kuchibhotlae1330ff2015-11-20 09:53:44 -080074DEFINE_int32(num_channels_per_server, 1, "Number of channels for each server");
75
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070076DEFINE_int32(num_stubs_per_channel, 1,
77 "Number of stubs per each channels to server. This number also "
78 "indicates the max number of parallel RPC calls on each channel "
79 "at any given time.");
80
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -070081// TODO(sreek): Add more test cases here in future
82DEFINE_string(test_cases, "",
83 "List of test cases to call along with the"
84 " relative weights in the following format:\n"
85 " \"<testcase_1:w_1>,<testcase_2:w_2>...<testcase_n:w_n>\"\n"
86 " The following testcases are currently supported:\n"
87 " empty_unary\n"
88 " large_unary\n"
89 " large_compressed_unary\n"
90 " client_streaming\n"
91 " server_streaming\n"
Sree Kuchibhotla51304272016-05-05 15:19:00 -070092 " server_compressed_streaming\n"
93 " slow_consumer\n"
94 " half_duplex\n"
95 " ping_pong\n"
96 " cancel_after_begin\n"
97 " cancel_after_first_response\n"
98 " timeout_on_sleeping_server\n"
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -070099 " empty_stream\n"
Sree Kuchibhotla51304272016-05-05 15:19:00 -0700100 " status_code_and_message\n"
101 " custom_metadata\n"
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700102 " Example: \"empty_unary:20,large_unary:10,empty_stream:70\"\n"
103 " The above will execute 'empty_unary', 20% of the time,"
104 " 'large_unary', 10% of the time and 'empty_stream' the remaining"
105 " 70% of the time");
106
Sree Kuchibhotla16dd3e42016-03-31 09:46:20 -0700107DEFINE_int32(log_level, GPR_LOG_SEVERITY_INFO,
Sree Kuchibhotla0fa95ea2016-01-06 07:58:10 -0800108 "Severity level of messages that should be logged. Any messages "
109 "greater than or equal to the level set here will be logged. "
110 "The choices are: 0 (GPR_LOG_SEVERITY_DEBUG), 1 "
Sree Kuchibhotla16dd3e42016-03-31 09:46:20 -0700111 "(GPR_LOG_SEVERITY_INFO) and 2 (GPR_LOG_SEVERITY_ERROR)");
Sree Kuchibhotla0fa95ea2016-01-06 07:58:10 -0800112
Sree Kuchibhotlaad0f7922016-05-04 19:49:31 -0700113DEFINE_bool(do_not_abort_on_transient_failures, true,
114 "If set to 'true', abort() is not called in case of transient "
115 "failures like temporary connection failures.");
116
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700117using grpc::testing::kTestCaseList;
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700118using grpc::testing::MetricsService;
119using grpc::testing::MetricsServiceImpl;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700120using grpc::testing::StressTestInteropClient;
121using grpc::testing::TestCaseType;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700122using grpc::testing::UNKNOWN_TEST;
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700123using grpc::testing::WeightedRandomTestSelector;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700124
Sree Kuchibhotla0fa95ea2016-01-06 07:58:10 -0800125static 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)
129void TestLogFunction(gpr_log_func_args* args) {
130 if (args->severity >= log_level) {
131 gpr_default_log(args);
132 }
133}
134
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700135TestCaseType GetTestTypeFromName(const grpc::string& test_name) {
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700136 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
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700149bool ParseCommaDelimitedString(const grpc::string& comma_delimited_str,
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -0800150 std::vector<grpc::string>& tokens) {
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700151 size_t bpos = 0;
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700152 size_t epos = grpc::string::npos;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700153
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700154 while ((epos = comma_delimited_str.find(',', bpos)) != grpc::string::npos) {
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700155 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
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700167bool ParseTestCasesString(const grpc::string& test_cases,
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -0800168 std::vector<std::pair<TestCaseType, int>>& tests) {
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700169 bool is_success = true;
170
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -0800171 std::vector<grpc::string> tokens;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700172 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(':');
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700177 if (colon_pos == grpc::string::npos) {
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700178 gpr_log(GPR_ERROR, "Error in parsing test case string: %s", it->c_str());
179 is_success = false;
180 break;
181 }
182
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700183 grpc::string test_name = it->substr(0, colon_pos);
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700184 int weight = std::stoi(it->substr(colon_pos + 1));
185 TestCaseType test_case = GetTestTypeFromName(test_name);
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700186 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
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -0800199void LogParameterInfo(const std::vector<grpc::string>& addresses,
200 const std::vector<std::pair<TestCaseType, int>>& tests) {
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700201 gpr_log(GPR_INFO, "server_addresses: %s", FLAGS_server_addresses.c_str());
202 gpr_log(GPR_INFO, "test_cases : %s", FLAGS_test_cases.c_str());
203 gpr_log(GPR_INFO, "sleep_duration_ms: %d", FLAGS_sleep_duration_ms);
204 gpr_log(GPR_INFO, "test_duration_secs: %d", FLAGS_test_duration_secs);
Sree Kuchibhotlaad0f7922016-05-04 19:49:31 -0700205 gpr_log(GPR_INFO, "num_channels_per_server: %d",
206 FLAGS_num_channels_per_server);
207 gpr_log(GPR_INFO, "num_stubs_per_channel: %d", FLAGS_num_stubs_per_channel);
208 gpr_log(GPR_INFO, "log_level: %d", FLAGS_log_level);
209 gpr_log(GPR_INFO, "do_not_abort_on_transient_failures: %s",
210 FLAGS_do_not_abort_on_transient_failures ? "true" : "false");
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700211
212 int num = 0;
213 for (auto it = addresses.begin(); it != addresses.end(); it++) {
214 gpr_log(GPR_INFO, "%d:%s", ++num, it->c_str());
215 }
216
217 num = 0;
218 for (auto it = tests.begin(); it != tests.end(); it++) {
219 TestCaseType test_case = it->first;
220 int weight = it->second;
221 gpr_log(GPR_INFO, "%d. TestCaseType: %d, Weight: %d", ++num, test_case,
222 weight);
223 }
224}
225
226int main(int argc, char** argv) {
227 grpc::testing::InitTest(&argc, &argv, true);
228
Sree Kuchibhotla0fa95ea2016-01-06 07:58:10 -0800229 if (FLAGS_log_level > GPR_LOG_SEVERITY_ERROR ||
230 FLAGS_log_level < GPR_LOG_SEVERITY_DEBUG) {
231 gpr_log(GPR_ERROR, "log_level should be an integer between %d and %d",
232 GPR_LOG_SEVERITY_DEBUG, GPR_LOG_SEVERITY_ERROR);
233 return 1;
234 }
235
236 // Change the default log function to TestLogFunction which respects the
237 // log_level setting.
238 log_level = FLAGS_log_level;
239 gpr_set_log_function(TestLogFunction);
240
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700241 srand(time(NULL));
242
243 // Parse the server addresses
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -0800244 std::vector<grpc::string> server_addresses;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700245 ParseCommaDelimitedString(FLAGS_server_addresses, server_addresses);
246
247 // Parse test cases and weights
248 if (FLAGS_test_cases.length() == 0) {
Sree Kuchibhotla0fa95ea2016-01-06 07:58:10 -0800249 gpr_log(GPR_ERROR, "Not running tests. The 'test_cases' string is empty");
Sree Kuchibhotla117c8af2015-10-26 10:59:17 -0700250 return 1;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700251 }
252
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -0800253 std::vector<std::pair<TestCaseType, int>> tests;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700254 if (!ParseTestCasesString(FLAGS_test_cases, tests)) {
255 gpr_log(GPR_ERROR, "Error in parsing test cases string %s ",
256 FLAGS_test_cases.c_str());
257 return 1;
258 }
259
260 LogParameterInfo(server_addresses, tests);
261
262 WeightedRandomTestSelector test_selector(tests);
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700263 MetricsServiceImpl metrics_service;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700264
265 gpr_log(GPR_INFO, "Starting test(s)..");
Sree Kuchibhotla728a6102015-10-16 10:56:31 -0700266
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -0800267 std::vector<grpc::thread> test_threads;
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700268
Sree Kuchibhotlae1330ff2015-11-20 09:53:44 -0800269 // Create and start the test threads.
270 // Note that:
271 // - Each server can have multiple channels (as configured by
272 // FLAGS_num_channels_per_server).
273 //
274 // - Each channel can have multiple stubs (as configured by
275 // FLAGS_num_stubs_per_channel). This is to test calling multiple RPCs in
276 // parallel on the same channel.
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700277 int thread_idx = 0;
Sree Kuchibhotlae1330ff2015-11-20 09:53:44 -0800278 int server_idx = -1;
279 char buffer[256];
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700280 for (auto it = server_addresses.begin(); it != server_addresses.end(); it++) {
Sree Kuchibhotlae1330ff2015-11-20 09:53:44 -0800281 ++server_idx;
282 // Create channel(s) for each server
283 for (int channel_idx = 0; channel_idx < FLAGS_num_channels_per_server;
284 channel_idx++) {
285 // TODO (sreek). This won't work for tests that require Authentication
286 std::shared_ptr<grpc::Channel> channel(
287 grpc::CreateChannel(*it, grpc::InsecureChannelCredentials()));
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700288
Sree Kuchibhotlae1330ff2015-11-20 09:53:44 -0800289 // Create stub(s) for each channel
290 for (int stub_idx = 0; stub_idx < FLAGS_num_stubs_per_channel;
291 stub_idx++) {
292 StressTestInteropClient* client = new StressTestInteropClient(
293 ++thread_idx, *it, channel, test_selector, FLAGS_test_duration_secs,
Sree Kuchibhotlaad0f7922016-05-04 19:49:31 -0700294 FLAGS_sleep_duration_ms, FLAGS_do_not_abort_on_transient_failures);
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700295
Sree Kuchibhotla3714e302016-04-22 09:50:46 -0700296 bool is_already_created = false;
297 // QpsGauge name
Sree Kuchibhotlae1330ff2015-11-20 09:53:44 -0800298 std::snprintf(buffer, sizeof(buffer),
299 "/stress_test/server_%d/channel_%d/stub_%d/qps",
300 server_idx, channel_idx, stub_idx);
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700301
Sree Kuchibhotlae1330ff2015-11-20 09:53:44 -0800302 test_threads.emplace_back(grpc::thread(
303 &StressTestInteropClient::MainLoop, client,
Sree Kuchibhotla3714e302016-04-22 09:50:46 -0700304 metrics_service.CreateQpsGauge(buffer, &is_already_created)));
Sree Kuchibhotlae1330ff2015-11-20 09:53:44 -0800305
Sree Kuchibhotla3714e302016-04-22 09:50:46 -0700306 // The QpsGauge should not have been already created
Sree Kuchibhotlae1330ff2015-11-20 09:53:44 -0800307 GPR_ASSERT(!is_already_created);
308 }
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700309 }
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700310 }
311
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700312 // Start metrics server before waiting for the stress test threads
313 std::unique_ptr<grpc::Server> metrics_server =
314 metrics_service.StartServer(FLAGS_metrics_port);
315
316 // Wait for the stress test threads to complete
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700317 for (auto it = test_threads.begin(); it != test_threads.end(); it++) {
318 it->join();
319 }
320
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700321 return 0;
322}