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