diff --git a/test/cpp/qps/async_streaming_ping_pong_test.cc b/test/cpp/qps/async_streaming_ping_pong_test.cc index 8cb0949bb4d5d98e58f0fa7e1f5b9ea27af0cbd7..dc972443ff6966c4fe7ae0a732408919fea8c8b1 100644 --- a/test/cpp/qps/async_streaming_ping_pong_test.cc +++ b/test/cpp/qps/async_streaming_ping_pong_test.cc @@ -47,8 +47,7 @@ namespace testing { static const int WARMUP = 5; static const int BENCHMARK = 10; -static void RunAsyncStreamingPingPong( - const std::vector<std::unique_ptr<Reporter> >& reporters) { +static void RunAsyncStreamingPingPong() { gpr_log(GPR_INFO, "Running Async Streaming Ping Pong"); ClientConfig client_config; @@ -68,10 +67,8 @@ static void RunAsyncStreamingPingPong( const auto result = RunScenario(client_config, 1, server_config, 1, WARMUP, BENCHMARK, -2); - for (const auto& reporter : reporters) { - reporter->ReportQPS(result); - reporter->ReportLatency(result); - } + GetReporter()->ReportQPS(result); + GetReporter()->ReportLatency(result); } } // namespace testing @@ -79,9 +76,8 @@ static void RunAsyncStreamingPingPong( int main(int argc, char** argv) { grpc::testing::InitBenchmark(&argc, &argv, true); - const auto& reporters = grpc::testing::InitBenchmarkReporters(); signal(SIGPIPE, SIG_IGN); - grpc::testing::RunAsyncStreamingPingPong(reporters); + grpc::testing::RunAsyncStreamingPingPong(); return 0; } diff --git a/test/cpp/qps/async_unary_ping_pong_test.cc b/test/cpp/qps/async_unary_ping_pong_test.cc index 997cbced302a58b2c244470965484aeb12ea33aa..05bc08a3200a026c0c9d33d3ed8d9e65d34ee49a 100644 --- a/test/cpp/qps/async_unary_ping_pong_test.cc +++ b/test/cpp/qps/async_unary_ping_pong_test.cc @@ -47,8 +47,7 @@ namespace testing { static const int WARMUP = 5; static const int BENCHMARK = 10; -static void RunAsyncUnaryPingPong( - const std::vector<std::unique_ptr<Reporter> >& reporters) { +static void RunAsyncUnaryPingPong() { gpr_log(GPR_INFO, "Running Async Unary Ping Pong"); ClientConfig client_config; @@ -68,19 +67,16 @@ static void RunAsyncUnaryPingPong( const auto result = RunScenario(client_config, 1, server_config, 1, WARMUP, BENCHMARK, -2); - for (const auto& reporter : reporters) { - reporter->ReportQPS(result); - reporter->ReportLatency(result); - } + GetReporter()->ReportQPS(result); + GetReporter()->ReportLatency(result); } } // namespace testing } // namespace grpc int main(int argc, char** argv) { grpc::testing::InitBenchmark(&argc, &argv, true); - const auto& reporters = grpc::testing::InitBenchmarkReporters(); signal(SIGPIPE, SIG_IGN); - grpc::testing::RunAsyncUnaryPingPong(reporters); + grpc::testing::RunAsyncUnaryPingPong(); return 0; } diff --git a/test/cpp/qps/qps_driver.cc b/test/cpp/qps/qps_driver.cc index 1f17424fad0dce3a19393ce311d3829855723a9e..2e491eeb05d66e3ae08dffbd536270010b3a05d4 100644 --- a/test/cpp/qps/qps_driver.cc +++ b/test/cpp/qps/qps_driver.cc @@ -74,8 +74,7 @@ using grpc::testing::ResourceUsage; namespace grpc { namespace testing { -static void QpsDriver( - const std::vector<std::unique_ptr<Reporter> >& reporters) { +static void QpsDriver() { RpcType rpc_type; GPR_ASSERT(RpcType_Parse(FLAGS_rpc_type, &rpc_type)); @@ -112,12 +111,10 @@ static void QpsDriver( client_config, FLAGS_num_clients, server_config, FLAGS_num_servers, FLAGS_warmup_seconds, FLAGS_benchmark_seconds, FLAGS_local_workers); - for (const auto& reporter : reporters) { - reporter->ReportQPS(result); - reporter->ReportQPSPerCore(result, server_config); - reporter->ReportLatency(result); - reporter->ReportTimes(result); - } + GetReporter()->ReportQPS(result); + GetReporter()->ReportQPSPerCore(result, server_config); + GetReporter()->ReportLatency(result); + GetReporter()->ReportTimes(result); } } // namespace testing @@ -125,10 +122,9 @@ static void QpsDriver( int main(int argc, char** argv) { grpc::testing::InitBenchmark(&argc, &argv, true); - const auto& reporters = grpc::testing::InitBenchmarkReporters(); signal(SIGPIPE, SIG_IGN); - grpc::testing::QpsDriver(reporters); + grpc::testing::QpsDriver(); return 0; } diff --git a/test/cpp/qps/qps_test.cc b/test/cpp/qps/qps_test.cc index 92940795a7a78e6707bde8364dcb696fc211a610..03c9c2c4238ad97311021536bb0c6b9702770583 100644 --- a/test/cpp/qps/qps_test.cc +++ b/test/cpp/qps/qps_test.cc @@ -47,7 +47,7 @@ namespace testing { static const int WARMUP = 5; static const int BENCHMARK = 10; -static void RunQPS(const std::vector<std::unique_ptr<Reporter> >& reporters) { +static void RunQPS() { gpr_log(GPR_INFO, "Running QPS test"); ClientConfig client_config; @@ -67,10 +67,8 @@ static void RunQPS(const std::vector<std::unique_ptr<Reporter> >& reporters) { const auto result = RunScenario(client_config, 1, server_config, 1, WARMUP, BENCHMARK, -2); - for (const auto& reporter : reporters) { - reporter->ReportQPSPerCore(result, server_config); - reporter->ReportLatency(result); - } + GetReporter()->ReportQPSPerCore(result, server_config); + GetReporter()->ReportLatency(result); } } // namespace testing @@ -78,10 +76,9 @@ static void RunQPS(const std::vector<std::unique_ptr<Reporter> >& reporters) { int main(int argc, char** argv) { grpc::testing::InitBenchmark(&argc, &argv, true); - const auto& reporters = grpc::testing::InitBenchmarkReporters(); signal(SIGPIPE, SIG_IGN); - grpc::testing::RunQPS(reporters); + grpc::testing::RunQPS(); return 0; } diff --git a/test/cpp/qps/report.cc b/test/cpp/qps/report.cc index 9c4bb0d95411a7c74dc559b6910afc873ee163df..e116175e3b3718dfd675eeff4f558b07fe8b0c8c 100644 --- a/test/cpp/qps/report.cc +++ b/test/cpp/qps/report.cc @@ -39,6 +39,36 @@ namespace grpc { namespace testing { +void CompositeReporter::add(std::unique_ptr<Reporter> reporter) { + reporters_.emplace_back(std::move(reporter)); +} + +void CompositeReporter::ReportQPS(const ScenarioResult& result) const { + for (size_t i = 0; i < reporters_.size(); ++i) { + reporters_[i]->ReportQPS(result); + } +} + +void CompositeReporter::ReportQPSPerCore(const ScenarioResult& result, + const ServerConfig& config) const { + for (size_t i = 0; i < reporters_.size(); ++i) { + reporters_[i]->ReportQPSPerCore(result, config); + } +} + +void CompositeReporter::ReportLatency(const ScenarioResult& result) const { + for (size_t i = 0; i < reporters_.size(); ++i) { + reporters_[i]->ReportLatency(result); + } +} + +void CompositeReporter::ReportTimes(const ScenarioResult& result) const { + for (size_t i = 0; i < reporters_.size(); ++i) { + reporters_[i]->ReportTimes(result); + } +} + + void GprLogReporter::ReportQPS(const ScenarioResult& result) const { gpr_log(GPR_INFO, "QPS: %.1f", result.latencies.Count() / diff --git a/test/cpp/qps/report.h b/test/cpp/qps/report.h index 32b948c34f943c9d807957f3abc98eedd106fec8..3712f67a47bd6d0728ff6650704ce75da2664f79 100644 --- a/test/cpp/qps/report.h +++ b/test/cpp/qps/report.h @@ -51,6 +51,8 @@ class Reporter { /** Construct a reporter with the given \a name. */ Reporter(const string& name) : name_(name) {} + virtual ~Reporter() {} + /** Returns this reporter's name. * * Names are constants, set at construction time. */ @@ -73,6 +75,24 @@ class Reporter { const string name_; }; +/** A composite for all reporters to be considered. */ +class CompositeReporter : public Reporter { + public: + CompositeReporter() : Reporter("CompositeReporter") {} + + /** Adds a \a reporter to the composite. */ + void add(std::unique_ptr<Reporter> reporter); + + void ReportQPS(const ScenarioResult& result) const GRPC_OVERRIDE; + void ReportQPSPerCore(const ScenarioResult& result, + const ServerConfig& config) const GRPC_OVERRIDE; + void ReportLatency(const ScenarioResult& result) const GRPC_OVERRIDE; + void ReportTimes(const ScenarioResult& result) const GRPC_OVERRIDE; + + private: + std::vector<std::unique_ptr<Reporter> > reporters_; +}; + /** Reporter to gpr_log(GPR_INFO). */ class GprLogReporter : public Reporter { public: diff --git a/test/cpp/qps/sync_streaming_ping_pong_test.cc b/test/cpp/qps/sync_streaming_ping_pong_test.cc index 6da107aa732f795b33c2a4d463f19c8eabacc93c..776fbdde1c00ec5cd0b657746c37680151586459 100644 --- a/test/cpp/qps/sync_streaming_ping_pong_test.cc +++ b/test/cpp/qps/sync_streaming_ping_pong_test.cc @@ -47,8 +47,7 @@ namespace testing { static const int WARMUP = 5; static const int BENCHMARK = 10; -static void RunSynchronousStreamingPingPong( - const std::vector<std::unique_ptr<Reporter> >& reporters) { +static void RunSynchronousStreamingPingPong() { gpr_log(GPR_INFO, "Running Synchronous Streaming Ping Pong"); ClientConfig client_config; @@ -67,20 +66,17 @@ static void RunSynchronousStreamingPingPong( const auto result = RunScenario(client_config, 1, server_config, 1, WARMUP, BENCHMARK, -2); - for (const auto& reporter : reporters) { - reporter->ReportQPS(result); - reporter->ReportLatency(result); - } + GetReporter()->ReportQPS(result); + GetReporter()->ReportLatency(result); } } // namespace testing } // namespace grpc int main(int argc, char** argv) { grpc::testing::InitBenchmark(&argc, &argv, true); - const auto& reporters = grpc::testing::InitBenchmarkReporters(); signal(SIGPIPE, SIG_IGN); - grpc::testing::RunSynchronousStreamingPingPong(reporters); + grpc::testing::RunSynchronousStreamingPingPong(); return 0; } diff --git a/test/cpp/qps/sync_unary_ping_pong_test.cc b/test/cpp/qps/sync_unary_ping_pong_test.cc index eb930def2a20bb8a4eb86ce703220ff2c567cb7b..e79b820ce383d9088250c3265354086530d9540a 100644 --- a/test/cpp/qps/sync_unary_ping_pong_test.cc +++ b/test/cpp/qps/sync_unary_ping_pong_test.cc @@ -47,8 +47,7 @@ namespace testing { static const int WARMUP = 5; static const int BENCHMARK = 10; -static void RunSynchronousUnaryPingPong( - const std::vector<std::unique_ptr<Reporter> >& reporters) { +static void RunSynchronousUnaryPingPong() { gpr_log(GPR_INFO, "Running Synchronous Unary Ping Pong"); ClientConfig client_config; @@ -67,10 +66,8 @@ static void RunSynchronousUnaryPingPong( const auto result = RunScenario(client_config, 1, server_config, 1, WARMUP, BENCHMARK, -2); - for (const auto& reporter : reporters) { - reporter->ReportQPS(result); - reporter->ReportLatency(result); - } + GetReporter()->ReportQPS(result); + GetReporter()->ReportLatency(result); } } // namespace testing @@ -78,10 +75,9 @@ static void RunSynchronousUnaryPingPong( int main(int argc, char** argv) { grpc::testing::InitBenchmark(&argc, &argv, true); - const auto& reporters = grpc::testing::InitBenchmarkReporters(); signal(SIGPIPE, SIG_IGN); - grpc::testing::RunSynchronousUnaryPingPong(reporters); + grpc::testing::RunSynchronousUnaryPingPong(); return 0; } diff --git a/test/cpp/util/benchmark_config.cc b/test/cpp/util/benchmark_config.cc index 914afa91ca117e9bd3eeabc607ff615f0cdcbc4e..1b15ddcbcc8ce958dd8b8239454a356381fb238a 100644 --- a/test/cpp/util/benchmark_config.cc +++ b/test/cpp/util/benchmark_config.cc @@ -51,12 +51,18 @@ void InitBenchmark(int* argc, char*** argv, bool remove_flags) { ParseCommandLineFlags(argc, argv, remove_flags); } -std::vector<std::unique_ptr<Reporter> > InitBenchmarkReporters() { - std::vector<std::unique_ptr<Reporter> > reporters; +static std::shared_ptr<Reporter> InitBenchmarkReporters() { + auto* composite_reporter = new CompositeReporter; if (FLAGS_enable_log_reporter) { - reporters.emplace_back(new GprLogReporter("LogReporter")); + composite_reporter->add( + std::unique_ptr<Reporter>(new GprLogReporter("LogReporter"))); } - return reporters; + return std::shared_ptr<Reporter>(composite_reporter); +} + +const std::shared_ptr<Reporter>& GetReporter() { + static std::shared_ptr<Reporter> reporter(InitBenchmarkReporters()); + return reporter; } } // namespace testing diff --git a/test/cpp/util/benchmark_config.h b/test/cpp/util/benchmark_config.h index efbcad88f078b9cef5bbe2d7ba69f710d6a1f716..3a3a6d61ae65c8a24ed83f8d4a45cfe7d93fc4cb 100644 --- a/test/cpp/util/benchmark_config.h +++ b/test/cpp/util/benchmark_config.h @@ -43,7 +43,13 @@ namespace grpc { namespace testing { void InitBenchmark(int* argc, char*** argv, bool remove_flags); -std::vector<std::unique_ptr<Reporter> > InitBenchmarkReporters(); + +/** Returns the benchmark Reporter instance. + * + * The returned instane will take care of generating reports for all the actual + * reporters configured via the "enable_*_reporter" command line flags (see + * benchmark_config.cc). */ +const std::shared_ptr<Reporter>& GetReporter(); } // namespace testing } // namespace grpc