bm_opencensus_plugin.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <benchmark/benchmark.h>
  19. #include <string>
  20. #include <thread> // NOLINT
  21. #include "absl/base/call_once.h"
  22. #include "absl/strings/str_cat.h"
  23. #include "include/grpc/grpc.h"
  24. #include "include/grpcpp/grpcpp.h"
  25. #include "opencensus/stats/stats.h"
  26. #include "src/cpp/ext/filters/census/grpc_plugin.h"
  27. #include "src/proto/grpc/testing/echo.grpc.pb.h"
  28. #include "test/cpp/microbenchmarks/helpers.h"
  29. absl::once_flag once;
  30. void RegisterOnce() { absl::call_once(once, grpc::RegisterOpenCensusPlugin); }
  31. class EchoServer final : public grpc::testing::EchoTestService::Service {
  32. grpc::Status Echo(grpc::ServerContext* context,
  33. const grpc::testing::EchoRequest* request,
  34. grpc::testing::EchoResponse* response) override {
  35. if (request->param().expected_error().code() == 0) {
  36. response->set_message(request->message());
  37. return grpc::Status::OK;
  38. } else {
  39. return grpc::Status(static_cast<grpc::StatusCode>(
  40. request->param().expected_error().code()),
  41. "");
  42. }
  43. }
  44. };
  45. // An EchoServerThread object creates an EchoServer on a separate thread and
  46. // shuts down the server and thread when it goes out of scope.
  47. class EchoServerThread final {
  48. public:
  49. EchoServerThread() {
  50. grpc::ServerBuilder builder;
  51. int port;
  52. builder.AddListeningPort("[::]:0", grpc::InsecureServerCredentials(),
  53. &port);
  54. builder.RegisterService(&service_);
  55. server_ = builder.BuildAndStart();
  56. if (server_ == nullptr || port == 0) {
  57. std::abort();
  58. }
  59. server_address_ = absl::StrCat("[::]:", port);
  60. server_thread_ = std::thread(&EchoServerThread::RunServerLoop, this);
  61. }
  62. ~EchoServerThread() {
  63. server_->Shutdown();
  64. server_thread_.join();
  65. }
  66. const std::string& address() { return server_address_; }
  67. private:
  68. void RunServerLoop() { server_->Wait(); }
  69. std::string server_address_;
  70. EchoServer service_;
  71. std::unique_ptr<grpc::Server> server_;
  72. std::thread server_thread_;
  73. };
  74. static void BM_E2eLatencyCensusDisabled(benchmark::State& state) {
  75. EchoServerThread server;
  76. std::unique_ptr<grpc::testing::EchoTestService::Stub> stub =
  77. grpc::testing::EchoTestService::NewStub(grpc::CreateChannel(
  78. server.address(), grpc::InsecureChannelCredentials()));
  79. grpc::testing::EchoResponse response;
  80. for (auto _ : state) {
  81. grpc::testing::EchoRequest request;
  82. grpc::ClientContext context;
  83. grpc::Status status = stub->Echo(&context, request, &response);
  84. }
  85. }
  86. BENCHMARK(BM_E2eLatencyCensusDisabled);
  87. static void BM_E2eLatencyCensusEnabled(benchmark::State& state) {
  88. // Avoid a data race between registering plugin and shutdown of previous
  89. // test (order-dependent) by doing an init/shutdown so that any previous
  90. // shutdowns are fully complete first.
  91. grpc_init();
  92. grpc_shutdown_blocking();
  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. EchoServerThread server;
  99. std::unique_ptr<grpc::testing::EchoTestService::Stub> stub =
  100. grpc::testing::EchoTestService::NewStub(grpc::CreateChannel(
  101. server.address(), grpc::InsecureChannelCredentials()));
  102. grpc::testing::EchoResponse response;
  103. for (auto _ : state) {
  104. grpc::testing::EchoRequest request;
  105. grpc::ClientContext context;
  106. grpc::Status status = stub->Echo(&context, request, &response);
  107. }
  108. }
  109. BENCHMARK(BM_E2eLatencyCensusEnabled);
  110. BENCHMARK_MAIN();