bm_opencensus_plugin.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. *
  3. * Copyright 2018 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 <string>
  19. #include <thread> // NOLINT
  20. #include <benchmark/benchmark.h>
  21. #include "absl/base/call_once.h"
  22. #include "absl/strings/str_cat.h"
  23. #include "opencensus/stats/stats.h"
  24. #include <grpc/grpc.h>
  25. #include <grpcpp/grpcpp.h>
  26. #include "src/core/lib/config/core_configuration.h"
  27. #include "src/cpp/ext/filters/census/grpc_plugin.h"
  28. #include "src/proto/grpc/testing/echo.grpc.pb.h"
  29. #include "test/core/util/test_config.h"
  30. #include "test/cpp/microbenchmarks/helpers.h"
  31. absl::once_flag once;
  32. void RegisterOnce() { absl::call_once(once, grpc::RegisterOpenCensusPlugin); }
  33. class EchoServer final : public grpc::testing::EchoTestService::Service {
  34. grpc::Status Echo(grpc::ServerContext* /*context*/,
  35. const grpc::testing::EchoRequest* request,
  36. grpc::testing::EchoResponse* response) override {
  37. if (request->param().expected_error().code() == 0) {
  38. response->set_message(request->message());
  39. return grpc::Status::OK;
  40. } else {
  41. return grpc::Status(static_cast<grpc::StatusCode>(
  42. request->param().expected_error().code()),
  43. "");
  44. }
  45. }
  46. };
  47. // An EchoServerThread object creates an EchoServer on a separate thread and
  48. // shuts down the server and thread when it goes out of scope.
  49. class EchoServerThread final {
  50. public:
  51. EchoServerThread() {
  52. grpc::ServerBuilder builder;
  53. int port;
  54. builder.AddListeningPort("[::]:0", grpc::InsecureServerCredentials(),
  55. &port);
  56. builder.RegisterService(&service_);
  57. server_ = builder.BuildAndStart();
  58. if (server_ == nullptr || port == 0) {
  59. std::abort();
  60. }
  61. server_address_ = absl::StrCat("[::]:", port);
  62. server_thread_ = std::thread(&EchoServerThread::RunServerLoop, this);
  63. }
  64. ~EchoServerThread() {
  65. server_->Shutdown();
  66. server_thread_.join();
  67. }
  68. const std::string& address() { return server_address_; }
  69. private:
  70. void RunServerLoop() { server_->Wait(); }
  71. std::string server_address_;
  72. EchoServer service_;
  73. std::unique_ptr<grpc::Server> server_;
  74. std::thread server_thread_;
  75. };
  76. static void BM_E2eLatencyCensusDisabled(benchmark::State& state) {
  77. grpc_core::CoreConfiguration::Reset();
  78. grpc::testing::TestGrpcScope grpc_scope;
  79. EchoServerThread server;
  80. std::unique_ptr<grpc::testing::EchoTestService::Stub> stub =
  81. grpc::testing::EchoTestService::NewStub(grpc::CreateChannel(
  82. server.address(), grpc::InsecureChannelCredentials()));
  83. grpc::testing::EchoResponse response;
  84. for (auto _ : state) {
  85. grpc::testing::EchoRequest request;
  86. grpc::ClientContext context;
  87. grpc::Status status = stub->Echo(&context, request, &response);
  88. }
  89. }
  90. BENCHMARK(BM_E2eLatencyCensusDisabled);
  91. static void BM_E2eLatencyCensusEnabled(benchmark::State& state) {
  92. grpc_core::CoreConfiguration::Reset();
  93. // Now start the test by registering the plugin (once in the execution)
  94. RegisterOnce();
  95. // This we can safely repeat, and doing so clears accumulated data to avoid
  96. // initialization costs varying between runs.
  97. grpc::RegisterOpenCensusViewsForExport();
  98. grpc::testing::TestGrpcScope grpc_scope;
  99. EchoServerThread server;
  100. std::unique_ptr<grpc::testing::EchoTestService::Stub> stub =
  101. grpc::testing::EchoTestService::NewStub(grpc::CreateChannel(
  102. server.address(), grpc::InsecureChannelCredentials()));
  103. grpc::testing::EchoResponse response;
  104. for (auto _ : state) {
  105. grpc::testing::EchoRequest request;
  106. grpc::ClientContext context;
  107. grpc::Status status = stub->Echo(&context, request, &response);
  108. }
  109. }
  110. BENCHMARK(BM_E2eLatencyCensusEnabled);
  111. int main(int argc, char** argv) {
  112. grpc::testing::TestEnvironment env(argc, argv);
  113. ::benchmark::Initialize(&argc, argv);
  114. if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1;
  115. ::benchmark::RunSpecifiedBenchmarks();
  116. }