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