blob: 5cb1e1fc90efc9013777f7ea1b625fd552ccd5fb [file] [log] [blame]
Sree Kuchibhotla728a6102015-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;
83using std::string;
84using std::thread;
85using std::vector;
86
87using grpc::testing::kTestCaseList;
88using grpc::testing::StressTestInteropClient;
89using grpc::testing::TestCaseType;
90using grpc::testing::TestSelector;
91using grpc::testing::UNKNOWN_TEST;
92
93TestCaseType get_test_type_from_name(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
106bool ParseCommaDelimitedString(const string& comma_delimited_str,
107 vector<string>& tokens) {
108 size_t bpos = 0;
109 size_t epos = string::npos;
110
111 while ((epos = comma_delimited_str.find(',', bpos)) != string::npos) {
112 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
124bool ParseTestCasesString(const string& test_cases,
125 vector<pair<TestCaseType, int>>& tests) {
126 bool is_success = true;
127
128 vector<string> tokens;
129 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(':');
134 if (colon_pos == string::npos) {
135 gpr_log(GPR_ERROR, "Error in parsing test case string: %s", it->c_str());
136 is_success = false;
137 break;
138 }
139
140 string test_name = it->substr(0, colon_pos);
141 int weight = std::stoi(it->substr(colon_pos + 1));
142 TestCaseType test_case = get_test_type_from_name(test_name);
143
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
157void 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
178int main(int argc, char** argv) {
179 grpc::testing::InitTest(&argc, &argv, true);
180 gpr_log(GPR_INFO, "starting test..");
181
182 srand(time(NULL));
183
184 // Parse the server addresses
185 vector<string> server_addresses;
186 ParseCommaDelimitedString(FLAGS_server_addresses, server_addresses);
187
188 // Parse test cases and weights
189 if (FLAGS_test_cases.length() == 0) {
190 gpr_log(GPR_INFO, "Test cases string is empty");
191 return 1;
192 }
193
194 vector<pair<TestCaseType, int>> tests;
195 if (!ParseTestCasesString(FLAGS_test_cases, tests)) {
196 gpr_log(GPR_ERROR, "Error in parsing test cases string %s ",
197 FLAGS_test_cases.c_str());
198 return 1;
199 }
200
201 LogParameterInfo(server_addresses, tests);
202
203 TestSelector test_selector(tests);
204
205 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}