3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <grpc/grpc.h>
24 #include <grpc/support/atm.h>
25 #include <grpc/support/log.h>
26 #include <grpc/support/time.h>
27 #include <grpcpp/channel.h>
28 #include <grpcpp/client_context.h>
29 #include <grpcpp/create_channel.h>
30 #include <grpcpp/security/credentials.h>
31 #include <grpcpp/security/server_credentials.h>
32 #include <grpcpp/server.h>
33 #include <grpcpp/server_builder.h>
34 #include <grpcpp/server_context.h>
36 #include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
37 #include "src/proto/grpc/testing/echo.grpc.pb.h"
38 #include "test/core/util/port.h"
39 #include "test/core/util/test_config.h"
41 #include <gtest/gtest.h>
43 using grpc::testing::EchoRequest;
44 using grpc::testing::EchoResponse;
45 using std::chrono::system_clock;
47 const char* kLargeString =
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.";
88 class TestServiceImpl : public ::grpc::testing::EchoTestService::Service {
90 static void BidiStream_Sender(
91 ServerReaderWriter<EchoResponse, EchoRequest>* stream,
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
98 // TODO (vpai): Mark this blocking
99 while (nanosleep(&tv, &rem) != 0) {
103 stream->Write(response);
107 // Only implement the one method we will be calling for brevity.
109 ServerContext* context,
110 ServerReaderWriter<EchoResponse, EchoRequest>* stream) override {
113 gpr_atm_rel_store(&should_exit, static_cast<gpr_atm>(0));
116 std::bind(&TestServiceImpl::BidiStream_Sender, stream, &should_exit));
118 while (stream->Read(&request)) {
119 struct timespec tv = {0, 3000000}; // 3 ms
121 // TODO (vpai): Mark this blocking
122 while (nanosleep(&tv, &rem) != 0) {
126 gpr_atm_rel_store(&should_exit, static_cast<gpr_atm>(1));
132 class End2endTest : public ::testing::Test {
134 void SetUp() override {
135 int port = grpc_pick_unused_port_or_die();
136 server_address_ << "localhost:" << port;
138 ServerBuilder builder;
139 builder.AddListeningPort(server_address_.str(),
140 InsecureServerCredentials());
141 builder.RegisterService(&service_);
142 server_ = builder.BuildAndStart();
145 void TearDown() override { server_->Shutdown(); }
148 std::shared_ptr<Channel> channel = grpc::CreateChannel(
149 server_address_.str(), InsecureChannelCredentials());
150 stub_ = grpc::testing::EchoTestService::NewStub(channel);
153 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
154 std::unique_ptr<Server> server_;
155 std::ostringstream server_address_;
156 TestServiceImpl service_;
159 static void Drainer(ClientReaderWriter<EchoRequest, EchoResponse>* reader) {
160 EchoResponse response;
161 while (reader->Read(&response)) {
162 // Just drain out the responses as fast as possible.
166 TEST_F(End2endTest, StreamingThroughput) {
168 grpc::ClientContext context;
169 auto stream = stub_->BidiStream(&context);
171 auto reader = stream.get();
172 std::thread receiver(std::bind(Drainer, reader));
174 for (int i = 0; i < 10000; i++) {
176 request.set_message(kLargeString);
177 ASSERT_TRUE(stream->Write(request));
179 gpr_log(GPR_INFO, "Send count = %d", i);
182 stream->WritesDone();
186 } // namespace testing
189 int main(int argc, char** argv) {
190 grpc::testing::TestEnvironment env(argc, argv);
191 ::testing::InitGoogleTest(&argc, argv);
192 return RUN_ALL_TESTS();