Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 1 | /* |
| 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-g | 90f43d4 | 2017-03-14 10:58:44 -0700 | [diff] [blame] | 34 | #include <iostream> |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 35 | #include <memory> |
| 36 | #include <set> |
| 37 | |
yang-g | 17487f9 | 2016-06-03 15:21:15 -0700 | [diff] [blame] | 38 | #include <grpc++/impl/codegen/config_protobuf.h> |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 39 | |
| 40 | #include <gflags/gflags.h> |
| 41 | #include <grpc/support/log.h> |
| 42 | |
Nicolas "Pixel" Noble | 6f7dcd0 | 2017-03-27 18:51:55 +0200 | [diff] [blame] | 43 | #include "test/cpp/qps/benchmark_config.h" |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 44 | #include "test/cpp/qps/driver.h" |
Craig Tiller | 3ab2fe0 | 2016-04-11 20:11:18 -0700 | [diff] [blame] | 45 | #include "test/cpp/qps/parse_json.h" |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 46 | #include "test/cpp/qps/report.h" |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 47 | |
| 48 | DEFINE_string(scenarios_file, "", |
| 49 | "JSON file containing an array of Scenario objects"); |
| 50 | DEFINE_string(scenarios_json, "", |
| 51 | "JSON string containing an array of Scenario objects"); |
vjpai | 29089c7 | 2016-04-20 12:38:16 -0700 | [diff] [blame] | 52 | DEFINE_bool(quit, false, "Quit the workers"); |
Yuxuan Li | c6c6cc2 | 2016-10-06 18:28:05 -0700 | [diff] [blame] | 53 | DEFINE_string(search_param, "", |
| 54 | "The parameter, whose value is to be searched for to achieve " |
Yuxuan Li | 60d55f8 | 2016-10-21 18:18:29 -0700 | [diff] [blame] | 55 | "targeted cpu load. For now, we have 'offered_load'. Later, " |
| 56 | "'num_channels', 'num_outstanding_requests', etc. shall be " |
| 57 | "added."); |
Yuxuan Li | c6c6cc2 | 2016-10-06 18:28:05 -0700 | [diff] [blame] | 58 | DEFINE_double( |
| 59 | initial_search_value, 0.0, |
| 60 | "initial parameter value to start the search with (i.e. lower bound)"); |
| 61 | DEFINE_double(targeted_cpu_load, 70.0, |
| 62 | "Targeted cpu load (unit: %, range [0,100])"); |
Yuxuan Li | 60d55f8 | 2016-10-21 18:18:29 -0700 | [diff] [blame] | 63 | DEFINE_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."); |
| 66 | DEFINE_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 Li | 58977f1 | 2016-10-04 14:04:39 -0700 | [diff] [blame] | 70 | |
Alexander Polcyn | 4873d30 | 2016-12-19 15:58:15 -0800 | [diff] [blame] | 71 | DEFINE_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 Polcyn | 4873d30 | 2016-12-19 15:58:15 -0800 | [diff] [blame] | 74 | |
Yuxuan Li | c9ca0a9 | 2016-10-28 14:08:36 -0700 | [diff] [blame] | 75 | namespace grpc { |
| 76 | namespace testing { |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 77 | |
Yuxuan Li | c9ca0a9 | 2016-10-28 14:08:36 -0700 | [diff] [blame] | 78 | static std::unique_ptr<ScenarioResult> RunAndReport(const Scenario& scenario, |
| 79 | bool* success) { |
| 80 | std::cerr << "RUNNING SCENARIO: " << scenario.name() << "\n"; |
Alexander Polcyn | 62a7ca8 | 2017-01-12 11:32:15 -0800 | [diff] [blame] | 81 | 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 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 | // 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 Li | 58977f1 | 2016-10-04 14:04:39 -0700 | [diff] [blame] | 91 | |
Yuxuan Li | c9ca0a9 | 2016-10-28 14:08:36 -0700 | [diff] [blame] | 92 | GetReporter()->ReportQPS(*result); |
| 93 | GetReporter()->ReportQPSPerCore(*result); |
| 94 | GetReporter()->ReportLatency(*result); |
| 95 | GetReporter()->ReportTimes(*result); |
| 96 | GetReporter()->ReportCpuUsage(*result); |
Yuxuan Li | 999ac15 | 2017-05-03 21:36:36 -0700 | [diff] [blame] | 97 | GetReporter()->ReportPollCount(*result); |
Yuxuan Li | 58977f1 | 2016-10-04 14:04:39 -0700 | [diff] [blame] | 98 | |
Yuxuan Li | c9ca0a9 | 2016-10-28 14:08:36 -0700 | [diff] [blame] | 99 | 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 Li | 58977f1 | 2016-10-04 14:04:39 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Yuxuan Li | c9ca0a9 | 2016-10-28 14:08:36 -0700 | [diff] [blame] | 106 | return result; |
| 107 | } |
| 108 | |
| 109 | static 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 | |
| 119 | static 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 Li | 49aeb59 | 2016-10-06 11:01:50 -0700 | [diff] [blame] | 134 | } |
Yuxuan Li | 58977f1 | 2016-10-04 14:04:39 -0700 | [diff] [blame] | 135 | |
Yuxuan Li | c9ca0a9 | 2016-10-28 14:08:36 -0700 | [diff] [blame] | 136 | return low; |
| 137 | } |
| 138 | |
| 139 | static 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 | |
| 164 | static 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 Li | ac87a46 | 2016-11-11 12:05:11 -0800 | [diff] [blame] | 214 | GetCpuLoad(scenario, targeted_offered_load, &success); |
Yuxuan Li | c6c6cc2 | 2016-10-06 18:28:05 -0700 | [diff] [blame] | 215 | } else { |
Yuxuan Li | c9ca0a9 | 2016-10-28 14:08:36 -0700 | [diff] [blame] | 216 | gpr_log(GPR_ERROR, "Unimplemented search param"); |
Yuxuan Li | 69c6131 | 2016-10-06 17:00:48 -0700 | [diff] [blame] | 217 | } |
Vijay Pai | ceb1a7d | 2016-07-07 11:06:04 -0700 | [diff] [blame] | 218 | } |
Yuxuan Li | 60d55f8 | 2016-10-21 18:18:29 -0700 | [diff] [blame] | 219 | } |
Yuxuan Li | c9ca0a9 | 2016-10-28 14:08:36 -0700 | [diff] [blame] | 220 | return success; |
| 221 | } |
Yuxuan Li | 60d55f8 | 2016-10-21 18:18:29 -0700 | [diff] [blame] | 222 | |
Yuxuan Li | c9ca0a9 | 2016-10-28 14:08:36 -0700 | [diff] [blame] | 223 | } // namespace testing |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 224 | } // namespace grpc |
| 225 | |
Yuxuan Li | 49aeb59 | 2016-10-06 11:01:50 -0700 | [diff] [blame] | 226 | int main(int argc, char** argv) { |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 227 | grpc::testing::InitBenchmark(&argc, &argv, true); |
| 228 | |
Vijay Pai | ceb1a7d | 2016-07-07 11:06:04 -0700 | [diff] [blame] | 229 | bool ok = grpc::testing::QpsDriver(); |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 230 | |
Vijay Pai | ceb1a7d | 2016-07-07 11:06:04 -0700 | [diff] [blame] | 231 | return ok ? 0 : 1; |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 232 | } |