streaming_throughput_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 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 <time.h>
20 
21 #include <mutex>
22 #include <thread>
23 
24 #include <gtest/gtest.h>
25 
26 #include <grpc/grpc.h>
27 #include <grpc/support/atm.h>
28 #include <grpc/support/log.h>
29 #include <grpc/support/time.h>
30 #include <grpcpp/channel.h>
31 #include <grpcpp/client_context.h>
32 #include <grpcpp/create_channel.h>
35 #include <grpcpp/server.h>
36 #include <grpcpp/server_builder.h>
37 #include <grpcpp/server_context.h>
38 
39 #include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
40 #include "src/proto/grpc/testing/echo.grpc.pb.h"
41 #include "test/core/util/port.h"
43 
44 using grpc::testing::EchoRequest;
45 using grpc::testing::EchoResponse;
46 
47 const char* kLargeString =
48  "("
49  "To be, or not to be- that is the question:"
50  "Whether 'tis nobler in the mind to suffer"
51  "The slings and arrows of outrageous fortune"
52  "Or to take arms against a sea of troubles,"
53  "And by opposing end them. To die- to sleep-"
54  "No more; and by a sleep to say we end"
55  "The heartache, and the thousand natural shock"
56  "That flesh is heir to. 'Tis a consummation"
57  "Devoutly to be wish'd. To die- to sleep."
58  "To sleep- perchance to dream: ay, there's the rub!"
59  "For in that sleep of death what dreams may come"
60  "When we have shuffled off this mortal coil,"
61  "Must give us pause. There's the respect"
62  "That makes calamity of so long life."
63  "For who would bear the whips and scorns of time,"
64  "Th' oppressor's wrong, the proud man's contumely,"
65  "The pangs of despis'd love, the law's delay,"
66  "The insolence of office, and the spurns"
67  "That patient merit of th' unworthy takes,"
68  "When he himself might his quietus make"
69  "With a bare bodkin? Who would these fardels bear,"
70  "To grunt and sweat under a weary life,"
71  "But that the dread of something after death-"
72  "The undiscover'd country, from whose bourn"
73  "No traveller returns- puzzles the will,"
74  "And makes us rather bear those ills we have"
75  "Than fly to others that we know not of?"
76  "Thus conscience does make cowards of us all,"
77  "And thus the native hue of resolution"
78  "Is sicklied o'er with the pale cast of thought,"
79  "And enterprises of great pith and moment"
80  "With this regard their currents turn awry"
81  "And lose the name of action.- Soft you now!"
82  "The fair Ophelia!- Nymph, in thy orisons"
83  "Be all my sins rememb'red.";
84 
85 namespace grpc {
86 namespace testing {
87 
88 class TestServiceImpl : public grpc::testing::EchoTestService::Service {
89  public:
90  static void BidiStream_Sender(
92  gpr_atm* should_exit) {
93  EchoResponse response;
94  response.set_message(kLargeString);
95  while (gpr_atm_acq_load(should_exit) == static_cast<gpr_atm>(0)) {
96  struct timespec tv = {0, 1000000}; // 1 ms
97  struct timespec rem;
98  // TODO (vpai): Mark this blocking
99  while (nanosleep(&tv, &rem) != 0) {
100  tv = rem;
101  };
102 
103  stream->Write(response);
104  }
105  }
106 
107  // Only implement the one method we will be calling for brevity.
109  ServerContext* /*context*/,
111  EchoRequest request;
112  gpr_atm should_exit;
113  gpr_atm_rel_store(&should_exit, static_cast<gpr_atm>(0));
114 
115  std::thread sender(
116  std::bind(&TestServiceImpl::BidiStream_Sender, stream, &should_exit));
117 
118  while (stream->Read(&request)) {
119  struct timespec tv = {0, 3000000}; // 3 ms
120  struct timespec rem;
121  // TODO (vpai): Mark this blocking
122  while (nanosleep(&tv, &rem) != 0) {
123  tv = rem;
124  };
125  }
126  gpr_atm_rel_store(&should_exit, static_cast<gpr_atm>(1));
127  sender.join();
128  return Status::OK;
129  }
130 };
131 
132 class End2endTest : public ::testing::Test {
133  protected:
134  void SetUp() override {
136  server_address_ << "localhost:" << port;
137  // Setup server
139  builder.AddListeningPort(server_address_.str(),
141  builder.RegisterService(&service_);
142  server_ = builder.BuildAndStart();
143  }
144 
145  void TearDown() override { server_->Shutdown(); }
146 
147  void ResetStub() {
148  std::shared_ptr<Channel> channel = grpc::CreateChannel(
150  stub_ = grpc::testing::EchoTestService::NewStub(channel);
151  }
152 
153  std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
154  std::unique_ptr<Server> server_;
155  std::ostringstream server_address_;
157 };
158 
160  EchoResponse response;
161  while (reader->Read(&response)) {
162  // Just drain out the responses as fast as possible.
163  }
164 }
165 
166 TEST_F(End2endTest, StreamingThroughput) {
167  ResetStub();
169  auto stream = stub_->BidiStream(&context);
170 
171  auto reader = stream.get();
172  std::thread receiver(std::bind(Drainer, reader));
173 
174  for (int i = 0; i < 10000; i++) {
175  EchoRequest request;
176  request.set_message(kLargeString);
177  ASSERT_TRUE(stream->Write(request));
178  if (i % 1000 == 0) {
179  gpr_log(GPR_INFO, "Send count = %d", i);
180  }
181  }
182  stream->WritesDone();
183  receiver.join();
184 }
185 
186 } // namespace testing
187 } // namespace grpc
188 
189 int main(int argc, char** argv) {
190  grpc::testing::TestEnvironment env(&argc, argv);
191  ::testing::InitGoogleTest(&argc, argv);
192  return RUN_ALL_TESTS();
193 }
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
testing
Definition: aws_request_signer_test.cc:25
grpc::ServerContext
Definition: grpcpp/impl/codegen/server_context.h:566
log.h
stub_
std::unique_ptr< grpc::testing::EchoTestService::Stub > stub_
Definition: client_channel_stress_test.cc:331
port.h
generate.env
env
Definition: generate.py:37
grpc::testing::End2endTest::stub_
std::unique_ptr< grpc::testing::EchoTestService::Stub > stub_
Definition: streaming_throughput_test.cc:153
grpc
Definition: grpcpp/alarm.h:33
benchmark.request
request
Definition: benchmark.py:77
nanosleep
int nanosleep(const struct timespec *req, struct timespec *rem)
Definition: os390-syscalls.c:385
grpc::ServerReaderWriter
Definition: grpcpp/impl/codegen/sync_stream.h:786
time.h
grpc::testing::End2endTest::SetUp
void SetUp() override
Definition: streaming_throughput_test.cc:134
grpc::testing::TestServiceImpl
TestMultipleServiceImpl< grpc::testing::EchoTestService::Service > TestServiceImpl
Definition: test_service_impl.h:498
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
server_
Server *const server_
Definition: chttp2_server.cc:260
main
int main(int argc, char **argv)
Definition: streaming_throughput_test.cc:189
grpc::ClientReaderWriter
Definition: grpcpp/impl/codegen/channel_interface.h:35
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
grpc::ServerBuilder
A builder class for the creation and startup of grpc::Server instances.
Definition: grpcpp/server_builder.h:86
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc.h
gpr_atm_acq_load
#define gpr_atm_acq_load(p)
Definition: impl/codegen/atm_gcc_atomic.h:52
channel.h
gpr_atm_rel_store
#define gpr_atm_rel_store(p, value)
Definition: impl/codegen/atm_gcc_atomic.h:54
grpc::Status::OK
static const Status & OK
An OK pre-defined instance.
Definition: include/grpcpp/impl/codegen/status.h:113
grpc::CreateChannel
std::shared_ptr< Channel > CreateChannel(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds)
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_pick_unused_port_or_die
int grpc_pick_unused_port_or_die(void)
grpc::testing::End2endTest::TearDown
void TearDown() override
Definition: streaming_throughput_test.cc:145
server_credentials.h
grpc::testing::End2endTest::ResetStub
void ResetStub()
Definition: streaming_throughput_test.cc:147
grpc::ClientContext
Definition: grpcpp/impl/codegen/client_context.h:195
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
test_config.h
grpc::testing::End2endTest::server_address_
std::ostringstream server_address_
Definition: streaming_throughput_test.cc:155
gpr_atm
intptr_t gpr_atm
Definition: impl/codegen/atm_gcc_atomic.h:32
client_context.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
credentials.h
grpc::testing::TEST_F
TEST_F(ChannelArgumentsTest, SetInt)
Definition: channel_arguments_test.cc:134
grpc::testing::TestServiceImpl::BidiStream_Sender
static void BidiStream_Sender(ServerReaderWriter< EchoResponse, EchoRequest > *stream, gpr_atm *should_exit)
Definition: streaming_throughput_test.cc:90
server_context.h
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc::testing::End2endTest::service_
TestServiceImpl service_
Definition: streaming_throughput_test.cc:156
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
server_address_
const char * server_address_
Definition: settings_timeout_test.cc:231
grpc::testing::TestServiceImpl::BidiStream
Status BidiStream(ServerContext *, ServerReaderWriter< EchoResponse, EchoRequest > *stream) override
Definition: streaming_throughput_test.cc:108
kLargeString
const char * kLargeString
Definition: streaming_throughput_test.cc:47
grpc::testing::End2endTest::server_
std::unique_ptr< Server > server_
Definition: streaming_throughput_test.cc:154
grpc::testing::Drainer
static void Drainer(ClientReaderWriter< EchoRequest, EchoResponse > *reader)
Definition: streaming_throughput_test.cc:159
grpc::InsecureServerCredentials
std::shared_ptr< ServerCredentials > InsecureServerCredentials()
Definition: insecure_server_credentials.cc:52
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
atm.h
server.h
grpc::InsecureChannelCredentials
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
Definition: cpp/client/insecure_credentials.cc:69
TestServiceImpl
Definition: interop_server.cc:139
thread
static uv_thread_t thread
Definition: test-async-null-cb.c:29
server_builder.h
reader
void reader(void *n)
Definition: libuv/docs/code/locks/main.c:8
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
create_channel.h
service_
std::unique_ptr< grpc::testing::TestServiceImpl > service_
Definition: end2end_binder_transport_test.cc:71
stream
voidpf stream
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136


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