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" |
| 47 | #include "test/cpp/util/test_config.h" |
| 48 | |
| 49 | DEFINE_int32(sleep_duration_ms, 0, |
| 50 | "The duration (in millisec) between two" |
| 51 | " consecutive test calls (per server) issued by the server."); |
| 52 | |
| 53 | DEFINE_int32(test_duration_secs, -1, |
| 54 | "The length of time (in seconds) to run" |
| 55 | " the test. Enter -1 if the test should run continuously until" |
| 56 | " forcefully terminated."); |
| 57 | |
| 58 | DEFINE_string(server_addresses, "localhost:8080", |
| 59 | "The list of server" |
| 60 | " addresses in the format:\n" |
| 61 | " \"<name_1>:<port_1>,<name_2>:<port_1>...<name_N>:<port_N>\"\n" |
| 62 | " Note: <name> can be servername or IP address."); |
| 63 | |
| 64 | // TODO(sreek): Add more test cases here in future |
| 65 | DEFINE_string(test_cases, "", |
| 66 | "List of test cases to call along with the" |
| 67 | " relative weights in the following format:\n" |
| 68 | " \"<testcase_1:w_1>,<testcase_2:w_2>...<testcase_n:w_n>\"\n" |
| 69 | " The following testcases are currently supported:\n" |
| 70 | " empty_unary\n" |
| 71 | " large_unary\n" |
| 72 | " large_compressed_unary\n" |
| 73 | " client_streaming\n" |
| 74 | " server_streaming\n" |
| 75 | " empty_stream\n" |
| 76 | " Example: \"empty_unary:20,large_unary:10,empty_stream:70\"\n" |
| 77 | " The above will execute 'empty_unary', 20% of the time," |
| 78 | " 'large_unary', 10% of the time and 'empty_stream' the remaining" |
| 79 | " 70% of the time"); |
| 80 | |
| 81 | using std::make_pair; |
| 82 | using std::pair; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 83 | using std::thread; |
| 84 | using std::vector; |
| 85 | |
David Garcia Quintas | ba4290a | 2015-10-23 15:20:48 -0700 | [diff] [blame] | 86 | using grpc::string; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 87 | using grpc::testing::kTestCaseList; |
| 88 | using grpc::testing::StressTestInteropClient; |
| 89 | using grpc::testing::TestCaseType; |
| 90 | using grpc::testing::WeightedRandomTestSelector; |
| 91 | using grpc::testing::UNKNOWN_TEST; |
| 92 | |
| 93 | TestCaseType GetTestTypeFromName(const string& test_name) { |
| 94 | TestCaseType test_case = UNKNOWN_TEST; |
| 95 | |
| 96 | for (auto it = kTestCaseList.begin(); it != kTestCaseList.end(); it++) { |
| 97 | if (test_name == it->second) { |
| 98 | test_case = it->first; |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return test_case; |
| 104 | } |
| 105 | |
| 106 | // Converts a string of comma delimited tokens to a vector of tokens |
| 107 | bool ParseCommaDelimitedString(const string& comma_delimited_str, |
| 108 | vector<string>& tokens) { |
| 109 | size_t bpos = 0; |
| 110 | size_t epos = string::npos; |
| 111 | |
| 112 | while ((epos = comma_delimited_str.find(',', bpos)) != string::npos) { |
| 113 | tokens.emplace_back(comma_delimited_str.substr(bpos, epos - bpos)); |
| 114 | bpos = epos + 1; |
| 115 | } |
| 116 | |
| 117 | tokens.emplace_back(comma_delimited_str.substr(bpos)); // Last token |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | // Input: Test case string "<testcase_name:weight>,<testcase_name:weight>...." |
| 122 | // Output: |
| 123 | // - Whether parsing was successful (return value) |
| 124 | // - Vector of (test_type_enum, weight) pairs returned via 'tests' parameter |
| 125 | bool ParseTestCasesString(const string& test_cases, |
| 126 | vector<pair<TestCaseType, int>>& tests) { |
| 127 | bool is_success = true; |
| 128 | |
| 129 | vector<string> tokens; |
| 130 | ParseCommaDelimitedString(test_cases, tokens); |
| 131 | |
| 132 | for (auto it = tokens.begin(); it != tokens.end(); it++) { |
| 133 | // Token is in the form <test_name>:<test_weight> |
| 134 | size_t colon_pos = it->find(':'); |
| 135 | if (colon_pos == string::npos) { |
| 136 | gpr_log(GPR_ERROR, "Error in parsing test case string: %s", it->c_str()); |
| 137 | is_success = false; |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | string test_name = it->substr(0, colon_pos); |
| 142 | int weight = std::stoi(it->substr(colon_pos + 1)); |
| 143 | TestCaseType test_case = GetTestTypeFromName(test_name); |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 144 | if (test_case == UNKNOWN_TEST) { |
| 145 | gpr_log(GPR_ERROR, "Unknown test case: %s", test_name.c_str()); |
| 146 | is_success = false; |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | tests.emplace_back(std::make_pair(test_case, weight)); |
| 151 | } |
| 152 | |
| 153 | return is_success; |
| 154 | } |
| 155 | |
| 156 | // For debugging purposes |
| 157 | void LogParameterInfo(const vector<string>& addresses, |
| 158 | const vector<pair<TestCaseType, int>>& tests) { |
| 159 | gpr_log(GPR_INFO, "server_addresses: %s", FLAGS_server_addresses.c_str()); |
| 160 | gpr_log(GPR_INFO, "test_cases : %s", FLAGS_test_cases.c_str()); |
| 161 | gpr_log(GPR_INFO, "sleep_duration_ms: %d", FLAGS_sleep_duration_ms); |
| 162 | gpr_log(GPR_INFO, "test_duration_secs: %d", FLAGS_test_duration_secs); |
| 163 | |
| 164 | int num = 0; |
| 165 | for (auto it = addresses.begin(); it != addresses.end(); it++) { |
| 166 | gpr_log(GPR_INFO, "%d:%s", ++num, it->c_str()); |
| 167 | } |
| 168 | |
| 169 | num = 0; |
| 170 | for (auto it = tests.begin(); it != tests.end(); it++) { |
| 171 | TestCaseType test_case = it->first; |
| 172 | int weight = it->second; |
| 173 | gpr_log(GPR_INFO, "%d. TestCaseType: %d, Weight: %d", ++num, test_case, |
| 174 | weight); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | int main(int argc, char** argv) { |
| 179 | grpc::testing::InitTest(&argc, &argv, true); |
| 180 | |
| 181 | srand(time(NULL)); |
| 182 | |
| 183 | // Parse the server addresses |
| 184 | vector<string> server_addresses; |
| 185 | ParseCommaDelimitedString(FLAGS_server_addresses, server_addresses); |
| 186 | |
| 187 | // Parse test cases and weights |
| 188 | if (FLAGS_test_cases.length() == 0) { |
Sree Kuchibhotla | e6cd0e7 | 2015-10-22 15:56:07 -0700 | [diff] [blame] | 189 | 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^] | 190 | return 1; |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | vector<pair<TestCaseType, int>> tests; |
| 194 | if (!ParseTestCasesString(FLAGS_test_cases, tests)) { |
| 195 | gpr_log(GPR_ERROR, "Error in parsing test cases string %s ", |
| 196 | FLAGS_test_cases.c_str()); |
| 197 | return 1; |
| 198 | } |
| 199 | |
| 200 | LogParameterInfo(server_addresses, tests); |
| 201 | |
| 202 | WeightedRandomTestSelector test_selector(tests); |
| 203 | |
| 204 | gpr_log(GPR_INFO, "Starting test(s).."); |
Sree Kuchibhotla | 728a610 | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 205 | |
Sree Kuchibhotla | fbc376f | 2015-10-16 10:56:31 -0700 | [diff] [blame] | 206 | vector<thread> test_threads; |
| 207 | int thread_idx = 0; |
| 208 | for (auto it = server_addresses.begin(); it != server_addresses.end(); it++) { |
| 209 | StressTestInteropClient* client = new StressTestInteropClient( |
| 210 | ++thread_idx, *it, test_selector, FLAGS_test_duration_secs, |
| 211 | FLAGS_sleep_duration_ms); |
| 212 | |
| 213 | test_threads.emplace_back( |
| 214 | thread(&StressTestInteropClient::MainLoop, client)); |
| 215 | } |
| 216 | |
| 217 | for (auto it = test_threads.begin(); it != test_threads.end(); it++) { |
| 218 | it->join(); |
| 219 | } |
| 220 | |
| 221 | return 0; |
| 222 | } |