1 /*
2 *
3 * Copyright 2015-2016 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <fstream>
20 #include <iostream>
21 #include <memory>
22 #include <set>
23
24 #include <grpcpp/impl/codegen/config_protobuf.h>
25
26 #include <gflags/gflags.h>
27 #include <grpc/support/log.h>
28
29 #include "test/cpp/qps/benchmark_config.h"
30 #include "test/cpp/qps/driver.h"
31 #include "test/cpp/qps/parse_json.h"
32 #include "test/cpp/qps/report.h"
33 #include "test/cpp/qps/server.h"
34 #include "test/cpp/util/test_config.h"
35 #include "test/cpp/util/test_credentials_provider.h"
36
37 DEFINE_string(scenarios_file, "",
38 "JSON file containing an array of Scenario objects");
39 DEFINE_string(scenarios_json, "",
40 "JSON string containing an array of Scenario objects");
41 DEFINE_bool(quit, false, "Quit the workers");
42 DEFINE_string(search_param, "",
43 "The parameter, whose value is to be searched for to achieve "
44 "targeted cpu load. For now, we have 'offered_load'. Later, "
45 "'num_channels', 'num_outstanding_requests', etc. shall be "
46 "added.");
47 DEFINE_double(
48 initial_search_value, 0.0,
49 "initial parameter value to start the search with (i.e. lower bound)");
50 DEFINE_double(targeted_cpu_load, 70.0,
51 "Targeted cpu load (unit: %, range [0,100])");
52 DEFINE_double(stride, 1,
53 "Defines each stride of the search. The larger the stride is, "
54 "the coarser the result will be, but will also be faster.");
55 DEFINE_double(error_tolerance, 0.01,
56 "Defines threshold for stopping the search. When current search "
57 "range is narrower than the error_tolerance computed range, we "
58 "stop the search.");
59
60 DEFINE_string(qps_server_target_override, "",
61 "Override QPS server target to configure in client configs."
62 "Only applicable if there is a single benchmark server.");
63
64 DEFINE_string(json_file_out, "", "File to write the JSON output to.");
65
66 DEFINE_string(credential_type, grpc::testing::kInsecureCredentialsType,
67 "Credential type for communication with workers");
68 DEFINE_string(
69 per_worker_credential_types, "",
70 "A map of QPS worker addresses to credential types. When creating a "
71 "channel to a QPS worker's driver port, the qps_json_driver first checks "
72 "if the 'name:port' string is in the map, and it uses the corresponding "
73 "credential type if so. If the QPS worker's 'name:port' string is not "
74 "in the map, then the driver -> worker channel will be created with "
75 "the credentials specified in --credential_type. The value of this flag "
76 "is a semicolon-separated list of map entries, where each map entry is "
77 "a comma-separated pair.");
78 DEFINE_bool(run_inproc, false, "Perform an in-process transport test");
79 DEFINE_int32(
80 median_latency_collection_interval_millis, 0,
81 "Specifies the period between gathering latency medians in "
82 "milliseconds. The medians will be logged out on the client at the "
83 "end of the benchmark run. If 0, this periodic collection is disabled.");
84
85 namespace grpc {
86 namespace testing {
87
88 static std::map<std::string, std::string>
ConstructPerWorkerCredentialTypesMap()89 ConstructPerWorkerCredentialTypesMap() {
90 // Parse a list of the form: "addr1,cred_type1;addr2,cred_type2;..." into
91 // a map.
92 std::string remaining = FLAGS_per_worker_credential_types;
93 std::map<std::string, std::string> out;
94 while (!remaining.empty()) {
95 size_t next_semicolon = remaining.find(';');
96 std::string next_entry = remaining.substr(0, next_semicolon);
97 if (next_semicolon == std::string::npos) {
98 remaining = "";
99 } else {
100 remaining = remaining.substr(next_semicolon + 1, std::string::npos);
101 }
102 size_t comma = next_entry.find(',');
103 if (comma == std::string::npos) {
104 gpr_log(GPR_ERROR,
105 "Expectd --per_worker_credential_types to be a list "
106 "of the form: 'addr1,cred_type1;addr2,cred_type2;...' "
107 "into.");
108 abort();
109 }
110 std::string addr = next_entry.substr(0, comma);
111 std::string cred_type = next_entry.substr(comma + 1, std::string::npos);
112 if (out.find(addr) != out.end()) {
113 gpr_log(GPR_ERROR,
114 "Found duplicate addr in per_worker_credential_types.");
115 abort();
116 }
117 out[addr] = cred_type;
118 }
119 return out;
120 }
121
RunAndReport(const Scenario & scenario,const std::map<std::string,std::string> & per_worker_credential_types,bool * success)122 static std::unique_ptr<ScenarioResult> RunAndReport(
123 const Scenario& scenario,
124 const std::map<std::string, std::string>& per_worker_credential_types,
125 bool* success) {
126 std::cerr << "RUNNING SCENARIO: " << scenario.name() << "\n";
127 auto result =
128 RunScenario(scenario.client_config(), scenario.num_clients(),
129 scenario.server_config(), scenario.num_servers(),
130 scenario.warmup_seconds(), scenario.benchmark_seconds(),
131 !FLAGS_run_inproc ? scenario.spawn_local_worker_count() : -2,
132 FLAGS_qps_server_target_override, FLAGS_credential_type,
133 per_worker_credential_types, FLAGS_run_inproc,
134 FLAGS_median_latency_collection_interval_millis);
135
136 // Amend the result with scenario config. Eventually we should adjust
137 // RunScenario contract so we don't need to touch the result here.
138 result->mutable_scenario()->CopyFrom(scenario);
139
140 GetReporter()->ReportQPS(*result);
141 GetReporter()->ReportQPSPerCore(*result);
142 GetReporter()->ReportLatency(*result);
143 GetReporter()->ReportTimes(*result);
144 GetReporter()->ReportCpuUsage(*result);
145 GetReporter()->ReportPollCount(*result);
146 GetReporter()->ReportQueriesPerCpuSec(*result);
147
148 for (int i = 0; *success && i < result->client_success_size(); i++) {
149 *success = result->client_success(i);
150 }
151 for (int i = 0; *success && i < result->server_success_size(); i++) {
152 *success = result->server_success(i);
153 }
154
155 if (FLAGS_json_file_out != "") {
156 std::ofstream json_outfile;
157 json_outfile.open(FLAGS_json_file_out);
158 json_outfile << "{\"qps\": " << result->summary().qps() << "}\n";
159 json_outfile.close();
160 }
161
162 return result;
163 }
164
GetCpuLoad(Scenario * scenario,double offered_load,const std::map<std::string,std::string> & per_worker_credential_types,bool * success)165 static double GetCpuLoad(
166 Scenario* scenario, double offered_load,
167 const std::map<std::string, std::string>& per_worker_credential_types,
168 bool* success) {
169 scenario->mutable_client_config()
170 ->mutable_load_params()
171 ->mutable_poisson()
172 ->set_offered_load(offered_load);
173 auto result = RunAndReport(*scenario, per_worker_credential_types, success);
174 return result->summary().server_cpu_usage();
175 }
176
BinarySearch(Scenario * scenario,double targeted_cpu_load,double low,double high,const std::map<std::string,std::string> & per_worker_credential_types,bool * success)177 static double BinarySearch(
178 Scenario* scenario, double targeted_cpu_load, double low, double high,
179 const std::map<std::string, std::string>& per_worker_credential_types,
180 bool* success) {
181 while (low <= high * (1 - FLAGS_error_tolerance)) {
182 double mid = low + (high - low) / 2;
183 double current_cpu_load =
184 GetCpuLoad(scenario, mid, per_worker_credential_types, success);
185 gpr_log(GPR_DEBUG, "Binary Search: current_offered_load %.0f", mid);
186 if (!*success) {
187 gpr_log(GPR_ERROR, "Client/Server Failure");
188 break;
189 }
190 if (targeted_cpu_load <= current_cpu_load) {
191 high = mid - FLAGS_stride;
192 } else {
193 low = mid + FLAGS_stride;
194 }
195 }
196
197 return low;
198 }
199
SearchOfferedLoad(double initial_offered_load,double targeted_cpu_load,Scenario * scenario,const std::map<std::string,std::string> & per_worker_credential_types,bool * success)200 static double SearchOfferedLoad(
201 double initial_offered_load, double targeted_cpu_load, Scenario* scenario,
202 const std::map<std::string, std::string>& per_worker_credential_types,
203 bool* success) {
204 std::cerr << "RUNNING SCENARIO: " << scenario->name() << "\n";
205 double current_offered_load = initial_offered_load;
206 double current_cpu_load = GetCpuLoad(scenario, current_offered_load,
207 per_worker_credential_types, success);
208 if (current_cpu_load > targeted_cpu_load) {
209 gpr_log(GPR_ERROR, "Initial offered load too high");
210 return -1;
211 }
212
213 while (*success && (current_cpu_load < targeted_cpu_load)) {
214 current_offered_load *= 2;
215 current_cpu_load = GetCpuLoad(scenario, current_offered_load,
216 per_worker_credential_types, success);
217 gpr_log(GPR_DEBUG, "Binary Search: current_offered_load %.0f",
218 current_offered_load);
219 }
220
221 double targeted_offered_load =
222 BinarySearch(scenario, targeted_cpu_load, current_offered_load / 2,
223 current_offered_load, per_worker_credential_types, success);
224
225 return targeted_offered_load;
226 }
227
QpsDriver()228 static bool QpsDriver() {
229 std::string json;
230
231 bool scfile = (FLAGS_scenarios_file != "");
232 bool scjson = (FLAGS_scenarios_json != "");
233 if ((!scfile && !scjson && !FLAGS_quit) ||
234 (scfile && (scjson || FLAGS_quit)) || (scjson && FLAGS_quit)) {
235 gpr_log(GPR_ERROR,
236 "Exactly one of --scenarios_file, --scenarios_json, "
237 "or --quit must be set");
238 abort();
239 }
240
241 auto per_worker_credential_types = ConstructPerWorkerCredentialTypesMap();
242 if (scfile) {
243 // Read the json data from disk
244 FILE* json_file = fopen(FLAGS_scenarios_file.c_str(), "r");
245 GPR_ASSERT(json_file != nullptr);
246 fseek(json_file, 0, SEEK_END);
247 long len = ftell(json_file);
248 char* data = new char[len];
249 fseek(json_file, 0, SEEK_SET);
250 GPR_ASSERT(len == (long)fread(data, 1, len, json_file));
251 fclose(json_file);
252 json = std::string(data, data + len);
253 delete[] data;
254 } else if (scjson) {
255 json = FLAGS_scenarios_json.c_str();
256 } else if (FLAGS_quit) {
257 return RunQuit(FLAGS_credential_type, per_worker_credential_types);
258 }
259
260 // Parse into an array of scenarios
261 Scenarios scenarios;
262 ParseJson(json.c_str(), "grpc.testing.Scenarios", &scenarios);
263 bool success = true;
264
265 // Make sure that there is at least some valid scenario here
266 GPR_ASSERT(scenarios.scenarios_size() > 0);
267
268 for (int i = 0; i < scenarios.scenarios_size(); i++) {
269 if (FLAGS_search_param == "") {
270 const Scenario& scenario = scenarios.scenarios(i);
271 RunAndReport(scenario, per_worker_credential_types, &success);
272 } else {
273 if (FLAGS_search_param == "offered_load") {
274 Scenario* scenario = scenarios.mutable_scenarios(i);
275 double targeted_offered_load = SearchOfferedLoad(
276 FLAGS_initial_search_value, FLAGS_targeted_cpu_load, scenario,
277 per_worker_credential_types, &success);
278 gpr_log(GPR_INFO, "targeted_offered_load %f", targeted_offered_load);
279 GetCpuLoad(scenario, targeted_offered_load, per_worker_credential_types,
280 &success);
281 } else {
282 gpr_log(GPR_ERROR, "Unimplemented search param");
283 }
284 }
285 }
286 return success;
287 }
288
289 } // namespace testing
290 } // namespace grpc
291
main(int argc,char ** argv)292 int main(int argc, char** argv) {
293 grpc::testing::InitTest(&argc, &argv, true);
294
295 bool ok = grpc::testing::QpsDriver();
296
297 return ok ? 0 : 1;
298 }
299