blob: b2449da69cb254f4305b0d88096150a61ec8460c [file] [log] [blame]
Craig Tiller0bda0b32016-03-03 12:51:53 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015-2016 gRPC authors.
Craig Tiller0bda0b32016-03-03 12:51:53 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
Craig Tiller0bda0b32016-03-03 12:51:53 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller0bda0b32016-03-03 12:51:53 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
Craig Tiller0bda0b32016-03-03 12:51:53 -080016 *
17 */
18
ncteisen24e82072017-06-14 19:56:29 -070019#include <fstream>
ncteisendc1b51e2017-06-19 09:00:25 -070020#include <iostream>
Craig Tiller0bda0b32016-03-03 12:51:53 -080021#include <memory>
22#include <set>
23
yang-g17487f92016-06-03 15:21:15 -070024#include <grpc++/impl/codegen/config_protobuf.h>
Craig Tiller0bda0b32016-03-03 12:51:53 -080025
26#include <gflags/gflags.h>
27#include <grpc/support/log.h>
28
Nicolas "Pixel" Noble6f7dcd02017-03-27 18:51:55 +020029#include "test/cpp/qps/benchmark_config.h"
Craig Tiller0bda0b32016-03-03 12:51:53 -080030#include "test/cpp/qps/driver.h"
Craig Tiller3ab2fe02016-04-11 20:11:18 -070031#include "test/cpp/qps/parse_json.h"
Craig Tiller0bda0b32016-03-03 12:51:53 -080032#include "test/cpp/qps/report.h"
Vijay Pai088112f2017-08-24 10:42:27 -070033#include "test/cpp/qps/server.h"
Guantao Liuabe3cf52017-07-12 17:36:30 -070034#include "test/cpp/util/test_config.h"
Guantao Liuac9fdfb2017-07-14 17:33:19 -070035#include "test/cpp/util/test_credentials_provider.h"
Craig Tiller0bda0b32016-03-03 12:51:53 -080036
37DEFINE_string(scenarios_file, "",
38 "JSON file containing an array of Scenario objects");
39DEFINE_string(scenarios_json, "",
40 "JSON string containing an array of Scenario objects");
vjpai29089c72016-04-20 12:38:16 -070041DEFINE_bool(quit, false, "Quit the workers");
Yuxuan Lic6c6cc22016-10-06 18:28:05 -070042DEFINE_string(search_param, "",
43 "The parameter, whose value is to be searched for to achieve "
Yuxuan Li60d55f82016-10-21 18:18:29 -070044 "targeted cpu load. For now, we have 'offered_load'. Later, "
45 "'num_channels', 'num_outstanding_requests', etc. shall be "
46 "added.");
Yuxuan Lic6c6cc22016-10-06 18:28:05 -070047DEFINE_double(
48 initial_search_value, 0.0,
49 "initial parameter value to start the search with (i.e. lower bound)");
50DEFINE_double(targeted_cpu_load, 70.0,
51 "Targeted cpu load (unit: %, range [0,100])");
Yuxuan Li60d55f82016-10-21 18:18:29 -070052DEFINE_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.");
55DEFINE_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.");
Yuxuan Li58977f12016-10-04 14:04:39 -070059
Alexander Polcyn4873d302016-12-19 15:58:15 -080060DEFINE_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.");
Alexander Polcyn4873d302016-12-19 15:58:15 -080063
ncteisendc1b51e2017-06-19 09:00:25 -070064DEFINE_string(json_file_out, "", "File to write the JSON output to.");
ncteisen24e82072017-06-14 19:56:29 -070065
Guantao Liuac9fdfb2017-07-14 17:33:19 -070066DEFINE_string(credential_type, grpc::testing::kInsecureCredentialsType,
Guantao Liuabe3cf52017-07-12 17:36:30 -070067 "Credential type for communication with workers");
Vijay Pai088112f2017-08-24 10:42:27 -070068DEFINE_bool(run_inproc, false, "Perform an in-process transport test");
Guantao Liuabe3cf52017-07-12 17:36:30 -070069
Yuxuan Lic9ca0a92016-10-28 14:08:36 -070070namespace grpc {
71namespace testing {
Craig Tiller0bda0b32016-03-03 12:51:53 -080072
Yuxuan Lic9ca0a92016-10-28 14:08:36 -070073static std::unique_ptr<ScenarioResult> RunAndReport(const Scenario& scenario,
74 bool* success) {
75 std::cerr << "RUNNING SCENARIO: " << scenario.name() << "\n";
Alexander Polcyn62a7ca82017-01-12 11:32:15 -080076 auto result =
77 RunScenario(scenario.client_config(), scenario.num_clients(),
78 scenario.server_config(), scenario.num_servers(),
79 scenario.warmup_seconds(), scenario.benchmark_seconds(),
Vijay Pai088112f2017-08-24 10:42:27 -070080 !FLAGS_run_inproc ? scenario.spawn_local_worker_count() : -2,
81 FLAGS_qps_server_target_override, FLAGS_credential_type,
82 FLAGS_run_inproc);
Yuxuan Li58977f12016-10-04 14:04:39 -070083
Yuxuan Lic9ca0a92016-10-28 14:08:36 -070084 // Amend the result with scenario config. Eventually we should adjust
85 // RunScenario contract so we don't need to touch the result here.
86 result->mutable_scenario()->CopyFrom(scenario);
Yuxuan Li58977f12016-10-04 14:04:39 -070087
Yuxuan Lic9ca0a92016-10-28 14:08:36 -070088 GetReporter()->ReportQPS(*result);
89 GetReporter()->ReportQPSPerCore(*result);
90 GetReporter()->ReportLatency(*result);
91 GetReporter()->ReportTimes(*result);
92 GetReporter()->ReportCpuUsage(*result);
Yuxuan Li999ac152017-05-03 21:36:36 -070093 GetReporter()->ReportPollCount(*result);
Guantao Liuabe3cf52017-07-12 17:36:30 -070094 GetReporter()->ReportQueriesPerCpuSec(*result);
Yuxuan Li58977f12016-10-04 14:04:39 -070095
Yuxuan Lic9ca0a92016-10-28 14:08:36 -070096 for (int i = 0; *success && i < result->client_success_size(); i++) {
97 *success = result->client_success(i);
98 }
99 for (int i = 0; *success && i < result->server_success_size(); i++) {
100 *success = result->server_success(i);
Yuxuan Li58977f12016-10-04 14:04:39 -0700101 }
102
ncteisen24e82072017-06-14 19:56:29 -0700103 if (FLAGS_json_file_out != "") {
104 std::ofstream json_outfile;
105 json_outfile.open(FLAGS_json_file_out);
106 json_outfile << "{\"qps\": " << result->summary().qps() << "}\n";
107 json_outfile.close();
108 }
109
Yuxuan Lic9ca0a92016-10-28 14:08:36 -0700110 return result;
111}
112
113static double GetCpuLoad(Scenario* scenario, double offered_load,
114 bool* success) {
115 scenario->mutable_client_config()
116 ->mutable_load_params()
117 ->mutable_poisson()
118 ->set_offered_load(offered_load);
119 auto result = RunAndReport(*scenario, success);
120 return result->summary().server_cpu_usage();
121}
122
123static double BinarySearch(Scenario* scenario, double targeted_cpu_load,
124 double low, double high, bool* success) {
125 while (low <= high * (1 - FLAGS_error_tolerance)) {
126 double mid = low + (high - low) / 2;
127 double current_cpu_load = GetCpuLoad(scenario, mid, success);
128 gpr_log(GPR_DEBUG, "Binary Search: current_offered_load %.0f", mid);
129 if (!*success) {
130 gpr_log(GPR_ERROR, "Client/Server Failure");
131 break;
132 }
133 if (targeted_cpu_load <= current_cpu_load) {
134 high = mid - FLAGS_stride;
135 } else {
136 low = mid + FLAGS_stride;
137 }
Yuxuan Li49aeb592016-10-06 11:01:50 -0700138 }
Yuxuan Li58977f12016-10-04 14:04:39 -0700139
Yuxuan Lic9ca0a92016-10-28 14:08:36 -0700140 return low;
141}
142
143static double SearchOfferedLoad(double initial_offered_load,
144 double targeted_cpu_load, Scenario* scenario,
145 bool* success) {
146 std::cerr << "RUNNING SCENARIO: " << scenario->name() << "\n";
147 double current_offered_load = initial_offered_load;
148 double current_cpu_load = GetCpuLoad(scenario, current_offered_load, success);
149 if (current_cpu_load > targeted_cpu_load) {
150 gpr_log(GPR_ERROR, "Initial offered load too high");
151 return -1;
152 }
153
154 while (*success && (current_cpu_load < targeted_cpu_load)) {
155 current_offered_load *= 2;
156 current_cpu_load = GetCpuLoad(scenario, current_offered_load, success);
157 gpr_log(GPR_DEBUG, "Binary Search: current_offered_load %.0f",
158 current_offered_load);
159 }
160
161 double targeted_offered_load =
162 BinarySearch(scenario, targeted_cpu_load, current_offered_load / 2,
163 current_offered_load, success);
164
165 return targeted_offered_load;
166}
167
168static bool QpsDriver() {
169 grpc::string json;
170
171 bool scfile = (FLAGS_scenarios_file != "");
172 bool scjson = (FLAGS_scenarios_json != "");
173 if ((!scfile && !scjson && !FLAGS_quit) ||
174 (scfile && (scjson || FLAGS_quit)) || (scjson && FLAGS_quit)) {
175 gpr_log(GPR_ERROR,
176 "Exactly one of --scenarios_file, --scenarios_json, "
177 "or --quit must be set");
178 abort();
179 }
180
181 if (scfile) {
182 // Read the json data from disk
183 FILE* json_file = fopen(FLAGS_scenarios_file.c_str(), "r");
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800184 GPR_ASSERT(json_file != nullptr);
Yuxuan Lic9ca0a92016-10-28 14:08:36 -0700185 fseek(json_file, 0, SEEK_END);
186 long len = ftell(json_file);
187 char* data = new char[len];
188 fseek(json_file, 0, SEEK_SET);
189 GPR_ASSERT(len == (long)fread(data, 1, len, json_file));
190 fclose(json_file);
191 json = grpc::string(data, data + len);
192 delete[] data;
193 } else if (scjson) {
194 json = FLAGS_scenarios_json.c_str();
195 } else if (FLAGS_quit) {
Guantao Liuac9fdfb2017-07-14 17:33:19 -0700196 return RunQuit(FLAGS_credential_type);
Yuxuan Lic9ca0a92016-10-28 14:08:36 -0700197 }
198
199 // Parse into an array of scenarios
200 Scenarios scenarios;
201 ParseJson(json.c_str(), "grpc.testing.Scenarios", &scenarios);
202 bool success = true;
203
204 // Make sure that there is at least some valid scenario here
205 GPR_ASSERT(scenarios.scenarios_size() > 0);
206
207 for (int i = 0; i < scenarios.scenarios_size(); i++) {
208 if (FLAGS_search_param == "") {
209 const Scenario& scenario = scenarios.scenarios(i);
210 RunAndReport(scenario, &success);
211 } else {
212 if (FLAGS_search_param == "offered_load") {
213 Scenario* scenario = scenarios.mutable_scenarios(i);
214 double targeted_offered_load =
215 SearchOfferedLoad(FLAGS_initial_search_value,
216 FLAGS_targeted_cpu_load, scenario, &success);
217 gpr_log(GPR_INFO, "targeted_offered_load %f", targeted_offered_load);
Yuxuan Liac87a462016-11-11 12:05:11 -0800218 GetCpuLoad(scenario, targeted_offered_load, &success);
Yuxuan Lic6c6cc22016-10-06 18:28:05 -0700219 } else {
Yuxuan Lic9ca0a92016-10-28 14:08:36 -0700220 gpr_log(GPR_ERROR, "Unimplemented search param");
Yuxuan Li69c61312016-10-06 17:00:48 -0700221 }
Vijay Paiceb1a7d2016-07-07 11:06:04 -0700222 }
Yuxuan Li60d55f82016-10-21 18:18:29 -0700223 }
Yuxuan Lic9ca0a92016-10-28 14:08:36 -0700224 return success;
225}
Yuxuan Li60d55f82016-10-21 18:18:29 -0700226
Yuxuan Lic9ca0a92016-10-28 14:08:36 -0700227} // namespace testing
Craig Tiller0bda0b32016-03-03 12:51:53 -0800228} // namespace grpc
229
Yuxuan Li49aeb592016-10-06 11:01:50 -0700230int main(int argc, char** argv) {
Guantao Liuabe3cf52017-07-12 17:36:30 -0700231 grpc::testing::InitTest(&argc, &argv, true);
Craig Tiller0bda0b32016-03-03 12:51:53 -0800232
Vijay Paiceb1a7d2016-07-07 11:06:04 -0700233 bool ok = grpc::testing::QpsDriver();
Craig Tiller0bda0b32016-03-03 12:51:53 -0800234
Vijay Paiceb1a7d2016-07-07 11:06:04 -0700235 return ok ? 0 : 1;
Craig Tiller0bda0b32016-03-03 12:51:53 -0800236}