blob: f00f771ea02ca42b752e8421f3f07222a5d12dd5 [file] [log] [blame]
Craig Tiller0bda0b32016-03-03 12:51:53 -08001/*
2 *
3 * Copyright 2015-2016, 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 *
32 */
33
yang-g90f43d42017-03-14 10:58:44 -070034#include <iostream>
Craig Tiller0bda0b32016-03-03 12:51:53 -080035#include <memory>
36#include <set>
37
yang-g17487f92016-06-03 15:21:15 -070038#include <grpc++/impl/codegen/config_protobuf.h>
Craig Tiller0bda0b32016-03-03 12:51:53 -080039
40#include <gflags/gflags.h>
41#include <grpc/support/log.h>
42
Nicolas "Pixel" Noble6f7dcd02017-03-27 18:51:55 +020043#include "test/cpp/qps/benchmark_config.h"
Craig Tiller0bda0b32016-03-03 12:51:53 -080044#include "test/cpp/qps/driver.h"
Craig Tiller3ab2fe02016-04-11 20:11:18 -070045#include "test/cpp/qps/parse_json.h"
Craig Tiller0bda0b32016-03-03 12:51:53 -080046#include "test/cpp/qps/report.h"
Craig Tiller0bda0b32016-03-03 12:51:53 -080047
48DEFINE_string(scenarios_file, "",
49 "JSON file containing an array of Scenario objects");
50DEFINE_string(scenarios_json, "",
51 "JSON string containing an array of Scenario objects");
vjpai29089c72016-04-20 12:38:16 -070052DEFINE_bool(quit, false, "Quit the workers");
Yuxuan Lic6c6cc22016-10-06 18:28:05 -070053DEFINE_string(search_param, "",
54 "The parameter, whose value is to be searched for to achieve "
Yuxuan Li60d55f82016-10-21 18:18:29 -070055 "targeted cpu load. For now, we have 'offered_load'. Later, "
56 "'num_channels', 'num_outstanding_requests', etc. shall be "
57 "added.");
Yuxuan Lic6c6cc22016-10-06 18:28:05 -070058DEFINE_double(
59 initial_search_value, 0.0,
60 "initial parameter value to start the search with (i.e. lower bound)");
61DEFINE_double(targeted_cpu_load, 70.0,
62 "Targeted cpu load (unit: %, range [0,100])");
Yuxuan Li60d55f82016-10-21 18:18:29 -070063DEFINE_double(stride, 1,
64 "Defines each stride of the search. The larger the stride is, "
65 "the coarser the result will be, but will also be faster.");
66DEFINE_double(error_tolerance, 0.01,
67 "Defines threshold for stopping the search. When current search "
68 "range is narrower than the error_tolerance computed range, we "
69 "stop the search.");
Yuxuan Li58977f12016-10-04 14:04:39 -070070
Alexander Polcyn4873d302016-12-19 15:58:15 -080071DEFINE_string(qps_server_target_override, "",
72 "Override QPS server target to configure in client configs."
73 "Only applicable if there is a single benchmark server.");
Alexander Polcyn4873d302016-12-19 15:58:15 -080074
Yuxuan Lic9ca0a92016-10-28 14:08:36 -070075namespace grpc {
76namespace testing {
Craig Tiller0bda0b32016-03-03 12:51:53 -080077
Yuxuan Lic9ca0a92016-10-28 14:08:36 -070078static std::unique_ptr<ScenarioResult> RunAndReport(const Scenario& scenario,
79 bool* success) {
80 std::cerr << "RUNNING SCENARIO: " << scenario.name() << "\n";
Alexander Polcyn62a7ca82017-01-12 11:32:15 -080081 auto result =
82 RunScenario(scenario.client_config(), scenario.num_clients(),
83 scenario.server_config(), scenario.num_servers(),
84 scenario.warmup_seconds(), scenario.benchmark_seconds(),
85 scenario.spawn_local_worker_count(),
86 FLAGS_qps_server_target_override.c_str());
Yuxuan Li58977f12016-10-04 14:04:39 -070087
Yuxuan Lic9ca0a92016-10-28 14:08:36 -070088 // Amend the result with scenario config. Eventually we should adjust
89 // RunScenario contract so we don't need to touch the result here.
90 result->mutable_scenario()->CopyFrom(scenario);
Yuxuan Li58977f12016-10-04 14:04:39 -070091
Yuxuan Lic9ca0a92016-10-28 14:08:36 -070092 GetReporter()->ReportQPS(*result);
93 GetReporter()->ReportQPSPerCore(*result);
94 GetReporter()->ReportLatency(*result);
95 GetReporter()->ReportTimes(*result);
96 GetReporter()->ReportCpuUsage(*result);
Yuxuan Li999ac152017-05-03 21:36:36 -070097 GetReporter()->ReportPollCount(*result);
Yuxuan Li58977f12016-10-04 14:04:39 -070098
Yuxuan Lic9ca0a92016-10-28 14:08:36 -070099 for (int i = 0; *success && i < result->client_success_size(); i++) {
100 *success = result->client_success(i);
101 }
102 for (int i = 0; *success && i < result->server_success_size(); i++) {
103 *success = result->server_success(i);
Yuxuan Li58977f12016-10-04 14:04:39 -0700104 }
105
Yuxuan Lic9ca0a92016-10-28 14:08:36 -0700106 return result;
107}
108
109static double GetCpuLoad(Scenario* scenario, double offered_load,
110 bool* success) {
111 scenario->mutable_client_config()
112 ->mutable_load_params()
113 ->mutable_poisson()
114 ->set_offered_load(offered_load);
115 auto result = RunAndReport(*scenario, success);
116 return result->summary().server_cpu_usage();
117}
118
119static double BinarySearch(Scenario* scenario, double targeted_cpu_load,
120 double low, double high, bool* success) {
121 while (low <= high * (1 - FLAGS_error_tolerance)) {
122 double mid = low + (high - low) / 2;
123 double current_cpu_load = GetCpuLoad(scenario, mid, success);
124 gpr_log(GPR_DEBUG, "Binary Search: current_offered_load %.0f", mid);
125 if (!*success) {
126 gpr_log(GPR_ERROR, "Client/Server Failure");
127 break;
128 }
129 if (targeted_cpu_load <= current_cpu_load) {
130 high = mid - FLAGS_stride;
131 } else {
132 low = mid + FLAGS_stride;
133 }
Yuxuan Li49aeb592016-10-06 11:01:50 -0700134 }
Yuxuan Li58977f12016-10-04 14:04:39 -0700135
Yuxuan Lic9ca0a92016-10-28 14:08:36 -0700136 return low;
137}
138
139static double SearchOfferedLoad(double initial_offered_load,
140 double targeted_cpu_load, Scenario* scenario,
141 bool* success) {
142 std::cerr << "RUNNING SCENARIO: " << scenario->name() << "\n";
143 double current_offered_load = initial_offered_load;
144 double current_cpu_load = GetCpuLoad(scenario, current_offered_load, success);
145 if (current_cpu_load > targeted_cpu_load) {
146 gpr_log(GPR_ERROR, "Initial offered load too high");
147 return -1;
148 }
149
150 while (*success && (current_cpu_load < targeted_cpu_load)) {
151 current_offered_load *= 2;
152 current_cpu_load = GetCpuLoad(scenario, current_offered_load, success);
153 gpr_log(GPR_DEBUG, "Binary Search: current_offered_load %.0f",
154 current_offered_load);
155 }
156
157 double targeted_offered_load =
158 BinarySearch(scenario, targeted_cpu_load, current_offered_load / 2,
159 current_offered_load, success);
160
161 return targeted_offered_load;
162}
163
164static bool QpsDriver() {
165 grpc::string json;
166
167 bool scfile = (FLAGS_scenarios_file != "");
168 bool scjson = (FLAGS_scenarios_json != "");
169 if ((!scfile && !scjson && !FLAGS_quit) ||
170 (scfile && (scjson || FLAGS_quit)) || (scjson && FLAGS_quit)) {
171 gpr_log(GPR_ERROR,
172 "Exactly one of --scenarios_file, --scenarios_json, "
173 "or --quit must be set");
174 abort();
175 }
176
177 if (scfile) {
178 // Read the json data from disk
179 FILE* json_file = fopen(FLAGS_scenarios_file.c_str(), "r");
180 GPR_ASSERT(json_file != NULL);
181 fseek(json_file, 0, SEEK_END);
182 long len = ftell(json_file);
183 char* data = new char[len];
184 fseek(json_file, 0, SEEK_SET);
185 GPR_ASSERT(len == (long)fread(data, 1, len, json_file));
186 fclose(json_file);
187 json = grpc::string(data, data + len);
188 delete[] data;
189 } else if (scjson) {
190 json = FLAGS_scenarios_json.c_str();
191 } else if (FLAGS_quit) {
192 return RunQuit();
193 }
194
195 // Parse into an array of scenarios
196 Scenarios scenarios;
197 ParseJson(json.c_str(), "grpc.testing.Scenarios", &scenarios);
198 bool success = true;
199
200 // Make sure that there is at least some valid scenario here
201 GPR_ASSERT(scenarios.scenarios_size() > 0);
202
203 for (int i = 0; i < scenarios.scenarios_size(); i++) {
204 if (FLAGS_search_param == "") {
205 const Scenario& scenario = scenarios.scenarios(i);
206 RunAndReport(scenario, &success);
207 } else {
208 if (FLAGS_search_param == "offered_load") {
209 Scenario* scenario = scenarios.mutable_scenarios(i);
210 double targeted_offered_load =
211 SearchOfferedLoad(FLAGS_initial_search_value,
212 FLAGS_targeted_cpu_load, scenario, &success);
213 gpr_log(GPR_INFO, "targeted_offered_load %f", targeted_offered_load);
Yuxuan Liac87a462016-11-11 12:05:11 -0800214 GetCpuLoad(scenario, targeted_offered_load, &success);
Yuxuan Lic6c6cc22016-10-06 18:28:05 -0700215 } else {
Yuxuan Lic9ca0a92016-10-28 14:08:36 -0700216 gpr_log(GPR_ERROR, "Unimplemented search param");
Yuxuan Li69c61312016-10-06 17:00:48 -0700217 }
Vijay Paiceb1a7d2016-07-07 11:06:04 -0700218 }
Yuxuan Li60d55f82016-10-21 18:18:29 -0700219 }
Yuxuan Lic9ca0a92016-10-28 14:08:36 -0700220 return success;
221}
Yuxuan Li60d55f82016-10-21 18:18:29 -0700222
Yuxuan Lic9ca0a92016-10-28 14:08:36 -0700223} // namespace testing
Craig Tiller0bda0b32016-03-03 12:51:53 -0800224} // namespace grpc
225
Yuxuan Li49aeb592016-10-06 11:01:50 -0700226int main(int argc, char** argv) {
Craig Tiller0bda0b32016-03-03 12:51:53 -0800227 grpc::testing::InitBenchmark(&argc, &argv, true);
228
Vijay Paiceb1a7d2016-07-07 11:06:04 -0700229 bool ok = grpc::testing::QpsDriver();
Craig Tiller0bda0b32016-03-03 12:51:53 -0800230
Vijay Paiceb1a7d2016-07-07 11:06:04 -0700231 return ok ? 0 : 1;
Craig Tiller0bda0b32016-03-03 12:51:53 -0800232}