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