|
| 1 | +// Copyright 2018, OpenCensus Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <cstdlib> |
| 16 | +#include <string> |
| 17 | +#include <thread> // NOLINT |
| 18 | + |
| 19 | +#include "absl/base/call_once.h" |
| 20 | +#include "absl/strings/str_cat.h" |
| 21 | +#include "benchmark/benchmark.h" |
| 22 | +#include "include/grpc++/grpc++.h" |
| 23 | +#include "opencensus/plugins/grpc/grpc_plugin.h" |
| 24 | +#include "opencensus/plugins/grpc/internal/testing/echo.grpc.pb.h" |
| 25 | +#include "opencensus/stats/stats.h" |
| 26 | + |
| 27 | +namespace opencensus { |
| 28 | +namespace { |
| 29 | + |
| 30 | +absl::once_flag once; |
| 31 | +void RegisterOnce() { absl::call_once(once, RegisterGrpcPlugin); } |
| 32 | + |
| 33 | +class EchoServer final : public testing::EchoService::Service { |
| 34 | + ::grpc::Status Echo(::grpc::ServerContext* context, |
| 35 | + const testing::EchoRequest* request, |
| 36 | + testing::EchoResponse* response) override { |
| 37 | + if (request->status_code() == 0) { |
| 38 | + response->set_message(request->message()); |
| 39 | + return ::grpc::Status::OK; |
| 40 | + } else { |
| 41 | + return ::grpc::Status( |
| 42 | + static_cast<::grpc::StatusCode>(request->status_code()), ""); |
| 43 | + } |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +// An EchoServerThread object creates an EchoServer on a separate thread and |
| 48 | +// shuts down the server and thread when it goes out of scope. |
| 49 | +class EchoServerThread final { |
| 50 | + public: |
| 51 | + EchoServerThread() { |
| 52 | + ::grpc::ServerBuilder builder; |
| 53 | + int port; |
| 54 | + builder.AddListeningPort("[::]:0", ::grpc::InsecureServerCredentials(), |
| 55 | + &port); |
| 56 | + builder.RegisterService(&service_); |
| 57 | + server_ = builder.BuildAndStart(); |
| 58 | + if (server_ == nullptr || port == 0) { |
| 59 | + std::abort(); |
| 60 | + } |
| 61 | + server_address_ = absl::StrCat("[::]:", port); |
| 62 | + server_thread_ = std::thread(&EchoServerThread::RunServerLoop, this); |
| 63 | + } |
| 64 | + |
| 65 | + ~EchoServerThread() { |
| 66 | + server_->Shutdown(); |
| 67 | + server_thread_.join(); |
| 68 | + } |
| 69 | + |
| 70 | + const std::string& address() { return server_address_; } |
| 71 | + |
| 72 | + private: |
| 73 | + void RunServerLoop() { server_->Wait(); } |
| 74 | + |
| 75 | + std::string server_address_; |
| 76 | + EchoServer service_; |
| 77 | + std::unique_ptr<grpc::Server> server_; |
| 78 | + std::thread server_thread_; |
| 79 | +}; |
| 80 | + |
| 81 | +void BM_E2eLatencyCensusDisabled(benchmark::State& state) { |
| 82 | + EchoServerThread server; |
| 83 | + std::unique_ptr<testing::EchoService::Stub> stub = |
| 84 | + testing::EchoService::NewStub(::grpc::CreateChannel( |
| 85 | + server.address(), ::grpc::InsecureChannelCredentials())); |
| 86 | + |
| 87 | + testing::EchoResponse response; |
| 88 | + for (auto _ : state) { |
| 89 | + testing::EchoRequest request; |
| 90 | + ::grpc::ClientContext context; |
| 91 | + ::grpc::Status status = stub->Echo(&context, request, &response); |
| 92 | + } |
| 93 | +} |
| 94 | +BENCHMARK(BM_E2eLatencyCensusDisabled); |
| 95 | + |
| 96 | +void BM_E2eLatencyCensusEnabled(benchmark::State& state) { |
| 97 | + RegisterOnce(); |
| 98 | + |
| 99 | + EchoServerThread server; |
| 100 | + std::unique_ptr<testing::EchoService::Stub> stub = |
| 101 | + testing::EchoService::NewStub(::grpc::CreateChannel( |
| 102 | + server.address(), ::grpc::InsecureChannelCredentials())); |
| 103 | + |
| 104 | + testing::EchoResponse response; |
| 105 | + for (auto _ : state) { |
| 106 | + testing::EchoRequest request; |
| 107 | + ::grpc::ClientContext context; |
| 108 | + ::grpc::Status status = stub->Echo(&context, request, &response); |
| 109 | + } |
| 110 | +} |
| 111 | +BENCHMARK(BM_E2eLatencyCensusEnabled); |
| 112 | + |
| 113 | +} // namespace |
| 114 | +} // namespace opencensus |
| 115 | + |
| 116 | +BENCHMARK_MAIN(); |
0 commit comments