forked from rsocket/rsocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamThroughputMemory.cpp
More file actions
187 lines (142 loc) · 5.16 KB
/
StreamThroughputMemory.cpp
File metadata and controls
187 lines (142 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) Facebook, Inc. and its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "rsocket/benchmarks/Throughput.h"
#include <folly/Benchmark.h>
#include <folly/Synchronized.h>
#include <folly/init/Init.h>
#include <folly/io/async/ScopedEventBaseThread.h>
#include <folly/portability/GFlags.h>
#include <folly/synchronization/Baton.h>
#include "rsocket/RSocket.h"
#include "yarpl/Flowable.h"
using namespace rsocket;
constexpr size_t kMessageLen = 32;
DEFINE_int32(items, 1000000, "number of items in stream");
namespace {
/// State shared across the client and server DirectDuplexConnections.
struct State {
/// Whether one of the two connections has been destroyed.
folly::Synchronized<bool> destroyed;
};
/// DuplexConnection that talks to another DuplexConnection via memory.
class DirectDuplexConnection : public DuplexConnection {
public:
DirectDuplexConnection(std::shared_ptr<State> state, folly::EventBase& evb)
: state_{std::move(state)}, evb_{evb} {}
~DirectDuplexConnection() override {
*state_->destroyed.wlock() = true;
}
// Tie two DirectDuplexConnections together so they can talk to each other.
void tie(DirectDuplexConnection* other) {
other_ = other;
other_->other_ = this;
}
void setInput(std::shared_ptr<DuplexConnection::Subscriber> input) override {
input_ = std::move(input);
}
void send(std::unique_ptr<folly::IOBuf> buf) override {
auto destroyed = state_->destroyed.rlock();
if (*destroyed || !other_) {
return;
}
other_->evb_.runInEventBaseThread(
[state = state_, other = other_, b = std::move(buf)]() mutable {
auto destroyed = state->destroyed.rlock();
if (*destroyed) {
return;
}
other->input_->onNext(std::move(b));
});
}
private:
std::shared_ptr<State> state_;
folly::EventBase& evb_;
DirectDuplexConnection* other_{nullptr};
std::shared_ptr<DuplexConnection::Subscriber> input_;
};
class Acceptor : public ConnectionAcceptor {
public:
explicit Acceptor(std::shared_ptr<State> state) : state_{std::move(state)} {}
void setClientConnection(DirectDuplexConnection* connection) {
client_ = connection;
}
void start(OnDuplexConnectionAccept onAccept) override {
worker_.getEventBase()->runInEventBaseThread(
[this, onAccept = std::move(onAccept)]() mutable {
auto server = std::make_unique<DirectDuplexConnection>(
std::move(state_), *worker_.getEventBase());
server->tie(client_);
onAccept(std::move(server), *worker_.getEventBase());
});
}
void stop() override {}
folly::Optional<uint16_t> listeningPort() const override {
return folly::none;
}
private:
std::shared_ptr<State> state_;
DirectDuplexConnection* client_{nullptr};
folly::ScopedEventBaseThread worker_;
};
class Factory : public ConnectionFactory {
public:
Factory() {
auto state = std::make_shared<State>();
connection_ = std::make_unique<DirectDuplexConnection>(
state, *worker_.getEventBase());
auto acceptor = std::make_unique<Acceptor>(state);
acceptor_ = acceptor.get();
acceptor_->setClientConnection(connection_.get());
auto responder =
std::make_shared<FixedResponder>(std::string(kMessageLen, 'a'));
server_ = std::make_unique<RSocketServer>(std::move(acceptor));
server_->start([responder](const SetupParameters&) { return responder; });
}
folly::Future<ConnectedDuplexConnection> connect(
ProtocolVersion,
ResumeStatus /* unused */) override {
return folly::via(worker_.getEventBase(), [this] {
return ConnectedDuplexConnection{std::move(connection_),
*worker_.getEventBase()};
});
}
private:
std::unique_ptr<DirectDuplexConnection> connection_;
std::unique_ptr<rsocket::RSocketServer> server_;
Acceptor* acceptor_{nullptr};
folly::ScopedEventBaseThread worker_;
};
std::shared_ptr<RSocketClient> makeClient() {
auto factory = std::make_unique<Factory>();
return RSocket::createConnectedClient(std::move(factory)).get();
}
} // namespace
BENCHMARK(StreamThroughput, n) {
(void)n;
std::shared_ptr<RSocketClient> client;
std::shared_ptr<BoundedSubscriber> subscriber;
folly::ScopedEventBaseThread worker;
Latch latch{1};
BENCHMARK_SUSPEND {
LOG(INFO) << " Running with " << FLAGS_items << " items";
client = makeClient();
}
client->getRequester()
->requestStream(Payload("InMemoryStream"))
->subscribe(std::make_shared<BoundedSubscriber>(latch, FLAGS_items));
constexpr std::chrono::minutes timeout{5};
if (!latch.timed_wait(timeout)) {
LOG(ERROR) << "Timed out!";
}
}