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 | |
| 34 | #include <memory> |
| 35 | #include <set> |
| 36 | |
Craig Tiller | 3ab2fe0 | 2016-04-11 20:11:18 -0700 | [diff] [blame] | 37 | #include <grpc++/support/config_protobuf.h> |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 38 | |
| 39 | #include <gflags/gflags.h> |
| 40 | #include <grpc/support/log.h> |
| 41 | |
| 42 | #include "test/cpp/qps/driver.h" |
Craig Tiller | 3ab2fe0 | 2016-04-11 20:11:18 -0700 | [diff] [blame] | 43 | #include "test/cpp/qps/parse_json.h" |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 44 | #include "test/cpp/qps/report.h" |
| 45 | #include "test/cpp/util/benchmark_config.h" |
| 46 | |
| 47 | DEFINE_string(scenarios_file, "", |
| 48 | "JSON file containing an array of Scenario objects"); |
| 49 | DEFINE_string(scenarios_json, "", |
| 50 | "JSON string containing an array of Scenario objects"); |
vjpai | 29089c7 | 2016-04-20 12:38:16 -0700 | [diff] [blame] | 51 | DEFINE_bool(quit, false, "Quit the workers"); |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 52 | |
| 53 | namespace grpc { |
| 54 | namespace testing { |
| 55 | |
| 56 | static void QpsDriver() { |
| 57 | grpc::string json; |
| 58 | |
vjpai | 29089c7 | 2016-04-20 12:38:16 -0700 | [diff] [blame] | 59 | bool scfile = (FLAGS_scenarios_file != ""); |
| 60 | bool scjson = (FLAGS_scenarios_json != ""); |
| 61 | if ((!scfile && !scjson && !FLAGS_quit) || |
Vijay Pai | 8909428 | 2016-04-20 14:21:30 -0700 | [diff] [blame] | 62 | (scfile && (scjson || FLAGS_quit)) || (scjson && FLAGS_quit)) { |
| 63 | gpr_log(GPR_ERROR, |
| 64 | "Exactly one of --scenarios_file, --scenarios_json, " |
| 65 | "or --quit must be set"); |
vjpai | 29089c7 | 2016-04-20 12:38:16 -0700 | [diff] [blame] | 66 | abort(); |
| 67 | } |
| 68 | |
| 69 | if (scfile) { |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 70 | // Read the json data from disk |
| 71 | FILE *json_file = fopen(FLAGS_scenarios_file.c_str(), "r"); |
| 72 | GPR_ASSERT(json_file != NULL); |
| 73 | fseek(json_file, 0, SEEK_END); |
| 74 | long len = ftell(json_file); |
| 75 | char *data = new char[len]; |
| 76 | fseek(json_file, 0, SEEK_SET); |
| 77 | GPR_ASSERT(len == (long)fread(data, 1, len, json_file)); |
| 78 | fclose(json_file); |
| 79 | json = grpc::string(data, data + len); |
| 80 | delete[] data; |
vjpai | 29089c7 | 2016-04-20 12:38:16 -0700 | [diff] [blame] | 81 | } else if (scjson) { |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 82 | json = FLAGS_scenarios_json.c_str(); |
vjpai | 29089c7 | 2016-04-20 12:38:16 -0700 | [diff] [blame] | 83 | } else if (FLAGS_quit) { |
| 84 | RunQuit(); |
| 85 | return; |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // Parse into an array of scenarios |
| 89 | Scenarios scenarios; |
Craig Tiller | 8f98e0b | 2016-04-11 23:39:12 -0700 | [diff] [blame] | 90 | ParseJson(json.c_str(), "grpc.testing.Scenarios", &scenarios); |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 91 | |
Vijay Pai | dfdfe26 | 2016-04-21 11:38:31 -0700 | [diff] [blame] | 92 | // Make sure that there is at least some valid scenario here |
| 93 | GPR_ASSERT(scenarios.scenarios_size() > 0); |
| 94 | |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 95 | for (int i = 0; i < scenarios.scenarios_size(); i++) { |
| 96 | const Scenario &scenario = scenarios.scenarios(i); |
| 97 | std::cerr << "RUNNING SCENARIO: " << scenario.name() << "\n"; |
Jan Tattermusch | 969ffaf | 2016-04-14 12:59:42 -0700 | [diff] [blame] | 98 | auto result = |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 99 | RunScenario(scenario.client_config(), scenario.num_clients(), |
| 100 | scenario.server_config(), scenario.num_servers(), |
| 101 | scenario.warmup_seconds(), scenario.benchmark_seconds(), |
| 102 | scenario.spawn_local_worker_count()); |
| 103 | |
Jan Tattermusch | 969ffaf | 2016-04-14 12:59:42 -0700 | [diff] [blame] | 104 | // Amend the result with scenario config. Eventually we should adjust |
| 105 | // RunScenario contract so we don't need to touch the result here. |
| 106 | result->mutable_scenario()->CopyFrom(scenario); |
| 107 | |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 108 | GetReporter()->ReportQPS(*result); |
| 109 | GetReporter()->ReportQPSPerCore(*result); |
| 110 | GetReporter()->ReportLatency(*result); |
| 111 | GetReporter()->ReportTimes(*result); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | } // namespace testing |
| 116 | } // namespace grpc |
| 117 | |
| 118 | int main(int argc, char **argv) { |
| 119 | grpc::testing::InitBenchmark(&argc, &argv, true); |
| 120 | |
| 121 | grpc::testing::QpsDriver(); |
| 122 | |
| 123 | return 0; |
| 124 | } |