Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015-2016 gRPC authors. |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
ncteisen | 24e8207 | 2017-06-14 19:56:29 -0700 | [diff] [blame] | 19 | #include <fstream> |
ncteisen | dc1b51e | 2017-06-19 09:00:25 -0700 | [diff] [blame] | 20 | #include <iostream> |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 21 | #include <memory> |
| 22 | #include <set> |
| 23 | |
yang-g | 17487f9 | 2016-06-03 15:21:15 -0700 | [diff] [blame] | 24 | #include <grpc++/impl/codegen/config_protobuf.h> |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 25 | |
| 26 | #include <gflags/gflags.h> |
| 27 | #include <grpc/support/log.h> |
| 28 | |
Nicolas "Pixel" Noble | 6f7dcd0 | 2017-03-27 18:51:55 +0200 | [diff] [blame] | 29 | #include "test/cpp/qps/benchmark_config.h" |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 30 | #include "test/cpp/qps/driver.h" |
Craig Tiller | 3ab2fe0 | 2016-04-11 20:11:18 -0700 | [diff] [blame] | 31 | #include "test/cpp/qps/parse_json.h" |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 32 | #include "test/cpp/qps/report.h" |
Vijay Pai | 088112f | 2017-08-24 10:42:27 -0700 | [diff] [blame] | 33 | #include "test/cpp/qps/server.h" |
Guantao Liu | abe3cf5 | 2017-07-12 17:36:30 -0700 | [diff] [blame] | 34 | #include "test/cpp/util/test_config.h" |
Guantao Liu | ac9fdfb | 2017-07-14 17:33:19 -0700 | [diff] [blame] | 35 | #include "test/cpp/util/test_credentials_provider.h" |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 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"); |
vjpai | 29089c7 | 2016-04-20 12:38:16 -0700 | [diff] [blame] | 41 | DEFINE_bool(quit, false, "Quit the workers"); |
Yuxuan Li | c6c6cc2 | 2016-10-06 18:28:05 -0700 | [diff] [blame] | 42 | DEFINE_string(search_param, "", |
| 43 | "The parameter, whose value is to be searched for to achieve " |
Yuxuan Li | 60d55f8 | 2016-10-21 18:18:29 -0700 | [diff] [blame] | 44 | "targeted cpu load. For now, we have 'offered_load'. Later, " |
| 45 | "'num_channels', 'num_outstanding_requests', etc. shall be " |
| 46 | "added."); |
Yuxuan Li | c6c6cc2 | 2016-10-06 18:28:05 -0700 | [diff] [blame] | 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])"); |
Yuxuan Li | 60d55f8 | 2016-10-21 18:18:29 -0700 | [diff] [blame] | 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."); |
Yuxuan Li | 58977f1 | 2016-10-04 14:04:39 -0700 | [diff] [blame] | 59 | |
Alexander Polcyn | 4873d30 | 2016-12-19 15:58:15 -0800 | [diff] [blame] | 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."); |
Alexander Polcyn | 4873d30 | 2016-12-19 15:58:15 -0800 | [diff] [blame] | 63 | |
ncteisen | dc1b51e | 2017-06-19 09:00:25 -0700 | [diff] [blame] | 64 | DEFINE_string(json_file_out, "", "File to write the JSON output to."); |
ncteisen | 24e8207 | 2017-06-14 19:56:29 -0700 | [diff] [blame] | 65 | |
Guantao Liu | ac9fdfb | 2017-07-14 17:33:19 -0700 | [diff] [blame] | 66 | DEFINE_string(credential_type, grpc::testing::kInsecureCredentialsType, |
Guantao Liu | abe3cf5 | 2017-07-12 17:36:30 -0700 | [diff] [blame] | 67 | "Credential type for communication with workers"); |
Vijay Pai | 088112f | 2017-08-24 10:42:27 -0700 | [diff] [blame] | 68 | DEFINE_bool(run_inproc, false, "Perform an in-process transport test"); |
Guantao Liu | abe3cf5 | 2017-07-12 17:36:30 -0700 | [diff] [blame] | 69 | |
Yuxuan Li | c9ca0a9 | 2016-10-28 14:08:36 -0700 | [diff] [blame] | 70 | namespace grpc { |
| 71 | namespace testing { |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 72 | |
Yuxuan Li | c9ca0a9 | 2016-10-28 14:08:36 -0700 | [diff] [blame] | 73 | static std::unique_ptr<ScenarioResult> RunAndReport(const Scenario& scenario, |
| 74 | bool* success) { |
| 75 | std::cerr << "RUNNING SCENARIO: " << scenario.name() << "\n"; |
Alexander Polcyn | 62a7ca8 | 2017-01-12 11:32:15 -0800 | [diff] [blame] | 76 | 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 Pai | 088112f | 2017-08-24 10:42:27 -0700 | [diff] [blame] | 80 | !FLAGS_run_inproc ? scenario.spawn_local_worker_count() : -2, |
| 81 | FLAGS_qps_server_target_override, FLAGS_credential_type, |
| 82 | FLAGS_run_inproc); |
Yuxuan Li | 58977f1 | 2016-10-04 14:04:39 -0700 | [diff] [blame] | 83 | |
Yuxuan Li | c9ca0a9 | 2016-10-28 14:08:36 -0700 | [diff] [blame] | 84 | // 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 Li | 58977f1 | 2016-10-04 14:04:39 -0700 | [diff] [blame] | 87 | |
Yuxuan Li | c9ca0a9 | 2016-10-28 14:08:36 -0700 | [diff] [blame] | 88 | GetReporter()->ReportQPS(*result); |
| 89 | GetReporter()->ReportQPSPerCore(*result); |
| 90 | GetReporter()->ReportLatency(*result); |
| 91 | GetReporter()->ReportTimes(*result); |
| 92 | GetReporter()->ReportCpuUsage(*result); |
Yuxuan Li | 999ac15 | 2017-05-03 21:36:36 -0700 | [diff] [blame] | 93 | GetReporter()->ReportPollCount(*result); |
Guantao Liu | abe3cf5 | 2017-07-12 17:36:30 -0700 | [diff] [blame] | 94 | GetReporter()->ReportQueriesPerCpuSec(*result); |
Yuxuan Li | 58977f1 | 2016-10-04 14:04:39 -0700 | [diff] [blame] | 95 | |
Yuxuan Li | c9ca0a9 | 2016-10-28 14:08:36 -0700 | [diff] [blame] | 96 | 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 Li | 58977f1 | 2016-10-04 14:04:39 -0700 | [diff] [blame] | 101 | } |
| 102 | |
ncteisen | 24e8207 | 2017-06-14 19:56:29 -0700 | [diff] [blame] | 103 | 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 Li | c9ca0a9 | 2016-10-28 14:08:36 -0700 | [diff] [blame] | 110 | return result; |
| 111 | } |
| 112 | |
| 113 | static 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 | |
| 123 | static 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 Li | 49aeb59 | 2016-10-06 11:01:50 -0700 | [diff] [blame] | 138 | } |
Yuxuan Li | 58977f1 | 2016-10-04 14:04:39 -0700 | [diff] [blame] | 139 | |
Yuxuan Li | c9ca0a9 | 2016-10-28 14:08:36 -0700 | [diff] [blame] | 140 | return low; |
| 141 | } |
| 142 | |
| 143 | static 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 | |
| 168 | static 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 Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 184 | GPR_ASSERT(json_file != nullptr); |
Yuxuan Li | c9ca0a9 | 2016-10-28 14:08:36 -0700 | [diff] [blame] | 185 | 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 Liu | ac9fdfb | 2017-07-14 17:33:19 -0700 | [diff] [blame] | 196 | return RunQuit(FLAGS_credential_type); |
Yuxuan Li | c9ca0a9 | 2016-10-28 14:08:36 -0700 | [diff] [blame] | 197 | } |
| 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 Li | ac87a46 | 2016-11-11 12:05:11 -0800 | [diff] [blame] | 218 | GetCpuLoad(scenario, targeted_offered_load, &success); |
Yuxuan Li | c6c6cc2 | 2016-10-06 18:28:05 -0700 | [diff] [blame] | 219 | } else { |
Yuxuan Li | c9ca0a9 | 2016-10-28 14:08:36 -0700 | [diff] [blame] | 220 | gpr_log(GPR_ERROR, "Unimplemented search param"); |
Yuxuan Li | 69c6131 | 2016-10-06 17:00:48 -0700 | [diff] [blame] | 221 | } |
Vijay Pai | ceb1a7d | 2016-07-07 11:06:04 -0700 | [diff] [blame] | 222 | } |
Yuxuan Li | 60d55f8 | 2016-10-21 18:18:29 -0700 | [diff] [blame] | 223 | } |
Yuxuan Li | c9ca0a9 | 2016-10-28 14:08:36 -0700 | [diff] [blame] | 224 | return success; |
| 225 | } |
Yuxuan Li | 60d55f8 | 2016-10-21 18:18:29 -0700 | [diff] [blame] | 226 | |
Yuxuan Li | c9ca0a9 | 2016-10-28 14:08:36 -0700 | [diff] [blame] | 227 | } // namespace testing |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 228 | } // namespace grpc |
| 229 | |
Yuxuan Li | 49aeb59 | 2016-10-06 11:01:50 -0700 | [diff] [blame] | 230 | int main(int argc, char** argv) { |
Guantao Liu | abe3cf5 | 2017-07-12 17:36:30 -0700 | [diff] [blame] | 231 | grpc::testing::InitTest(&argc, &argv, true); |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 232 | |
Vijay Pai | ceb1a7d | 2016-07-07 11:06:04 -0700 | [diff] [blame] | 233 | bool ok = grpc::testing::QpsDriver(); |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 234 | |
Vijay Pai | ceb1a7d | 2016-07-07 11:06:04 -0700 | [diff] [blame] | 235 | return ok ? 0 : 1; |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 236 | } |