writes_per_rpc_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2017 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 <gtest/gtest.h>
20 
21 #include <grpc/support/log.h>
22 #include <grpcpp/channel.h>
23 #include <grpcpp/create_channel.h>
27 #include <grpcpp/server.h>
28 #include <grpcpp/server_builder.h>
29 
40 #include "src/proto/grpc/testing/echo.grpc.pb.h"
42 #include "test/core/util/port.h"
44 
45 namespace grpc {
46 namespace testing {
47 
48 static void* tag(intptr_t x) { return reinterpret_cast<void*>(x); }
49 
51  b->SetMaxReceiveMessageSize(INT_MAX);
52  b->SetMaxSendMessageSize(INT_MAX);
53 }
54 
56  c->SetInt(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH, INT_MAX);
57  c->SetInt(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH, INT_MAX);
58  c->SetResourceQuota(ResourceQuota());
59 }
60 
61 class EndpointPairFixture {
62  public:
65  cq_ = b.AddCompletionQueue(true);
66  b.RegisterService(service);
68  server_ = b.BuildAndStart();
69 
71 
72  /* add server endpoint to server_ */
73  {
74  grpc_core::Server* core_server =
75  grpc_core::Server::FromC(server_->c_server());
76  const grpc_channel_args* server_args = core_server->channel_args();
78  server_args, endpoints.server, false /* is_client */);
79  for (grpc_pollset* pollset : core_server->pollsets()) {
80  grpc_endpoint_add_to_pollset(endpoints.server, pollset);
81  }
82 
84  "SetupTransport", core_server->SetupTransport(transport, nullptr,
85  server_args, nullptr)));
86  grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr);
87  }
88 
89  /* create channel */
90  {
92  args.SetString(GRPC_ARG_DEFAULT_AUTHORITY, "test.authority");
94 
95  grpc_channel_args c_args = args.c_channel_args();
97  grpc_create_chttp2_transport(&c_args, endpoints.client, true);
103  ->release()
104  ->c_ptr();
105  grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr);
106 
108  "", channel,
109  std::vector<std::unique_ptr<
111  }
112  }
113 
115  server_->Shutdown();
116  cq_->Shutdown();
117  void* tag;
118  bool ok;
119  while (cq_->Next(&tag, &ok)) {
120  }
121  }
122 
123  ServerCompletionQueue* cq() { return cq_.get(); }
124  std::shared_ptr<Channel> channel() { return channel_; }
125 
126  private:
127  std::unique_ptr<Server> server_;
128  std::unique_ptr<ServerCompletionQueue> cq_;
129  std::shared_ptr<Channel> channel_;
130 };
131 
132 class InProcessCHTTP2 : public EndpointPairFixture {
133  public:
136 
137  ~InProcessCHTTP2() override {
138  if (stats_ != nullptr) {
140  }
141  }
142 
143  int writes_performed() const { return stats_->num_writes; }
144 
145  private:
147 
150  grpc_passthru_endpoint_create(&p.client, &p.server, stats);
151  return p;
152  }
153 };
154 
155 static double UnaryPingPong(int request_size, int response_size) {
156  const int kIterations = 10000;
157 
158  EchoTestService::AsyncService service;
159  std::unique_ptr<InProcessCHTTP2> fixture(
161  EchoRequest send_request;
162  EchoResponse send_response;
163  EchoResponse recv_response;
164  if (request_size > 0) {
165  send_request.set_message(std::string(request_size, 'a'));
166  }
167  if (response_size > 0) {
168  send_response.set_message(std::string(response_size, 'a'));
169  }
170  Status recv_status;
171  struct ServerEnv {
173  EchoRequest recv_request;
175  ServerEnv() : response_writer(&ctx) {}
176  };
177  uint8_t server_env_buffer[2 * sizeof(ServerEnv)];
178  ServerEnv* server_env[2] = {
179  reinterpret_cast<ServerEnv*>(server_env_buffer),
180  reinterpret_cast<ServerEnv*>(server_env_buffer + sizeof(ServerEnv))};
181  new (server_env[0]) ServerEnv;
182  new (server_env[1]) ServerEnv;
183  service.RequestEcho(&server_env[0]->ctx, &server_env[0]->recv_request,
184  &server_env[0]->response_writer, fixture->cq(),
185  fixture->cq(), tag(0));
186  service.RequestEcho(&server_env[1]->ctx, &server_env[1]->recv_request,
187  &server_env[1]->response_writer, fixture->cq(),
188  fixture->cq(), tag(1));
189  std::unique_ptr<EchoTestService::Stub> stub(
190  EchoTestService::NewStub(fixture->channel()));
191  for (int iteration = 0; iteration < kIterations; iteration++) {
192  recv_response.Clear();
193  ClientContext cli_ctx;
194  std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
195  stub->AsyncEcho(&cli_ctx, send_request, fixture->cq()));
196  void* t;
197  bool ok;
198  response_reader->Finish(&recv_response, &recv_status, tag(4));
199  GPR_ASSERT(fixture->cq()->Next(&t, &ok));
200  GPR_ASSERT(ok);
201  GPR_ASSERT(t == tag(0) || t == tag(1));
202  intptr_t slot = reinterpret_cast<intptr_t>(t);
203  ServerEnv* senv = server_env[slot];
204  senv->response_writer.Finish(send_response, Status::OK, tag(3));
205  for (int i = (1 << 3) | (1 << 4); i != 0;) {
206  GPR_ASSERT(fixture->cq()->Next(&t, &ok));
207  GPR_ASSERT(ok);
208  int tagnum = static_cast<int>(reinterpret_cast<intptr_t>(t));
209  GPR_ASSERT(i & (1 << tagnum));
210  i -= 1 << tagnum;
211  }
212  GPR_ASSERT(recv_status.ok());
213 
214  senv->~ServerEnv();
215  senv = new (senv) ServerEnv();
216  service.RequestEcho(&senv->ctx, &senv->recv_request, &senv->response_writer,
217  fixture->cq(), fixture->cq(), tag(slot));
218  }
219 
220  double writes_per_iteration =
221  static_cast<double>(fixture->writes_performed()) /
222  static_cast<double>(kIterations);
223 
224  fixture.reset();
225  server_env[0]->~ServerEnv();
226  server_env[1]->~ServerEnv();
227 
228  return writes_per_iteration;
229 }
230 
231 TEST(WritesPerRpcTest, UnaryPingPong) {
232  EXPECT_LT(UnaryPingPong(0, 0), 2.05);
233  EXPECT_LT(UnaryPingPong(1, 0), 2.05);
234  EXPECT_LT(UnaryPingPong(0, 1), 2.05);
235  EXPECT_LT(UnaryPingPong(4096, 0), 2.5);
236  EXPECT_LT(UnaryPingPong(0, 4096), 2.5);
237 }
238 
239 } // namespace testing
240 } // namespace grpc
241 
242 int main(int argc, char** argv) {
243  ::testing::InitGoogleTest(&argc, argv);
244  grpc::testing::TestEnvironment env(&argc, argv);
245  grpc_init();
246  int ret = RUN_ALL_TESTS();
247  grpc_shutdown();
248  return ret;
249 }
grpc_core::Server::SetupTransport
grpc_error_handle SetupTransport(grpc_transport *transport, grpc_pollset *accepting_pollset, const grpc_channel_args *args, const RefCountedPtr< channelz::SocketNode > &socket_node)
Definition: src/core/lib/surface/server.cc:605
grpc::CreateChannelInternal
std::shared_ptr< Channel > CreateChannelInternal(const std::string &host, grpc_channel *c_channel, std::vector< std::unique_ptr< experimental::ClientInterceptorFactoryInterface >> interceptor_creators)
testing
Definition: aws_request_signer_test.cc:25
grpc::ServerCompletionQueue
Definition: include/grpcpp/impl/codegen/completion_queue.h:436
create_channel_internal.h
grpc::ServerContext
Definition: grpcpp/impl/codegen/server_context.h:566
log.h
port.h
grpc::testing::EndpointPairFixture::channel_
std::shared_ptr< Channel > channel_
Definition: fullstack_fixtures.h:247
ctx
Definition: benchmark-async.c:30
generate.env
env
Definition: generate.py:37
grpc
Definition: grpcpp/alarm.h:33
grpc::Status::ok
bool ok() const
Is the status OK?
Definition: include/grpcpp/impl/codegen/status.h:126
grpc_create_chttp2_transport
grpc_transport * grpc_create_chttp2_transport(const grpc_channel_args *channel_args, grpc_endpoint *ep, bool is_client)
Definition: chttp2_transport.cc:3122
grpc_core::Server::channel_args
const grpc_channel_args * channel_args() const
Definition: src/core/lib/surface/server.h:132
GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH
#define GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH
Definition: grpc_types.h:153
send_request
Definition: ares_private.h:147
GRPC_CLIENT_DIRECT_CHANNEL
@ GRPC_CLIENT_DIRECT_CHANNEL
Definition: channel_stack_type.h:34
grpc::experimental::ClientInterceptorFactoryInterface
Definition: impl/codegen/client_interceptor.h:48
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_passthru_endpoint_stats::num_writes
gpr_atm num_writes
Definition: passthru_endpoint.h:31
grpc::testing::InProcessCHTTP2::writes_performed
int writes_performed() const
Definition: writes_per_rpc_test.cc:143
grpc_core::ChannelArgs::FromC
static ChannelArgs FromC(const grpc_channel_args *args)
Definition: channel_args.cc:84
completion_queue.h
grpc::ResourceQuota
Definition: include/grpcpp/resource_quota.h:34
grpc_endpoint_pair::server
grpc_endpoint * server
Definition: endpoint_pair.h:28
ctx
static struct test_ctx ctx
Definition: test-ipc-send-recv.c:65
grpc::testing::InProcessCHTTP2::~InProcessCHTTP2
~InProcessCHTTP2() override
Definition: writes_per_rpc_test.cc:137
GRPC_LOG_IF_ERROR
#define GRPC_LOG_IF_ERROR(what, error)
Definition: error.h:398
grpc_passthru_endpoint_stats_create
grpc_passthru_endpoint_stats * grpc_passthru_endpoint_stats_create()
Definition: passthru_endpoint.cc:440
GRPC_ARG_MAX_SEND_MESSAGE_LENGTH
#define GRPC_ARG_MAX_SEND_MESSAGE_LENGTH
Definition: grpc_types.h:159
async_greeter_client.stub
stub
Definition: hellostreamingworld/async_greeter_client.py:26
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
grpc_channel_args
Definition: grpc_types.h:132
grpc::Service
Desriptor of an RPC service and its various RPC methods.
Definition: grpcpp/impl/codegen/service_type.h:58
endpoint_pair.h
grpc::testing::InProcessCHTTP2::MakeEndpoints
static grpc_endpoint_pair MakeEndpoints(grpc_passthru_endpoint_stats *stats)
Definition: writes_per_rpc_test.cc:148
grpc::testing::InProcessCHTTP2::InProcessCHTTP2
InProcessCHTTP2(Service *service, grpc_passthru_endpoint_stats *stats)
Definition: writes_per_rpc_test.cc:134
GRPC_ARG_DEFAULT_AUTHORITY
#define GRPC_ARG_DEFAULT_AUTHORITY
Definition: grpc_types.h:251
grpc_passthru_endpoint_create
void grpc_passthru_endpoint_create(grpc_endpoint **client, grpc_endpoint **server, grpc_passthru_endpoint_stats *stats, bool simulate_channel_actions)
Definition: passthru_endpoint.cc:413
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
grpc_core::Server::pollsets
const std::vector< grpc_pollset * > & pollsets() const
Definition: src/core/lib/surface/server.h:138
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
fixture
static const char fixture[]
Definition: test-fs-copyfile.c:36
grpc::ServerBuilder
A builder class for the creation and startup of grpc::Server instances.
Definition: grpcpp/server_builder.h:86
grpc::testing::EndpointPairFixture::~EndpointPairFixture
virtual ~EndpointPairFixture()
Definition: writes_per_rpc_test.cc:114
grpc::testing::UnaryPingPong
static double UnaryPingPong(int request_size, int response_size)
Definition: writes_per_rpc_test.cc:155
transport
grpc_transport transport
Definition: filter_fuzzer.cc:146
main
int main(int argc, char **argv)
Definition: writes_per_rpc_test.cc:242
tcp_posix.h
gen_stats_data.stats
list stats
Definition: gen_stats_data.py:58
channel.h
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
grpc::Status::OK
static const Status & OK
An OK pre-defined instance.
Definition: include/grpcpp/impl/codegen/status.h:113
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
grpc::testing::EndpointPairFixture::cq_
std::unique_ptr< ServerCompletionQueue > cq_
Definition: fullstack_fixtures.h:246
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
grpc::testing::ApplyCommonServerBuilderConfig
static void ApplyCommonServerBuilderConfig(ServerBuilder *b)
Definition: writes_per_rpc_test.cc:50
grpc_core::ExecCtx
Definition: exec_ctx.h:97
grpc_passthru_endpoint_stats
Definition: passthru_endpoint.h:29
grpc::testing::EndpointPairFixture::server_
std::unique_ptr< Server > server_
Definition: fullstack_fixtures.h:245
server_credentials.h
grpc::testing::tag
static void * tag(intptr_t t)
Definition: h2_ssl_cert_test.cc:263
grpc::ClientContext
Definition: grpcpp/impl/codegen/client_context.h:195
test_config.h
grpc::testing::EndpointPairFixture::channel
std::shared_ptr< Channel > channel()
Definition: fullstack_fixtures.h:237
grpc::ChannelArguments
Definition: grpcpp/support/channel_arguments.h:39
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc_core::Server
Definition: src/core/lib/surface/server.h:75
credentials.h
grpc_library.h
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
grpc_endpoint_pair::client
grpc_endpoint * client
Definition: endpoint_pair.h:27
grpc::testing::ApplyCommonChannelArguments
static void ApplyCommonChannelArguments(ChannelArguments *c)
Definition: writes_per_rpc_test.cc:55
grpc::testing::EndpointPairFixture::cq
ServerCompletionQueue * cq()
Definition: writes_per_rpc_test.cc:123
passthru_endpoint.h
grpc::testing::InProcessCHTTP2::stats_
grpc_passthru_endpoint_stats * stats_
Definition: writes_per_rpc_test.cc:146
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
EXPECT_LT
#define EXPECT_LT(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2032
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc::testing::EndpointPairFixture::EndpointPairFixture
EndpointPairFixture(Service *service, grpc_endpoint_pair endpoints)
Definition: writes_per_rpc_test.cc:63
chttp2_transport.h
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
ok
bool ok
Definition: async_end2end_test.cc:197
exec_ctx.h
server.h
grpc_channel
struct grpc_channel grpc_channel
Definition: grpc_types.h:62
grpc_transport
Definition: transport_impl.h:89
channel_args.h
grpc::testing::TEST
TEST(StatsTest, IncCounters)
Definition: stats_test.cc:51
server.h
grpc::ServerAsyncResponseWriter
Definition: grpcpp/impl/codegen/async_unary_call.h:295
endpoint.h
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
service
__attribute__((deprecated("Please use GRPCProtoMethod."))) @interface ProtoMethod NSString * service
Definition: ProtoMethod.h:25
grpc_endpoint_pair
Definition: endpoint_pair.h:26
grpc_endpoint_add_to_pollset
void grpc_endpoint_add_to_pollset(grpc_endpoint *ep, grpc_pollset *pollset)
Definition: endpoint.cc:35
grpc_core::CppImplOf< Server, grpc_server >::FromC
static Server * FromC(grpc_server *c_type)
Definition: cpp_impl_of.h:30
grpc::testing::InProcessCHTTP2
Definition: fullstack_fixtures.h:296
grpc_pollset
Definition: bm_cq_multiple_threads.cc:37
grpc_passthru_endpoint_stats_destroy
void grpc_passthru_endpoint_stats_destroy(grpc_passthru_endpoint_stats *stats)
Definition: passthru_endpoint.cc:449
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
server_builder.h
grpc_core::Channel::Create
static absl::StatusOr< RefCountedPtr< Channel > > Create(const char *target, ChannelArgs args, grpc_channel_stack_type channel_stack_type, grpc_transport *optional_transport)
Definition: channel.cc:202
grpc_chttp2_transport_start_reading
void grpc_chttp2_transport_start_reading(grpc_transport *transport, grpc_slice_buffer *read_buffer, grpc_closure *notify_on_receive_settings, grpc_closure *notify_on_close)
Definition: chttp2_transport.cc:3128
grpc::testing::EndpointPairFixture
Definition: fullstack_fixtures.h:163
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
create_channel.h
channel.h


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:55