bm_opencensus_plugin.cc
Go to the documentation of this file.
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 
34 #include "src/proto/grpc/testing/echo.grpc.pb.h"
37 
40 
41 class EchoServer final : public grpc::testing::EchoTestService::Service {
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:
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);
72  }
73 
75  server_->Shutdown();
76  server_thread_.join();
77  }
78 
79  const std::string& address() { return server_address_; }
80 
81  private:
82  void RunServerLoop() { server_->Wait(); }
83 
86  std::unique_ptr<grpc::Server> server_;
88 };
89 
94  std::unique_ptr<grpc::testing::EchoTestService::Stub> stub =
95  grpc::testing::EchoTestService::NewStub(grpc::CreateChannel(
97 
98  grpc::testing::EchoResponse response;
99  for (auto _ : state) {
100  grpc::testing::EchoRequest request;
103  }
104 }
106 
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.
114 
115  grpc::testing::TestGrpcScope grpc_scope;
117  std::unique_ptr<grpc::testing::EchoTestService::Stub> stub =
118  grpc::testing::EchoTestService::NewStub(grpc::CreateChannel(
120 
121  grpc::testing::EchoResponse response;
122  for (auto _ : state) {
123  grpc::testing::EchoRequest request;
126  }
127 }
129 
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;
135 }
EchoServerThread::EchoServerThread
EchoServerThread()
Definition: bm_opencensus_plugin.cc:60
grpc::ServerContext
Definition: grpcpp/impl/codegen/server_context.h:566
core_configuration.h
generate.env
env
Definition: generate.py:37
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
benchmark.request
request
Definition: benchmark.py:77
grpc::testing::TestGrpcScope
Definition: test/core/util/test_config.h:63
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc::RegisterOpenCensusPlugin
void RegisterOpenCensusPlugin()
Definition: grpc_plugin.cc:42
status
absl::Status status
Definition: rls.cc:251
async_greeter_client.stub
stub
Definition: hellostreamingworld/async_greeter_client.py:26
absl::call_once
void call_once(absl::once_flag &flag, Callable &&fn, Args &&... args)
Definition: abseil-cpp/absl/base/call_once.h:206
benchmark::RunSpecifiedBenchmarks
size_t RunSpecifiedBenchmarks()
Definition: benchmark/src/benchmark.cc:437
server
std::unique_ptr< Server > server
Definition: channelz_service_test.cc:330
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
grpc.StatusCode
Definition: src/python/grpcio/grpc/__init__.py:232
grpc::ServerBuilder
A builder class for the creation and startup of grpc::Server instances.
Definition: grpcpp/server_builder.h:86
EchoServerThread::server_thread_
std::thread server_thread_
Definition: bm_opencensus_plugin.cc:87
BENCHMARK
BENCHMARK(BM_E2eLatencyCensusDisabled)
grpc.h
gmock_output_test._
_
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
EchoServer::Echo
grpc::Status Echo(grpc::ServerContext *, const grpc::testing::EchoRequest *request, grpc::testing::EchoResponse *response) override
Definition: bm_opencensus_plugin.cc:42
grpcpp.h
EchoServer
Definition: bm_opencensus_plugin.cc:41
grpc::Status::OK
static const Status & OK
An OK pre-defined instance.
Definition: include/grpcpp/impl/codegen/status.h:113
EchoServerThread::address
const std::string & address()
Definition: bm_opencensus_plugin.cc:79
benchmark::Initialize
void Initialize(int *argc, char **argv)
Definition: benchmark/src/benchmark.cc:602
RegisterOnce
void RegisterOnce()
Definition: bm_opencensus_plugin.cc:39
grpc::CreateChannel
std::shared_ptr< Channel > CreateChannel(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds)
opencensus.h
helpers.h
benchmark::ReportUnrecognizedArguments
bool ReportUnrecognizedArguments(int argc, char **argv)
Definition: benchmark/src/benchmark.cc:611
EchoServerThread::server_
std::unique_ptr< grpc::Server > server_
Definition: bm_opencensus_plugin.cc:86
grpc_plugin.h
grpc::ClientContext
Definition: grpcpp/impl/codegen/client_context.h:195
EchoServerThread::service_
EchoServer service_
Definition: bm_opencensus_plugin.cc:85
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
test_config.h
once
absl::once_flag once
Definition: bm_opencensus_plugin.cc:38
server
Definition: examples/python/async_streaming/server.py:1
BM_E2eLatencyCensusEnabled
static void BM_E2eLatencyCensusEnabled(benchmark::State &state)
Definition: bm_opencensus_plugin.cc:107
benchmark::State
Definition: benchmark/include/benchmark/benchmark.h:503
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc_core::CoreConfiguration::Reset
static void Reset()
Definition: core_configuration.cc:96
EchoServerThread
Definition: bm_opencensus_plugin.cc:58
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
main
int main(int argc, char **argv)
Definition: bm_opencensus_plugin.cc:130
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
grpc::RegisterOpenCensusViewsForExport
void RegisterOpenCensusViewsForExport()
Definition: views.cc:78
grpc::InsecureServerCredentials
std::shared_ptr< ServerCredentials > InsecureServerCredentials()
Definition: insecure_server_credentials.cc:52
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
EchoServerThread::server_address_
std::string server_address_
Definition: bm_opencensus_plugin.cc:84
BM_E2eLatencyCensusDisabled
static void BM_E2eLatencyCensusDisabled(benchmark::State &state)
Definition: bm_opencensus_plugin.cc:90
EchoServerThread::~EchoServerThread
~EchoServerThread()
Definition: bm_opencensus_plugin.cc:74
absl::once_flag
Definition: abseil-cpp/absl/base/call_once.h:85
grpc::InsecureChannelCredentials
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
Definition: cpp/client/insecure_credentials.cc:69
EchoServerThread::RunServerLoop
void RunServerLoop()
Definition: bm_opencensus_plugin.cc:82
thread
static uv_thread_t thread
Definition: test-async-null-cb.c:29


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:49