report.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  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
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  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.
  16. *
  17. */
  18. #include "test/cpp/qps/report.h"
  19. #include <fstream>
  20. #include <grpc/support/log.h>
  21. #include <grpcpp/client_context.h>
  22. #include "src/cpp/util/core_stats.h"
  23. #include "src/proto/grpc/testing/report_qps_scenario_service.grpc.pb.h"
  24. #include "test/cpp/qps/driver.h"
  25. #include "test/cpp/qps/parse_json.h"
  26. #include "test/cpp/qps/stats.h"
  27. namespace grpc {
  28. namespace testing {
  29. void CompositeReporter::add(std::unique_ptr<Reporter> reporter) {
  30. reporters_.emplace_back(std::move(reporter));
  31. }
  32. void CompositeReporter::ReportQPS(const ScenarioResult& result) {
  33. for (size_t i = 0; i < reporters_.size(); ++i) {
  34. reporters_[i]->ReportQPS(result);
  35. }
  36. }
  37. void CompositeReporter::ReportQPSPerCore(const ScenarioResult& result) {
  38. for (size_t i = 0; i < reporters_.size(); ++i) {
  39. reporters_[i]->ReportQPSPerCore(result);
  40. }
  41. }
  42. void CompositeReporter::ReportLatency(const ScenarioResult& result) {
  43. for (size_t i = 0; i < reporters_.size(); ++i) {
  44. reporters_[i]->ReportLatency(result);
  45. }
  46. }
  47. void CompositeReporter::ReportTimes(const ScenarioResult& result) {
  48. for (size_t i = 0; i < reporters_.size(); ++i) {
  49. reporters_[i]->ReportTimes(result);
  50. }
  51. }
  52. void CompositeReporter::ReportCpuUsage(const ScenarioResult& result) {
  53. for (size_t i = 0; i < reporters_.size(); ++i) {
  54. reporters_[i]->ReportCpuUsage(result);
  55. }
  56. }
  57. void CompositeReporter::ReportPollCount(const ScenarioResult& result) {
  58. for (size_t i = 0; i < reporters_.size(); ++i) {
  59. reporters_[i]->ReportPollCount(result);
  60. }
  61. }
  62. void CompositeReporter::ReportQueriesPerCpuSec(const ScenarioResult& result) {
  63. for (size_t i = 0; i < reporters_.size(); ++i) {
  64. reporters_[i]->ReportQueriesPerCpuSec(result);
  65. }
  66. }
  67. void GprLogReporter::ReportQPS(const ScenarioResult& result) {
  68. gpr_log(GPR_INFO, "QPS: %.1f", result.summary().qps());
  69. if (result.summary().failed_requests_per_second() > 0) {
  70. gpr_log(GPR_INFO, "failed requests/second: %.1f",
  71. result.summary().failed_requests_per_second());
  72. gpr_log(GPR_INFO, "successful requests/second: %.1f",
  73. result.summary().successful_requests_per_second());
  74. }
  75. for (int i = 0; i < result.client_stats_size(); i++) {
  76. if (result.client_stats(i).has_core_stats()) {
  77. ReportCoreStats("CLIENT", i, result.client_stats(i).core_stats());
  78. }
  79. }
  80. for (int i = 0; i < result.server_stats_size(); i++) {
  81. if (result.server_stats(i).has_core_stats()) {
  82. ReportCoreStats("SERVER", i, result.server_stats(i).core_stats());
  83. }
  84. }
  85. }
  86. void GprLogReporter::ReportCoreStats(const char* name, int idx,
  87. const grpc::core::Stats& stats) {
  88. grpc_stats_data data;
  89. ProtoToCoreStats(stats, &data);
  90. for (int i = 0; i < GRPC_STATS_COUNTER_COUNT; i++) {
  91. gpr_log(GPR_DEBUG, "%s[%d].%s = %" PRIdPTR, name, idx,
  92. grpc_stats_counter_name[i], data.counters[i]);
  93. }
  94. for (int i = 0; i < GRPC_STATS_HISTOGRAM_COUNT; i++) {
  95. gpr_log(GPR_DEBUG, "%s[%d].%s = %.1lf/%.1lf/%.1lf (50/95/99%%-ile)", name,
  96. idx, grpc_stats_histogram_name[i],
  97. grpc_stats_histo_percentile(
  98. &data, static_cast<grpc_stats_histograms>(i), 50),
  99. grpc_stats_histo_percentile(
  100. &data, static_cast<grpc_stats_histograms>(i), 95),
  101. grpc_stats_histo_percentile(
  102. &data, static_cast<grpc_stats_histograms>(i), 99));
  103. }
  104. }
  105. void GprLogReporter::ReportQPSPerCore(const ScenarioResult& result) {
  106. gpr_log(GPR_INFO, "QPS: %.1f (%.1f/server core)", result.summary().qps(),
  107. result.summary().qps_per_server_core());
  108. }
  109. void GprLogReporter::ReportLatency(const ScenarioResult& result) {
  110. gpr_log(GPR_INFO,
  111. "Latencies (50/90/95/99/99.9%%-ile): %.1f/%.1f/%.1f/%.1f/%.1f us",
  112. result.summary().latency_50() / 1000,
  113. result.summary().latency_90() / 1000,
  114. result.summary().latency_95() / 1000,
  115. result.summary().latency_99() / 1000,
  116. result.summary().latency_999() / 1000);
  117. }
  118. void GprLogReporter::ReportTimes(const ScenarioResult& result) {
  119. gpr_log(GPR_INFO, "Server system time: %.2f%%",
  120. result.summary().server_system_time());
  121. gpr_log(GPR_INFO, "Server user time: %.2f%%",
  122. result.summary().server_user_time());
  123. gpr_log(GPR_INFO, "Client system time: %.2f%%",
  124. result.summary().client_system_time());
  125. gpr_log(GPR_INFO, "Client user time: %.2f%%",
  126. result.summary().client_user_time());
  127. }
  128. void GprLogReporter::ReportCpuUsage(const ScenarioResult& result) {
  129. gpr_log(GPR_INFO, "Server CPU usage: %.2f%%",
  130. result.summary().server_cpu_usage());
  131. }
  132. void GprLogReporter::ReportPollCount(const ScenarioResult& result) {
  133. gpr_log(GPR_INFO, "Client Polls per Request: %.2f",
  134. result.summary().client_polls_per_request());
  135. gpr_log(GPR_INFO, "Server Polls per Request: %.2f",
  136. result.summary().server_polls_per_request());
  137. }
  138. void GprLogReporter::ReportQueriesPerCpuSec(const ScenarioResult& result) {
  139. gpr_log(GPR_INFO, "Server Queries/CPU-sec: %.2f",
  140. result.summary().server_queries_per_cpu_sec());
  141. gpr_log(GPR_INFO, "Client Queries/CPU-sec: %.2f",
  142. result.summary().client_queries_per_cpu_sec());
  143. }
  144. void JsonReporter::ReportQPS(const ScenarioResult& result) {
  145. std::string json_string =
  146. SerializeJson(result, "type.googleapis.com/grpc.testing.ScenarioResult");
  147. std::ofstream output_file(report_file_);
  148. output_file << json_string;
  149. output_file.close();
  150. }
  151. void JsonReporter::ReportQPSPerCore(const ScenarioResult& /*result*/) {
  152. // NOP - all reporting is handled by ReportQPS.
  153. }
  154. void JsonReporter::ReportLatency(const ScenarioResult& /*result*/) {
  155. // NOP - all reporting is handled by ReportQPS.
  156. }
  157. void JsonReporter::ReportTimes(const ScenarioResult& /*result*/) {
  158. // NOP - all reporting is handled by ReportQPS.
  159. }
  160. void JsonReporter::ReportCpuUsage(const ScenarioResult& /*result*/) {
  161. // NOP - all reporting is handled by ReportQPS.
  162. }
  163. void JsonReporter::ReportPollCount(const ScenarioResult& /*result*/) {
  164. // NOP - all reporting is handled by ReportQPS.
  165. }
  166. void JsonReporter::ReportQueriesPerCpuSec(const ScenarioResult& /*result*/) {
  167. // NOP - all reporting is handled by ReportQPS.
  168. }
  169. void RpcReporter::ReportQPS(const ScenarioResult& result) {
  170. grpc::ClientContext context;
  171. grpc::Status status;
  172. Void phony;
  173. gpr_log(GPR_INFO, "RPC reporter sending scenario result to server");
  174. status = stub_->ReportScenario(&context, result, &phony);
  175. if (status.ok()) {
  176. gpr_log(GPR_INFO, "RpcReporter report RPC success!");
  177. } else {
  178. gpr_log(GPR_ERROR, "RpcReporter report RPC: code: %d. message: %s",
  179. status.error_code(), status.error_message().c_str());
  180. }
  181. }
  182. void RpcReporter::ReportQPSPerCore(const ScenarioResult& /*result*/) {
  183. // NOP - all reporting is handled by ReportQPS.
  184. }
  185. void RpcReporter::ReportLatency(const ScenarioResult& /*result*/) {
  186. // NOP - all reporting is handled by ReportQPS.
  187. }
  188. void RpcReporter::ReportTimes(const ScenarioResult& /*result*/) {
  189. // NOP - all reporting is handled by ReportQPS.
  190. }
  191. void RpcReporter::ReportCpuUsage(const ScenarioResult& /*result*/) {
  192. // NOP - all reporting is handled by ReportQPS.
  193. }
  194. void RpcReporter::ReportPollCount(const ScenarioResult& /*result*/) {
  195. // NOP - all reporting is handled by ReportQPS.
  196. }
  197. void RpcReporter::ReportQueriesPerCpuSec(const ScenarioResult& /*result*/) {
  198. // NOP - all reporting is handled by ReportQPS.
  199. }
  200. } // namespace testing
  201. } // namespace grpc