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