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