qps_json_driver.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #include <memory>
  34. #include <set>
  35. #include <grpc++/impl/codegen/config_protobuf.h>
  36. #include <gflags/gflags.h>
  37. #include <grpc/support/log.h>
  38. #include "test/cpp/qps/driver.h"
  39. #include "test/cpp/qps/parse_json.h"
  40. #include "test/cpp/qps/report.h"
  41. #include "test/cpp/util/benchmark_config.h"
  42. DEFINE_string(scenarios_file, "",
  43. "JSON file containing an array of Scenario objects");
  44. DEFINE_string(scenarios_json, "",
  45. "JSON string containing an array of Scenario objects");
  46. DEFINE_bool(quit, false, "Quit the workers");
  47. DEFINE_bool(
  48. search, false,
  49. "Search for the offered_load value that achieves targeted cpu load");
  50. DEFINE_double(initial_offered_load, 1000.0,
  51. "Set up for intial offered load to start the search");
  52. DEFINE_double(targeted_cpu_load, 99.0, "Targeted cpu load");
  53. DEFINE_double(precision, 500, "Final search result precision");
  54. namespace grpc {
  55. namespace testing {
  56. static std::unique_ptr<ScenarioResult> RunAndReport(const Scenario& scenario,
  57. bool* success) {
  58. std::cerr << "RUNNING SCENARIO: " << scenario.name() << "\n";
  59. auto result =
  60. RunScenario(scenario.client_config(), scenario.num_clients(),
  61. scenario.server_config(), scenario.num_servers(),
  62. scenario.warmup_seconds(), scenario.benchmark_seconds(),
  63. scenario.spawn_local_worker_count());
  64. // Amend the result with scenario config. Eventually we should adjust
  65. // RunScenario contract so we don't need to touch the result here.
  66. result->mutable_scenario()->CopyFrom(scenario);
  67. GetReporter()->ReportQPS(*result);
  68. GetReporter()->ReportQPSPerCore(*result);
  69. GetReporter()->ReportLatency(*result);
  70. GetReporter()->ReportTimes(*result);
  71. GetReporter()->ReportCpuUsage(*result);
  72. for (int i = 0; *success && i < result->client_success_size(); i++) {
  73. *success = result->client_success(i);
  74. }
  75. for (int i = 0; *success && i < result->server_success_size(); i++) {
  76. *success = result->server_success(i);
  77. }
  78. return result;
  79. }
  80. static double GetCpuLoad(Scenario* scenario, double offered_load,
  81. bool* success) {
  82. scenario->mutable_client_config()
  83. ->mutable_load_params()
  84. ->mutable_poisson()
  85. ->set_offered_load(offered_load);
  86. auto result = RunAndReport(*scenario, success);
  87. return result->summary().server_cpu_usage();
  88. }
  89. static double BinarySearch(Scenario* scenario, double targeted_cpu_load,
  90. double low, double high, bool* success) {
  91. while (low <= high - FLAGS_precision) {
  92. double mid = low + (high - low) / 2;
  93. double current_cpu_load = GetCpuLoad(scenario, mid, success);
  94. gpr_log(GPR_DEBUG, "Binary Search: current_offered_load %.0f", mid);
  95. if (!*success) {
  96. gpr_log(GPR_ERROR, "Client/Server Failure");
  97. break;
  98. }
  99. if (targeted_cpu_load < current_cpu_load) {
  100. high = mid - 1;
  101. } else if (targeted_cpu_load > current_cpu_load) {
  102. low = mid + 1;
  103. } else {
  104. high = mid - 1;
  105. }
  106. }
  107. return low;
  108. }
  109. static double SearchOfferedLoad(double initial_offered_load,
  110. double targeted_cpu_load, Scenario* scenario,
  111. bool* success) {
  112. std::cerr << "RUNNING SCENARIO: " << scenario->name() << "\n";
  113. double current_offered_load = initial_offered_load;
  114. double current_cpu_load = GetCpuLoad(scenario, current_offered_load, success);
  115. if (current_cpu_load > targeted_cpu_load) {
  116. gpr_log(GPR_ERROR, "Initial offered load too high");
  117. return -1;
  118. }
  119. while (*success && (current_cpu_load < targeted_cpu_load)) {
  120. current_offered_load *= 2;
  121. current_cpu_load = GetCpuLoad(scenario, current_offered_load, success);
  122. gpr_log(GPR_DEBUG, "Binary Search: current_offered_load %.0f",
  123. current_offered_load);
  124. }
  125. double targeted_offered_load =
  126. BinarySearch(scenario, targeted_cpu_load, current_offered_load / 2,
  127. current_offered_load, success);
  128. return targeted_offered_load;
  129. }
  130. static bool QpsDriver() {
  131. grpc::string json;
  132. bool scfile = (FLAGS_scenarios_file != "");
  133. bool scjson = (FLAGS_scenarios_json != "");
  134. if ((!scfile && !scjson && !FLAGS_quit) ||
  135. (scfile && (scjson || FLAGS_quit)) || (scjson && FLAGS_quit)) {
  136. gpr_log(GPR_ERROR,
  137. "Exactly one of --scenarios_file, --scenarios_json, "
  138. "or --quit must be set");
  139. abort();
  140. }
  141. if (scfile) {
  142. // Read the json data from disk
  143. FILE* json_file = fopen(FLAGS_scenarios_file.c_str(), "r");
  144. GPR_ASSERT(json_file != NULL);
  145. fseek(json_file, 0, SEEK_END);
  146. long len = ftell(json_file);
  147. char* data = new char[len];
  148. fseek(json_file, 0, SEEK_SET);
  149. GPR_ASSERT(len == (long)fread(data, 1, len, json_file));
  150. fclose(json_file);
  151. json = grpc::string(data, data + len);
  152. delete[] data;
  153. } else if (scjson) {
  154. json = FLAGS_scenarios_json.c_str();
  155. } else if (FLAGS_quit) {
  156. return RunQuit();
  157. }
  158. // Parse into an array of scenarios
  159. Scenarios scenarios;
  160. ParseJson(json.c_str(), "grpc.testing.Scenarios", &scenarios);
  161. bool success = true;
  162. // Make sure that there is at least some valid scenario here
  163. GPR_ASSERT(scenarios.scenarios_size() > 0);
  164. for (int i = 0; i < scenarios.scenarios_size(); i++) {
  165. if (!FLAGS_search) {
  166. const Scenario& scenario = scenarios.scenarios(i);
  167. RunAndReport(scenario, &success);
  168. } else {
  169. Scenario* scenario = scenarios.mutable_scenarios(i);
  170. double targeted_offered_load =
  171. SearchOfferedLoad(FLAGS_initial_offered_load, FLAGS_targeted_cpu_load,
  172. scenario, &success);
  173. gpr_log(GPR_INFO, "targeted_offered_load %f", targeted_offered_load);
  174. }
  175. }
  176. return success;
  177. }
  178. } // namespace testing
  179. } // namespace grpc
  180. int main(int argc, char** argv) {
  181. grpc::testing::InitBenchmark(&argc, &argv, true);
  182. bool ok = grpc::testing::QpsDriver();
  183. return ok ? 0 : 1;
  184. }