blob: 5d1419728e4ca7d46ddc809a7d828a12e1f2860d [file] [log] [blame]
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -07001/*
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
49DEFINE_int32(sleep_duration_ms, 0,
50 "The duration (in millisec) between two"
51 " consecutive test calls (per server) issued by the server.");
52
53DEFINE_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
58DEFINE_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
65DEFINE_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
81using std::make_pair;
82using std::pair;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -070083using std::thread;
84using std::vector;
85
86using grpc::testing::kTestCaseList;
87using grpc::testing::StressTestInteropClient;
88using grpc::testing::TestCaseType;
89using grpc::testing::WeightedRandomTestSelector;
90using grpc::testing::UNKNOWN_TEST;
91
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -070092TestCaseType GetTestTypeFromName(const grpc::string& test_name) {
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -070093 TestCaseType test_case = UNKNOWN_TEST;
94
95 for (auto it = kTestCaseList.begin(); it != kTestCaseList.end(); it++) {
96 if (test_name == it->second) {
97 test_case = it->first;
98 break;
99 }
100 }
101
102 return test_case;
103}
104
105// Converts a string of comma delimited tokens to a vector of tokens
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700106bool ParseCommaDelimitedString(const grpc::string& comma_delimited_str,
107 vector<grpc::string>& tokens) {
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700108 size_t bpos = 0;
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700109 size_t epos = grpc::string::npos;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700110
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700111 while ((epos = comma_delimited_str.find(',', bpos)) != grpc::string::npos) {
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700112 tokens.emplace_back(comma_delimited_str.substr(bpos, epos - bpos));
113 bpos = epos + 1;
114 }
115
116 tokens.emplace_back(comma_delimited_str.substr(bpos)); // Last token
117 return true;
118}
119
120// Input: Test case string "<testcase_name:weight>,<testcase_name:weight>...."
121// Output:
122// - Whether parsing was successful (return value)
123// - Vector of (test_type_enum, weight) pairs returned via 'tests' parameter
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700124bool ParseTestCasesString(const grpc::string& test_cases,
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700125 vector<pair<TestCaseType, int>>& tests) {
126 bool is_success = true;
127
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700128 vector<grpc::string> tokens;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700129 ParseCommaDelimitedString(test_cases, tokens);
130
131 for (auto it = tokens.begin(); it != tokens.end(); it++) {
132 // Token is in the form <test_name>:<test_weight>
133 size_t colon_pos = it->find(':');
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700134 if (colon_pos == grpc::string::npos) {
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700135 gpr_log(GPR_ERROR, "Error in parsing test case string: %s", it->c_str());
136 is_success = false;
137 break;
138 }
139
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700140 grpc::string test_name = it->substr(0, colon_pos);
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700141 int weight = std::stoi(it->substr(colon_pos + 1));
142 TestCaseType test_case = GetTestTypeFromName(test_name);
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700143 if (test_case == UNKNOWN_TEST) {
144 gpr_log(GPR_ERROR, "Unknown test case: %s", test_name.c_str());
145 is_success = false;
146 break;
147 }
148
149 tests.emplace_back(std::make_pair(test_case, weight));
150 }
151
152 return is_success;
153}
154
155// For debugging purposes
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700156void LogParameterInfo(const vector<grpc::string>& addresses,
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700157 const vector<pair<TestCaseType, int>>& tests) {
158 gpr_log(GPR_INFO, "server_addresses: %s", FLAGS_server_addresses.c_str());
159 gpr_log(GPR_INFO, "test_cases : %s", FLAGS_test_cases.c_str());
160 gpr_log(GPR_INFO, "sleep_duration_ms: %d", FLAGS_sleep_duration_ms);
161 gpr_log(GPR_INFO, "test_duration_secs: %d", FLAGS_test_duration_secs);
162
163 int num = 0;
164 for (auto it = addresses.begin(); it != addresses.end(); it++) {
165 gpr_log(GPR_INFO, "%d:%s", ++num, it->c_str());
166 }
167
168 num = 0;
169 for (auto it = tests.begin(); it != tests.end(); it++) {
170 TestCaseType test_case = it->first;
171 int weight = it->second;
172 gpr_log(GPR_INFO, "%d. TestCaseType: %d, Weight: %d", ++num, test_case,
173 weight);
174 }
175}
176
177int main(int argc, char** argv) {
178 grpc::testing::InitTest(&argc, &argv, true);
179
180 srand(time(NULL));
181
182 // Parse the server addresses
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700183 vector<grpc::string> server_addresses;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700184 ParseCommaDelimitedString(FLAGS_server_addresses, server_addresses);
185
186 // Parse test cases and weights
187 if (FLAGS_test_cases.length() == 0) {
Sree Kuchibhotlae6cd0e72015-10-22 15:56:07 -0700188 gpr_log(GPR_INFO, "Not running tests. The 'test_cases' string is empty");
Sree Kuchibhotla117c8af2015-10-26 10:59:17 -0700189 return 1;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700190 }
191
192 vector<pair<TestCaseType, int>> tests;
193 if (!ParseTestCasesString(FLAGS_test_cases, tests)) {
194 gpr_log(GPR_ERROR, "Error in parsing test cases string %s ",
195 FLAGS_test_cases.c_str());
196 return 1;
197 }
198
199 LogParameterInfo(server_addresses, tests);
200
201 WeightedRandomTestSelector test_selector(tests);
202
203 gpr_log(GPR_INFO, "Starting test(s)..");
Sree Kuchibhotla728a6102015-10-16 10:56:31 -0700204
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700205 vector<thread> test_threads;
206 int thread_idx = 0;
207 for (auto it = server_addresses.begin(); it != server_addresses.end(); it++) {
208 StressTestInteropClient* client = new StressTestInteropClient(
209 ++thread_idx, *it, test_selector, FLAGS_test_duration_secs,
210 FLAGS_sleep_duration_ms);
211
212 test_threads.emplace_back(
213 thread(&StressTestInteropClient::MainLoop, client));
214 }
215
216 for (auto it = test_threads.begin(); it != test_threads.end(); it++) {
217 it->join();
218 }
219
220 return 0;
221}