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> |
| 41 | #include <grpc/support/time.h> |
| 42 | #include <grpc++/create_channel.h> |
| 43 | #include <grpc++/grpc++.h> |
| 44 | |
| 45 | #include "test/cpp/interop/interop_client.h" |
| 46 | #include "test/cpp/interop/stress_interop_client.h" |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame^] | 47 | #include "test/cpp/util/metrics_server.h" |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 48 | #include "test/cpp/util/test_config.h" |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame^] | 49 | #include "test/proto/metrics.grpc.pb.h" |
| 50 | #include "test/proto/metrics.pb.h" |
| 51 | |
| 52 | DEFINE_int32(metrics_port, 8081, "The metrics server port."); |
| 53 | |
| 54 | DEFINE_int32(metrics_collection_interval_secs, 5, |
| 55 | "How often (in seconds) should metrics be recorded."); |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 56 | |
| 57 | DEFINE_int32(sleep_duration_ms, 0, |
| 58 | "The duration (in millisec) between two" |
| 59 | " consecutive test calls (per server) issued by the server."); |
| 60 | |
| 61 | DEFINE_int32(test_duration_secs, -1, |
| 62 | "The length of time (in seconds) to run" |
| 63 | " the test. Enter -1 if the test should run continuously until" |
| 64 | " forcefully terminated."); |
| 65 | |
| 66 | DEFINE_string(server_addresses, "localhost:8080", |
| 67 | "The list of server" |
| 68 | " addresses in the format:\n" |
| 69 | " \"<name_1>:<port_1>,<name_2>:<port_1>...<name_N>:<port_N>\"\n" |
| 70 | " Note: <name> can be servername or IP address."); |
| 71 | |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame^] | 72 | DEFINE_int32(num_stubs_per_channel, 1, |
| 73 | "Number of stubs per each channels to server. This number also " |
| 74 | "indicates the max number of parallel RPC calls on each channel " |
| 75 | "at any given time."); |
| 76 | |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 77 | // TODO(sreek): Add more test cases here in future |
| 78 | DEFINE_string(test_cases, "", |
| 79 | "List of test cases to call along with the" |
| 80 | " relative weights in the following format:\n" |
| 81 | " \"<testcase_1:w_1>,<testcase_2:w_2>...<testcase_n:w_n>\"\n" |
| 82 | " The following testcases are currently supported:\n" |
| 83 | " empty_unary\n" |
| 84 | " large_unary\n" |
| 85 | " large_compressed_unary\n" |
| 86 | " client_streaming\n" |
| 87 | " server_streaming\n" |
| 88 | " empty_stream\n" |
| 89 | " Example: \"empty_unary:20,large_unary:10,empty_stream:70\"\n" |
| 90 | " The above will execute 'empty_unary', 20% of the time," |
| 91 | " 'large_unary', 10% of the time and 'empty_stream' the remaining" |
| 92 | " 70% of the time"); |
| 93 | |
| 94 | using std::make_pair; |
| 95 | using std::pair; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 96 | using std::thread; |
| 97 | using std::vector; |
| 98 | |
| 99 | using grpc::testing::kTestCaseList; |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame^] | 100 | using grpc::testing::MetricsService; |
| 101 | using grpc::testing::MetricsServiceImpl; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 102 | using grpc::testing::StressTestInteropClient; |
| 103 | using grpc::testing::TestCaseType; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 104 | using grpc::testing::UNKNOWN_TEST; |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame^] | 105 | using grpc::testing::WeightedRandomTestSelector; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 106 | |
Sree Kuchibhotla | f51ea7a | 2015-10-26 13:34:41 -0700 | [diff] [blame] | 107 | TestCaseType GetTestTypeFromName(const grpc::string& test_name) { |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 108 | TestCaseType test_case = UNKNOWN_TEST; |
| 109 | |
| 110 | for (auto it = kTestCaseList.begin(); it != kTestCaseList.end(); it++) { |
| 111 | if (test_name == it->second) { |
| 112 | test_case = it->first; |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return test_case; |
| 118 | } |
| 119 | |
| 120 | // Converts a string of comma delimited tokens to a vector of tokens |
Sree Kuchibhotla | f51ea7a | 2015-10-26 13:34:41 -0700 | [diff] [blame] | 121 | bool ParseCommaDelimitedString(const grpc::string& comma_delimited_str, |
| 122 | vector<grpc::string>& tokens) { |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 123 | size_t bpos = 0; |
Sree Kuchibhotla | f51ea7a | 2015-10-26 13:34:41 -0700 | [diff] [blame] | 124 | size_t epos = grpc::string::npos; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 125 | |
Sree Kuchibhotla | f51ea7a | 2015-10-26 13:34:41 -0700 | [diff] [blame] | 126 | while ((epos = comma_delimited_str.find(',', bpos)) != grpc::string::npos) { |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 127 | tokens.emplace_back(comma_delimited_str.substr(bpos, epos - bpos)); |
| 128 | bpos = epos + 1; |
| 129 | } |
| 130 | |
| 131 | tokens.emplace_back(comma_delimited_str.substr(bpos)); // Last token |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | // Input: Test case string "<testcase_name:weight>,<testcase_name:weight>...." |
| 136 | // Output: |
| 137 | // - Whether parsing was successful (return value) |
| 138 | // - Vector of (test_type_enum, weight) pairs returned via 'tests' parameter |
Sree Kuchibhotla | f51ea7a | 2015-10-26 13:34:41 -0700 | [diff] [blame] | 139 | bool ParseTestCasesString(const grpc::string& test_cases, |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 140 | vector<pair<TestCaseType, int>>& tests) { |
| 141 | bool is_success = true; |
| 142 | |
Sree Kuchibhotla | f51ea7a | 2015-10-26 13:34:41 -0700 | [diff] [blame] | 143 | vector<grpc::string> tokens; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 144 | ParseCommaDelimitedString(test_cases, tokens); |
| 145 | |
| 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(':'); |
Sree Kuchibhotla | f51ea7a | 2015-10-26 13:34:41 -0700 | [diff] [blame] | 149 | if (colon_pos == grpc::string::npos) { |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 150 | gpr_log(GPR_ERROR, "Error in parsing test case string: %s", it->c_str()); |
| 151 | is_success = false; |
| 152 | break; |
| 153 | } |
| 154 | |
Sree Kuchibhotla | f51ea7a | 2015-10-26 13:34:41 -0700 | [diff] [blame] | 155 | grpc::string test_name = it->substr(0, colon_pos); |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 156 | int weight = std::stoi(it->substr(colon_pos + 1)); |
| 157 | TestCaseType test_case = GetTestTypeFromName(test_name); |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 158 | if (test_case == UNKNOWN_TEST) { |
| 159 | gpr_log(GPR_ERROR, "Unknown test case: %s", test_name.c_str()); |
| 160 | is_success = false; |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | tests.emplace_back(std::make_pair(test_case, weight)); |
| 165 | } |
| 166 | |
| 167 | return is_success; |
| 168 | } |
| 169 | |
| 170 | // For debugging purposes |
Sree Kuchibhotla | f51ea7a | 2015-10-26 13:34:41 -0700 | [diff] [blame] | 171 | void LogParameterInfo(const vector<grpc::string>& addresses, |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 172 | const vector<pair<TestCaseType, int>>& tests) { |
| 173 | gpr_log(GPR_INFO, "server_addresses: %s", FLAGS_server_addresses.c_str()); |
| 174 | gpr_log(GPR_INFO, "test_cases : %s", FLAGS_test_cases.c_str()); |
| 175 | gpr_log(GPR_INFO, "sleep_duration_ms: %d", FLAGS_sleep_duration_ms); |
| 176 | gpr_log(GPR_INFO, "test_duration_secs: %d", FLAGS_test_duration_secs); |
| 177 | |
| 178 | int num = 0; |
| 179 | for (auto it = addresses.begin(); it != addresses.end(); it++) { |
| 180 | gpr_log(GPR_INFO, "%d:%s", ++num, it->c_str()); |
| 181 | } |
| 182 | |
| 183 | num = 0; |
| 184 | for (auto it = tests.begin(); it != tests.end(); it++) { |
| 185 | TestCaseType test_case = it->first; |
| 186 | int weight = it->second; |
| 187 | gpr_log(GPR_INFO, "%d. TestCaseType: %d, Weight: %d", ++num, test_case, |
| 188 | weight); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | int main(int argc, char** argv) { |
| 193 | grpc::testing::InitTest(&argc, &argv, true); |
| 194 | |
| 195 | srand(time(NULL)); |
| 196 | |
| 197 | // Parse the server addresses |
Sree Kuchibhotla | f51ea7a | 2015-10-26 13:34:41 -0700 | [diff] [blame] | 198 | vector<grpc::string> server_addresses; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 199 | ParseCommaDelimitedString(FLAGS_server_addresses, server_addresses); |
| 200 | |
| 201 | // Parse test cases and weights |
| 202 | if (FLAGS_test_cases.length() == 0) { |
Sree Kuchibhotla | e6cd0e7 | 2015-10-22 15:56:07 -0700 | [diff] [blame] | 203 | gpr_log(GPR_INFO, "Not running tests. The 'test_cases' string is empty"); |
Sree Kuchibhotla | 117c8af | 2015-10-26 10:59:17 -0700 | [diff] [blame] | 204 | return 1; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | vector<pair<TestCaseType, int>> tests; |
| 208 | if (!ParseTestCasesString(FLAGS_test_cases, tests)) { |
| 209 | gpr_log(GPR_ERROR, "Error in parsing test cases string %s ", |
| 210 | FLAGS_test_cases.c_str()); |
| 211 | return 1; |
| 212 | } |
| 213 | |
| 214 | LogParameterInfo(server_addresses, tests); |
| 215 | |
| 216 | WeightedRandomTestSelector test_selector(tests); |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame^] | 217 | MetricsServiceImpl metrics_service; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 218 | |
| 219 | gpr_log(GPR_INFO, "Starting test(s).."); |
Sree Kuchibhotla | 728a610 | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 220 | |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 221 | vector<thread> test_threads; |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame^] | 222 | |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 223 | int thread_idx = 0; |
| 224 | for (auto it = server_addresses.begin(); it != server_addresses.end(); it++) { |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame^] | 225 | // TODO(sreek): This will change once we add support for other tests |
| 226 | // that won't work with InsecureCredentials() |
| 227 | std::shared_ptr<grpc::Channel> channel( |
| 228 | CreateChannel(*it, grpc::InsecureCredentials())); |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 229 | |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame^] | 230 | // Make multiple stubs (as defined by num_stubs_per_channel flag) to use the |
| 231 | // same channel. This is to test calling multiple RPC calls in parallel on |
| 232 | // each channel. |
| 233 | for (int i = 0; i < FLAGS_num_stubs_per_channel; i++) { |
| 234 | StressTestInteropClient* client = new StressTestInteropClient( |
| 235 | ++thread_idx, *it, channel, test_selector, FLAGS_test_duration_secs, |
| 236 | FLAGS_sleep_duration_ms, FLAGS_metrics_collection_interval_secs); |
| 237 | |
| 238 | bool is_already_created; |
| 239 | grpc::string metricName = "/stress_test/rps/thread/" + std::to_string(i); |
| 240 | test_threads.emplace_back( |
| 241 | thread(&StressTestInteropClient::MainLoop, client, |
| 242 | metrics_service.CreateGuage(metricName, is_already_created))); |
| 243 | |
| 244 | // The Guage should not have been already created |
| 245 | GPR_ASSERT(!is_already_created); |
| 246 | } |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame^] | 249 | // Start metrics server before waiting for the stress test threads |
| 250 | std::unique_ptr<grpc::Server> metrics_server = |
| 251 | metrics_service.StartServer(FLAGS_metrics_port); |
| 252 | |
| 253 | // Wait for the stress test threads to complete |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 254 | for (auto it = test_threads.begin(); it != test_threads.end(); it++) { |
| 255 | it->join(); |
| 256 | } |
| 257 | |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame^] | 258 | metrics_server->Wait(); |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 259 | return 0; |
| 260 | } |