Rakesh Iyer | 4abe76e | 2015-08-26 11:43:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
murgatroid99 | ace28d3 | 2016-01-14 10:12:10 -0800 | [diff] [blame^] | 3 | * Copyright 2015-2016, Google Inc. |
Rakesh Iyer | 4abe76e | 2015-08-26 11:43:53 -0700 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Sree Kuchibhotla | b0d0c8e | 2016-01-13 22:52:17 -0800 | [diff] [blame] | 34 | #include <time.h> |
Rakesh Iyer | 4abe76e | 2015-08-26 11:43:53 -0700 | [diff] [blame] | 35 | #include <mutex> |
| 36 | #include <thread> |
| 37 | |
Rakesh Iyer | 4abe76e | 2015-08-26 11:43:53 -0700 | [diff] [blame] | 38 | #include <grpc++/channel.h> |
| 39 | #include <grpc++/client_context.h> |
| 40 | #include <grpc++/create_channel.h> |
Craig Tiller | 93ddc61 | 2015-09-02 07:57:48 -0700 | [diff] [blame] | 41 | #include <grpc++/security/credentials.h> |
| 42 | #include <grpc++/security/server_credentials.h> |
Rakesh Iyer | 4abe76e | 2015-08-26 11:43:53 -0700 | [diff] [blame] | 43 | #include <grpc++/server.h> |
| 44 | #include <grpc++/server_builder.h> |
| 45 | #include <grpc++/server_context.h> |
Craig Tiller | 93ddc61 | 2015-09-02 07:57:48 -0700 | [diff] [blame] | 46 | #include <grpc/grpc.h> |
Vijay Pai | 8423203 | 2015-09-29 23:16:08 +0000 | [diff] [blame] | 47 | #include <grpc/support/atm.h> |
Craig Tiller | 93ddc61 | 2015-09-02 07:57:48 -0700 | [diff] [blame] | 48 | #include <grpc/support/thd.h> |
| 49 | #include <grpc/support/time.h> |
Rakesh Iyer | 4abe76e | 2015-08-26 11:43:53 -0700 | [diff] [blame] | 50 | #include <gtest/gtest.h> |
| 51 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 52 | #include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h" |
| 53 | #include "src/proto/grpc/testing/echo.grpc.pb.h" |
Sree Kuchibhotla | b0d0c8e | 2016-01-13 22:52:17 -0800 | [diff] [blame] | 54 | #include "test/core/util/port.h" |
| 55 | #include "test/core/util/test_config.h" |
Rakesh Iyer | 4abe76e | 2015-08-26 11:43:53 -0700 | [diff] [blame] | 56 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 57 | using grpc::testing::EchoRequest; |
| 58 | using grpc::testing::EchoResponse; |
Rakesh Iyer | 4abe76e | 2015-08-26 11:43:53 -0700 | [diff] [blame] | 59 | using std::chrono::system_clock; |
| 60 | |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 61 | const char* kLargeString = |
| 62 | "(" |
| 63 | "To be, or not to be- that is the question:" |
| 64 | "Whether 'tis nobler in the mind to suffer" |
| 65 | "The slings and arrows of outrageous fortune" |
| 66 | "Or to take arms against a sea of troubles," |
| 67 | "And by opposing end them. To die- to sleep-" |
| 68 | "No more; and by a sleep to say we end" |
| 69 | "The heartache, and the thousand natural shock" |
| 70 | "That flesh is heir to. 'Tis a consummation" |
| 71 | "Devoutly to be wish'd. To die- to sleep." |
| 72 | "To sleep- perchance to dream: ay, there's the rub!" |
| 73 | "For in that sleep of death what dreams may come" |
| 74 | "When we have shuffled off this mortal coil," |
| 75 | "Must give us pause. There's the respect" |
| 76 | "That makes calamity of so long life." |
| 77 | "For who would bear the whips and scorns of time," |
| 78 | "Th' oppressor's wrong, the proud man's contumely," |
| 79 | "The pangs of despis'd love, the law's delay," |
| 80 | "The insolence of office, and the spurns" |
| 81 | "That patient merit of th' unworthy takes," |
| 82 | "When he himself might his quietus make" |
| 83 | "With a bare bodkin? Who would these fardels bear," |
| 84 | "To grunt and sweat under a weary life," |
| 85 | "But that the dread of something after death-" |
| 86 | "The undiscover'd country, from whose bourn" |
| 87 | "No traveller returns- puzzles the will," |
| 88 | "And makes us rather bear those ills we have" |
| 89 | "Than fly to others that we know not of?" |
| 90 | "Thus conscience does make cowards of us all," |
| 91 | "And thus the native hue of resolution" |
| 92 | "Is sicklied o'er with the pale cast of thought," |
| 93 | "And enterprises of great pith and moment" |
| 94 | "With this regard their currents turn awry" |
| 95 | "And lose the name of action.- Soft you now!" |
| 96 | "The fair Ophelia!- Nymph, in thy orisons" |
| 97 | "Be all my sins rememb'red."; |
Rakesh Iyer | 4abe76e | 2015-08-26 11:43:53 -0700 | [diff] [blame] | 98 | |
| 99 | namespace grpc { |
| 100 | namespace testing { |
| 101 | |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 102 | class TestServiceImpl : public ::grpc::testing::EchoTestService::Service { |
Rakesh Iyer | 4abe76e | 2015-08-26 11:43:53 -0700 | [diff] [blame] | 103 | public: |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 104 | static void BidiStream_Sender( |
| 105 | ServerReaderWriter<EchoResponse, EchoRequest>* stream, |
Craig Tiller | b7a5977 | 2015-10-01 08:01:58 -0700 | [diff] [blame] | 106 | gpr_atm* should_exit) { |
Craig Tiller | a4517a2 | 2015-08-31 07:29:12 -0700 | [diff] [blame] | 107 | EchoResponse response; |
| 108 | response.set_message(kLargeString); |
Vijay Pai | 8423203 | 2015-09-29 23:16:08 +0000 | [diff] [blame] | 109 | while (gpr_atm_acq_load(should_exit) == static_cast<gpr_atm>(0)) { |
Craig Tiller | b7a5977 | 2015-10-01 08:01:58 -0700 | [diff] [blame] | 110 | struct timespec tv = {0, 1000000}; // 1 ms |
Vijay Pai | 8423203 | 2015-09-29 23:16:08 +0000 | [diff] [blame] | 111 | struct timespec rem; |
| 112 | // TODO (vpai): Mark this blocking |
| 113 | while (nanosleep(&tv, &rem) != 0) { |
Craig Tiller | b7a5977 | 2015-10-01 08:01:58 -0700 | [diff] [blame] | 114 | tv = rem; |
Vijay Pai | 8423203 | 2015-09-29 23:16:08 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
Craig Tiller | a4517a2 | 2015-08-31 07:29:12 -0700 | [diff] [blame] | 117 | stream->Write(response); |
| 118 | } |
| 119 | } |
| 120 | |
Rakesh Iyer | 4abe76e | 2015-08-26 11:43:53 -0700 | [diff] [blame] | 121 | // Only implement the one method we will be calling for brevity. |
| 122 | Status BidiStream(ServerContext* context, |
| 123 | ServerReaderWriter<EchoResponse, EchoRequest>* stream) |
| 124 | GRPC_OVERRIDE { |
| 125 | EchoRequest request; |
Vijay Pai | 8423203 | 2015-09-29 23:16:08 +0000 | [diff] [blame] | 126 | gpr_atm should_exit; |
| 127 | gpr_atm_rel_store(&should_exit, static_cast<gpr_atm>(0)); |
| 128 | |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 129 | std::thread sender( |
| 130 | std::bind(&TestServiceImpl::BidiStream_Sender, stream, &should_exit)); |
Rakesh Iyer | 4abe76e | 2015-08-26 11:43:53 -0700 | [diff] [blame] | 131 | |
| 132 | while (stream->Read(&request)) { |
Craig Tiller | b7a5977 | 2015-10-01 08:01:58 -0700 | [diff] [blame] | 133 | struct timespec tv = {0, 3000000}; // 3 ms |
Vijay Pai | 8423203 | 2015-09-29 23:16:08 +0000 | [diff] [blame] | 134 | struct timespec rem; |
| 135 | // TODO (vpai): Mark this blocking |
| 136 | while (nanosleep(&tv, &rem) != 0) { |
Craig Tiller | b7a5977 | 2015-10-01 08:01:58 -0700 | [diff] [blame] | 137 | tv = rem; |
Vijay Pai | 8423203 | 2015-09-29 23:16:08 +0000 | [diff] [blame] | 138 | }; |
Rakesh Iyer | 4abe76e | 2015-08-26 11:43:53 -0700 | [diff] [blame] | 139 | } |
Vijay Pai | 8423203 | 2015-09-29 23:16:08 +0000 | [diff] [blame] | 140 | gpr_atm_rel_store(&should_exit, static_cast<gpr_atm>(1)); |
Rakesh Iyer | 4abe76e | 2015-08-26 11:43:53 -0700 | [diff] [blame] | 141 | sender.join(); |
| 142 | return Status::OK; |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | class End2endTest : public ::testing::Test { |
| 147 | protected: |
| 148 | void SetUp() GRPC_OVERRIDE { |
| 149 | int port = grpc_pick_unused_port_or_die(); |
| 150 | server_address_ << "localhost:" << port; |
| 151 | // Setup server |
| 152 | ServerBuilder builder; |
| 153 | builder.AddListeningPort(server_address_.str(), |
| 154 | InsecureServerCredentials()); |
| 155 | builder.RegisterService(&service_); |
| 156 | server_ = builder.BuildAndStart(); |
| 157 | } |
| 158 | |
| 159 | void TearDown() GRPC_OVERRIDE { server_->Shutdown(); } |
| 160 | |
| 161 | void ResetStub() { |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 162 | std::shared_ptr<Channel> channel = |
Julien Boeuf | e5adc0e | 2015-10-12 14:08:10 -0700 | [diff] [blame] | 163 | CreateChannel(server_address_.str(), InsecureChannelCredentials()); |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 164 | stub_ = grpc::testing::EchoTestService::NewStub(channel); |
Rakesh Iyer | 4abe76e | 2015-08-26 11:43:53 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 167 | std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; |
Rakesh Iyer | 4abe76e | 2015-08-26 11:43:53 -0700 | [diff] [blame] | 168 | std::unique_ptr<Server> server_; |
| 169 | std::ostringstream server_address_; |
| 170 | TestServiceImpl service_; |
| 171 | }; |
| 172 | |
Craig Tiller | 93ddc61 | 2015-09-02 07:57:48 -0700 | [diff] [blame] | 173 | static void Drainer(ClientReaderWriter<EchoRequest, EchoResponse>* reader) { |
Craig Tiller | a4517a2 | 2015-08-31 07:29:12 -0700 | [diff] [blame] | 174 | EchoResponse response; |
| 175 | while (reader->Read(&response)) { |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 176 | // Just drain out the responses as fast as possible. |
Craig Tiller | a4517a2 | 2015-08-31 07:29:12 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Rakesh Iyer | 4abe76e | 2015-08-26 11:43:53 -0700 | [diff] [blame] | 180 | TEST_F(End2endTest, StreamingThroughput) { |
| 181 | ResetStub(); |
| 182 | grpc::ClientContext context; |
| 183 | auto stream = stub_->BidiStream(&context); |
| 184 | |
| 185 | auto reader = stream.get(); |
Craig Tiller | a4517a2 | 2015-08-31 07:29:12 -0700 | [diff] [blame] | 186 | std::thread receiver(std::bind(Drainer, reader)); |
Rakesh Iyer | 4abe76e | 2015-08-26 11:43:53 -0700 | [diff] [blame] | 187 | |
| 188 | for (int i = 0; i < 10000; i++) { |
| 189 | EchoRequest request; |
| 190 | request.set_message(kLargeString); |
| 191 | ASSERT_TRUE(stream->Write(request)); |
| 192 | if (i % 1000 == 0) { |
| 193 | gpr_log(GPR_INFO, "Send count = %d", i); |
| 194 | } |
| 195 | } |
| 196 | stream->WritesDone(); |
| 197 | receiver.join(); |
| 198 | } |
| 199 | |
| 200 | } // namespace testing |
| 201 | } // namespace grpc |
| 202 | |
| 203 | int main(int argc, char** argv) { |
| 204 | grpc_test_init(argc, argv); |
| 205 | ::testing::InitGoogleTest(&argc, argv); |
| 206 | return RUN_ALL_TESTS(); |
murgatroid99 | ace28d3 | 2016-01-14 10:12:10 -0800 | [diff] [blame^] | 207 | } |