writes_per_rpc_test.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #include <gtest/gtest.h>
  19. #include <grpc/support/log.h>
  20. #include <grpcpp/channel.h>
  21. #include <grpcpp/create_channel.h>
  22. #include <grpcpp/impl/grpc_library.h>
  23. #include <grpcpp/security/credentials.h>
  24. #include <grpcpp/security/server_credentials.h>
  25. #include <grpcpp/server.h>
  26. #include <grpcpp/server_builder.h>
  27. #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
  28. #include "src/core/lib/channel/channel_args.h"
  29. #include "src/core/lib/iomgr/endpoint.h"
  30. #include "src/core/lib/iomgr/endpoint_pair.h"
  31. #include "src/core/lib/iomgr/exec_ctx.h"
  32. #include "src/core/lib/iomgr/tcp_posix.h"
  33. #include "src/core/lib/surface/channel.h"
  34. #include "src/core/lib/surface/completion_queue.h"
  35. #include "src/core/lib/surface/server.h"
  36. #include "src/cpp/client/create_channel_internal.h"
  37. #include "src/proto/grpc/testing/echo.grpc.pb.h"
  38. #include "test/core/util/passthru_endpoint.h"
  39. #include "test/core/util/port.h"
  40. #include "test/core/util/test_config.h"
  41. namespace grpc {
  42. namespace testing {
  43. static void* tag(intptr_t x) { return reinterpret_cast<void*>(x); }
  44. static void ApplyCommonServerBuilderConfig(ServerBuilder* b) {
  45. b->SetMaxReceiveMessageSize(INT_MAX);
  46. b->SetMaxSendMessageSize(INT_MAX);
  47. }
  48. static void ApplyCommonChannelArguments(ChannelArguments* c) {
  49. c->SetInt(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH, INT_MAX);
  50. c->SetInt(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH, INT_MAX);
  51. c->SetResourceQuota(ResourceQuota());
  52. }
  53. class EndpointPairFixture {
  54. public:
  55. EndpointPairFixture(Service* service, grpc_endpoint_pair endpoints) {
  56. ServerBuilder b;
  57. cq_ = b.AddCompletionQueue(true);
  58. b.RegisterService(service);
  59. ApplyCommonServerBuilderConfig(&b);
  60. server_ = b.BuildAndStart();
  61. grpc_core::ExecCtx exec_ctx;
  62. /* add server endpoint to server_ */
  63. {
  64. grpc_core::Server* core_server =
  65. grpc_core::Server::FromC(server_->c_server());
  66. const grpc_channel_args* server_args = core_server->channel_args();
  67. grpc_transport* transport = grpc_create_chttp2_transport(
  68. server_args, endpoints.server, false /* is_client */);
  69. for (grpc_pollset* pollset : core_server->pollsets()) {
  70. grpc_endpoint_add_to_pollset(endpoints.server, pollset);
  71. }
  72. GPR_ASSERT(GRPC_LOG_IF_ERROR(
  73. "SetupTransport", core_server->SetupTransport(transport, nullptr,
  74. server_args, nullptr)));
  75. grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr);
  76. }
  77. /* create channel */
  78. {
  79. ChannelArguments args;
  80. args.SetString(GRPC_ARG_DEFAULT_AUTHORITY, "test.authority");
  81. ApplyCommonChannelArguments(&args);
  82. grpc_channel_args c_args = args.c_channel_args();
  83. grpc_transport* transport =
  84. grpc_create_chttp2_transport(&c_args, endpoints.client, true);
  85. GPR_ASSERT(transport);
  86. grpc_channel* channel = grpc_channel_create_internal(
  87. "target", &c_args, GRPC_CLIENT_DIRECT_CHANNEL, transport, nullptr);
  88. grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr);
  89. channel_ = grpc::CreateChannelInternal(
  90. "", channel,
  91. std::vector<std::unique_ptr<
  92. experimental::ClientInterceptorFactoryInterface>>());
  93. }
  94. }
  95. virtual ~EndpointPairFixture() {
  96. server_->Shutdown();
  97. cq_->Shutdown();
  98. void* tag;
  99. bool ok;
  100. while (cq_->Next(&tag, &ok)) {
  101. }
  102. }
  103. ServerCompletionQueue* cq() { return cq_.get(); }
  104. std::shared_ptr<Channel> channel() { return channel_; }
  105. private:
  106. std::unique_ptr<Server> server_;
  107. std::unique_ptr<ServerCompletionQueue> cq_;
  108. std::shared_ptr<Channel> channel_;
  109. };
  110. class InProcessCHTTP2 : public EndpointPairFixture {
  111. public:
  112. InProcessCHTTP2(Service* service, grpc_passthru_endpoint_stats* stats)
  113. : EndpointPairFixture(service, MakeEndpoints(stats)), stats_(stats) {}
  114. ~InProcessCHTTP2() override {
  115. if (stats_ != nullptr) {
  116. grpc_passthru_endpoint_stats_destroy(stats_);
  117. }
  118. }
  119. int writes_performed() const { return stats_->num_writes; }
  120. private:
  121. grpc_passthru_endpoint_stats* stats_;
  122. static grpc_endpoint_pair MakeEndpoints(grpc_passthru_endpoint_stats* stats) {
  123. grpc_endpoint_pair p;
  124. grpc_passthru_endpoint_create(&p.client, &p.server, stats);
  125. return p;
  126. }
  127. };
  128. static double UnaryPingPong(int request_size, int response_size) {
  129. const int kIterations = 10000;
  130. EchoTestService::AsyncService service;
  131. std::unique_ptr<InProcessCHTTP2> fixture(
  132. new InProcessCHTTP2(&service, grpc_passthru_endpoint_stats_create()));
  133. EchoRequest send_request;
  134. EchoResponse send_response;
  135. EchoResponse recv_response;
  136. if (request_size > 0) {
  137. send_request.set_message(std::string(request_size, 'a'));
  138. }
  139. if (response_size > 0) {
  140. send_response.set_message(std::string(response_size, 'a'));
  141. }
  142. Status recv_status;
  143. struct ServerEnv {
  144. ServerContext ctx;
  145. EchoRequest recv_request;
  146. grpc::ServerAsyncResponseWriter<EchoResponse> response_writer;
  147. ServerEnv() : response_writer(&ctx) {}
  148. };
  149. uint8_t server_env_buffer[2 * sizeof(ServerEnv)];
  150. ServerEnv* server_env[2] = {
  151. reinterpret_cast<ServerEnv*>(server_env_buffer),
  152. reinterpret_cast<ServerEnv*>(server_env_buffer + sizeof(ServerEnv))};
  153. new (server_env[0]) ServerEnv;
  154. new (server_env[1]) ServerEnv;
  155. service.RequestEcho(&server_env[0]->ctx, &server_env[0]->recv_request,
  156. &server_env[0]->response_writer, fixture->cq(),
  157. fixture->cq(), tag(0));
  158. service.RequestEcho(&server_env[1]->ctx, &server_env[1]->recv_request,
  159. &server_env[1]->response_writer, fixture->cq(),
  160. fixture->cq(), tag(1));
  161. std::unique_ptr<EchoTestService::Stub> stub(
  162. EchoTestService::NewStub(fixture->channel()));
  163. for (int iteration = 0; iteration < kIterations; iteration++) {
  164. recv_response.Clear();
  165. ClientContext cli_ctx;
  166. std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
  167. stub->AsyncEcho(&cli_ctx, send_request, fixture->cq()));
  168. void* t;
  169. bool ok;
  170. response_reader->Finish(&recv_response, &recv_status, tag(4));
  171. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  172. GPR_ASSERT(ok);
  173. GPR_ASSERT(t == tag(0) || t == tag(1));
  174. intptr_t slot = reinterpret_cast<intptr_t>(t);
  175. ServerEnv* senv = server_env[slot];
  176. senv->response_writer.Finish(send_response, Status::OK, tag(3));
  177. for (int i = (1 << 3) | (1 << 4); i != 0;) {
  178. GPR_ASSERT(fixture->cq()->Next(&t, &ok));
  179. GPR_ASSERT(ok);
  180. int tagnum = static_cast<int>(reinterpret_cast<intptr_t>(t));
  181. GPR_ASSERT(i & (1 << tagnum));
  182. i -= 1 << tagnum;
  183. }
  184. GPR_ASSERT(recv_status.ok());
  185. senv->~ServerEnv();
  186. senv = new (senv) ServerEnv();
  187. service.RequestEcho(&senv->ctx, &senv->recv_request, &senv->response_writer,
  188. fixture->cq(), fixture->cq(), tag(slot));
  189. }
  190. double writes_per_iteration =
  191. static_cast<double>(fixture->writes_performed()) /
  192. static_cast<double>(kIterations);
  193. fixture.reset();
  194. server_env[0]->~ServerEnv();
  195. server_env[1]->~ServerEnv();
  196. return writes_per_iteration;
  197. }
  198. TEST(WritesPerRpcTest, UnaryPingPong) {
  199. EXPECT_LT(UnaryPingPong(0, 0), 2.05);
  200. EXPECT_LT(UnaryPingPong(1, 0), 2.05);
  201. EXPECT_LT(UnaryPingPong(0, 1), 2.05);
  202. EXPECT_LT(UnaryPingPong(4096, 0), 2.5);
  203. EXPECT_LT(UnaryPingPong(0, 4096), 2.5);
  204. }
  205. } // namespace testing
  206. } // namespace grpc
  207. int main(int argc, char** argv) {
  208. ::testing::InitGoogleTest(&argc, argv);
  209. grpc::testing::TestEnvironment env(argc, argv);
  210. grpc_init();
  211. int ret = RUN_ALL_TESTS();
  212. grpc_shutdown();
  213. return ret;
  214. }