Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * 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 Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 41 | #include <grpc++/create_channel.h> |
| 42 | #include <grpc++/grpc++.h> |
Sree Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 43 | #include <grpc++/impl/thd.h> |
Sree Kuchibhotla | e1330ff | 2015-11-20 09:53:44 -0800 | [diff] [blame] | 44 | #include <grpc/support/time.h> |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 45 | |
| 46 | #include "test/cpp/interop/interop_client.h" |
| 47 | #include "test/cpp/interop/stress_interop_client.h" |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 48 | #include "test/cpp/util/metrics_server.h" |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 49 | #include "test/cpp/util/test_config.h" |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 50 | #include "test/proto/metrics.grpc.pb.h" |
| 51 | #include "test/proto/metrics.pb.h" |
| 52 | |
Sree Kuchibhotla | 0fa95ea | 2016-01-06 07:58:10 -0800 | [diff] [blame^] | 53 | extern "C" { |
| 54 | extern void gpr_default_log(gpr_log_func_args* args); |
| 55 | } |
| 56 | |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 57 | DEFINE_int32(metrics_port, 8081, "The metrics server port."); |
| 58 | |
| 59 | DEFINE_int32(metrics_collection_interval_secs, 5, |
| 60 | "How often (in seconds) should metrics be recorded."); |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 61 | |
| 62 | DEFINE_int32(sleep_duration_ms, 0, |
| 63 | "The duration (in millisec) between two" |
| 64 | " consecutive test calls (per server) issued by the server."); |
| 65 | |
| 66 | DEFINE_int32(test_duration_secs, -1, |
| 67 | "The length of time (in seconds) to run" |
| 68 | " the test. Enter -1 if the test should run continuously until" |
| 69 | " forcefully terminated."); |
| 70 | |
| 71 | DEFINE_string(server_addresses, "localhost:8080", |
| 72 | "The list of server" |
| 73 | " addresses in the format:\n" |
| 74 | " \"<name_1>:<port_1>,<name_2>:<port_1>...<name_N>:<port_N>\"\n" |
| 75 | " Note: <name> can be servername or IP address."); |
| 76 | |
Sree Kuchibhotla | e1330ff | 2015-11-20 09:53:44 -0800 | [diff] [blame] | 77 | DEFINE_int32(num_channels_per_server, 1, "Number of channels for each server"); |
| 78 | |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 79 | DEFINE_int32(num_stubs_per_channel, 1, |
| 80 | "Number of stubs per each channels to server. This number also " |
| 81 | "indicates the max number of parallel RPC calls on each channel " |
| 82 | "at any given time."); |
| 83 | |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 84 | // TODO(sreek): Add more test cases here in future |
| 85 | DEFINE_string(test_cases, "", |
| 86 | "List of test cases to call along with the" |
| 87 | " relative weights in the following format:\n" |
| 88 | " \"<testcase_1:w_1>,<testcase_2:w_2>...<testcase_n:w_n>\"\n" |
| 89 | " The following testcases are currently supported:\n" |
| 90 | " empty_unary\n" |
| 91 | " large_unary\n" |
| 92 | " large_compressed_unary\n" |
| 93 | " client_streaming\n" |
| 94 | " server_streaming\n" |
| 95 | " empty_stream\n" |
| 96 | " Example: \"empty_unary:20,large_unary:10,empty_stream:70\"\n" |
| 97 | " The above will execute 'empty_unary', 20% of the time," |
| 98 | " 'large_unary', 10% of the time and 'empty_stream' the remaining" |
| 99 | " 70% of the time"); |
| 100 | |
Sree Kuchibhotla | 0fa95ea | 2016-01-06 07:58:10 -0800 | [diff] [blame^] | 101 | DEFINE_int32(log_level, GPR_LOG_SEVERITY_DEBUG, |
| 102 | "Severity level of messages that should be logged. Any messages " |
| 103 | "greater than or equal to the level set here will be logged. " |
| 104 | "The choices are: 0 (GPR_LOG_SEVERITY_DEBUG), 1 " |
| 105 | "(GPR_LOG_SEVERITY_INFO) and 2 (GPR_LOG_SEVERITY_ERROR."); |
| 106 | |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 107 | using grpc::testing::kTestCaseList; |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 108 | using grpc::testing::MetricsService; |
| 109 | using grpc::testing::MetricsServiceImpl; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 110 | using grpc::testing::StressTestInteropClient; |
| 111 | using grpc::testing::TestCaseType; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 112 | using grpc::testing::UNKNOWN_TEST; |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 113 | using grpc::testing::WeightedRandomTestSelector; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 114 | |
Sree Kuchibhotla | 0fa95ea | 2016-01-06 07:58:10 -0800 | [diff] [blame^] | 115 | static int log_level = GPR_LOG_SEVERITY_DEBUG; |
| 116 | |
| 117 | // A simple wrapper to grp_default_log() function. This only logs messages at or |
| 118 | // above the current log level (set in 'log_level' variable) |
| 119 | void TestLogFunction(gpr_log_func_args* args) { |
| 120 | if (args->severity >= log_level) { |
| 121 | gpr_default_log(args); |
| 122 | } |
| 123 | } |
| 124 | |
Sree Kuchibhotla | f51ea7a | 2015-10-26 13:34:41 -0700 | [diff] [blame] | 125 | TestCaseType GetTestTypeFromName(const grpc::string& test_name) { |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 126 | TestCaseType test_case = UNKNOWN_TEST; |
| 127 | |
| 128 | for (auto it = kTestCaseList.begin(); it != kTestCaseList.end(); it++) { |
| 129 | if (test_name == it->second) { |
| 130 | test_case = it->first; |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | return test_case; |
| 136 | } |
| 137 | |
| 138 | // Converts a string of comma delimited tokens to a vector of tokens |
Sree Kuchibhotla | f51ea7a | 2015-10-26 13:34:41 -0700 | [diff] [blame] | 139 | bool ParseCommaDelimitedString(const grpc::string& comma_delimited_str, |
Sree Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 140 | std::vector<grpc::string>& tokens) { |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 141 | size_t bpos = 0; |
Sree Kuchibhotla | f51ea7a | 2015-10-26 13:34:41 -0700 | [diff] [blame] | 142 | size_t epos = grpc::string::npos; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 143 | |
Sree Kuchibhotla | f51ea7a | 2015-10-26 13:34:41 -0700 | [diff] [blame] | 144 | while ((epos = comma_delimited_str.find(',', bpos)) != grpc::string::npos) { |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 145 | tokens.emplace_back(comma_delimited_str.substr(bpos, epos - bpos)); |
| 146 | bpos = epos + 1; |
| 147 | } |
| 148 | |
| 149 | tokens.emplace_back(comma_delimited_str.substr(bpos)); // Last token |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | // Input: Test case string "<testcase_name:weight>,<testcase_name:weight>...." |
| 154 | // Output: |
| 155 | // - Whether parsing was successful (return value) |
| 156 | // - Vector of (test_type_enum, weight) pairs returned via 'tests' parameter |
Sree Kuchibhotla | f51ea7a | 2015-10-26 13:34:41 -0700 | [diff] [blame] | 157 | bool ParseTestCasesString(const grpc::string& test_cases, |
Sree Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 158 | std::vector<std::pair<TestCaseType, int>>& tests) { |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 159 | bool is_success = true; |
| 160 | |
Sree Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 161 | std::vector<grpc::string> tokens; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 162 | ParseCommaDelimitedString(test_cases, tokens); |
| 163 | |
| 164 | for (auto it = tokens.begin(); it != tokens.end(); it++) { |
| 165 | // Token is in the form <test_name>:<test_weight> |
| 166 | size_t colon_pos = it->find(':'); |
Sree Kuchibhotla | f51ea7a | 2015-10-26 13:34:41 -0700 | [diff] [blame] | 167 | if (colon_pos == grpc::string::npos) { |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 168 | gpr_log(GPR_ERROR, "Error in parsing test case string: %s", it->c_str()); |
| 169 | is_success = false; |
| 170 | break; |
| 171 | } |
| 172 | |
Sree Kuchibhotla | f51ea7a | 2015-10-26 13:34:41 -0700 | [diff] [blame] | 173 | grpc::string test_name = it->substr(0, colon_pos); |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 174 | int weight = std::stoi(it->substr(colon_pos + 1)); |
| 175 | TestCaseType test_case = GetTestTypeFromName(test_name); |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 176 | if (test_case == UNKNOWN_TEST) { |
| 177 | gpr_log(GPR_ERROR, "Unknown test case: %s", test_name.c_str()); |
| 178 | is_success = false; |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | tests.emplace_back(std::make_pair(test_case, weight)); |
| 183 | } |
| 184 | |
| 185 | return is_success; |
| 186 | } |
| 187 | |
| 188 | // For debugging purposes |
Sree Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 189 | void LogParameterInfo(const std::vector<grpc::string>& addresses, |
| 190 | const std::vector<std::pair<TestCaseType, int>>& tests) { |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 191 | gpr_log(GPR_INFO, "server_addresses: %s", FLAGS_server_addresses.c_str()); |
| 192 | gpr_log(GPR_INFO, "test_cases : %s", FLAGS_test_cases.c_str()); |
| 193 | gpr_log(GPR_INFO, "sleep_duration_ms: %d", FLAGS_sleep_duration_ms); |
| 194 | gpr_log(GPR_INFO, "test_duration_secs: %d", FLAGS_test_duration_secs); |
| 195 | |
| 196 | int num = 0; |
| 197 | for (auto it = addresses.begin(); it != addresses.end(); it++) { |
| 198 | gpr_log(GPR_INFO, "%d:%s", ++num, it->c_str()); |
| 199 | } |
| 200 | |
| 201 | num = 0; |
| 202 | for (auto it = tests.begin(); it != tests.end(); it++) { |
| 203 | TestCaseType test_case = it->first; |
| 204 | int weight = it->second; |
| 205 | gpr_log(GPR_INFO, "%d. TestCaseType: %d, Weight: %d", ++num, test_case, |
| 206 | weight); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | int main(int argc, char** argv) { |
| 211 | grpc::testing::InitTest(&argc, &argv, true); |
| 212 | |
Sree Kuchibhotla | 0fa95ea | 2016-01-06 07:58:10 -0800 | [diff] [blame^] | 213 | if (FLAGS_log_level > GPR_LOG_SEVERITY_ERROR || |
| 214 | FLAGS_log_level < GPR_LOG_SEVERITY_DEBUG) { |
| 215 | gpr_log(GPR_ERROR, "log_level should be an integer between %d and %d", |
| 216 | GPR_LOG_SEVERITY_DEBUG, GPR_LOG_SEVERITY_ERROR); |
| 217 | return 1; |
| 218 | } |
| 219 | |
| 220 | // Change the default log function to TestLogFunction which respects the |
| 221 | // log_level setting. |
| 222 | log_level = FLAGS_log_level; |
| 223 | gpr_set_log_function(TestLogFunction); |
| 224 | |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 225 | srand(time(NULL)); |
| 226 | |
| 227 | // Parse the server addresses |
Sree Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 228 | std::vector<grpc::string> server_addresses; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 229 | ParseCommaDelimitedString(FLAGS_server_addresses, server_addresses); |
| 230 | |
| 231 | // Parse test cases and weights |
| 232 | if (FLAGS_test_cases.length() == 0) { |
Sree Kuchibhotla | 0fa95ea | 2016-01-06 07:58:10 -0800 | [diff] [blame^] | 233 | gpr_log(GPR_ERROR, "Not running tests. The 'test_cases' string is empty"); |
Sree Kuchibhotla | 117c8af | 2015-10-26 10:59:17 -0700 | [diff] [blame] | 234 | return 1; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Sree Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 237 | std::vector<std::pair<TestCaseType, int>> tests; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 238 | if (!ParseTestCasesString(FLAGS_test_cases, tests)) { |
| 239 | gpr_log(GPR_ERROR, "Error in parsing test cases string %s ", |
| 240 | FLAGS_test_cases.c_str()); |
| 241 | return 1; |
| 242 | } |
| 243 | |
| 244 | LogParameterInfo(server_addresses, tests); |
| 245 | |
| 246 | WeightedRandomTestSelector test_selector(tests); |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 247 | MetricsServiceImpl metrics_service; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 248 | |
| 249 | gpr_log(GPR_INFO, "Starting test(s).."); |
Sree Kuchibhotla | 728a610 | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 250 | |
Sree Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 251 | std::vector<grpc::thread> test_threads; |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 252 | |
Sree Kuchibhotla | e1330ff | 2015-11-20 09:53:44 -0800 | [diff] [blame] | 253 | // Create and start the test threads. |
| 254 | // Note that: |
| 255 | // - Each server can have multiple channels (as configured by |
| 256 | // FLAGS_num_channels_per_server). |
| 257 | // |
| 258 | // - Each channel can have multiple stubs (as configured by |
| 259 | // FLAGS_num_stubs_per_channel). This is to test calling multiple RPCs in |
| 260 | // parallel on the same channel. |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 261 | int thread_idx = 0; |
Sree Kuchibhotla | e1330ff | 2015-11-20 09:53:44 -0800 | [diff] [blame] | 262 | int server_idx = -1; |
| 263 | char buffer[256]; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 264 | for (auto it = server_addresses.begin(); it != server_addresses.end(); it++) { |
Sree Kuchibhotla | e1330ff | 2015-11-20 09:53:44 -0800 | [diff] [blame] | 265 | ++server_idx; |
| 266 | // Create channel(s) for each server |
| 267 | for (int channel_idx = 0; channel_idx < FLAGS_num_channels_per_server; |
| 268 | channel_idx++) { |
| 269 | // TODO (sreek). This won't work for tests that require Authentication |
| 270 | std::shared_ptr<grpc::Channel> channel( |
| 271 | grpc::CreateChannel(*it, grpc::InsecureChannelCredentials())); |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 272 | |
Sree Kuchibhotla | e1330ff | 2015-11-20 09:53:44 -0800 | [diff] [blame] | 273 | // Create stub(s) for each channel |
| 274 | for (int stub_idx = 0; stub_idx < FLAGS_num_stubs_per_channel; |
| 275 | stub_idx++) { |
| 276 | StressTestInteropClient* client = new StressTestInteropClient( |
| 277 | ++thread_idx, *it, channel, test_selector, FLAGS_test_duration_secs, |
| 278 | FLAGS_sleep_duration_ms, FLAGS_metrics_collection_interval_secs); |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 279 | |
Sree Kuchibhotla | e1330ff | 2015-11-20 09:53:44 -0800 | [diff] [blame] | 280 | bool is_already_created; |
| 281 | // Gauge name |
| 282 | std::snprintf(buffer, sizeof(buffer), |
| 283 | "/stress_test/server_%d/channel_%d/stub_%d/qps", |
| 284 | server_idx, channel_idx, stub_idx); |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 285 | |
Sree Kuchibhotla | e1330ff | 2015-11-20 09:53:44 -0800 | [diff] [blame] | 286 | test_threads.emplace_back(grpc::thread( |
| 287 | &StressTestInteropClient::MainLoop, client, |
| 288 | metrics_service.CreateGauge(buffer, &is_already_created))); |
| 289 | |
| 290 | // The Gauge should not have been already created |
| 291 | GPR_ASSERT(!is_already_created); |
| 292 | } |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 293 | } |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 296 | // Start metrics server before waiting for the stress test threads |
| 297 | std::unique_ptr<grpc::Server> metrics_server = |
| 298 | metrics_service.StartServer(FLAGS_metrics_port); |
| 299 | |
| 300 | // Wait for the stress test threads to complete |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 301 | for (auto it = test_threads.begin(); it != test_threads.end(); it++) { |
| 302 | it->join(); |
| 303 | } |
| 304 | |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 305 | return 0; |
| 306 | } |