-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.cc
161 lines (138 loc) · 5.26 KB
/
client.cc
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
#include "core/print.hh"
#include "core/reactor.hh"
#include "core/app-template.hh"
#include "core/future-util.hh"
#include "core/distributed.hh"
#include "core/semaphore.hh"
#include "core/future-util.hh"
#include <chrono>
#include <array>
using namespace seastar;
static size_t buf_size = 128; //128; //128;//1600;
static std::string str_txbuf(buf_size, 'X');
class tcp_echo_client {
private:
unsigned _conn_per_core {0};
unsigned _reqs_per_conn {0};
std::vector<connected_socket> _sockets;
semaphore _conn_connected {0};
semaphore _conn_finished {0};
timer<> _run_timer;
bool _timer_done {false};
uint64_t _total_reqs {0};
public:
tcp_echo_client(unsigned total_conn, unsigned reqs_per_conn)
: _conn_per_core(total_conn / smp::count)
, _reqs_per_conn(reqs_per_conn)
, _run_timer([this] { _timer_done = true; })
{
}
class connection {
public:
connected_socket _fd;
input_stream<char> _in;
output_stream<char> _out;
tcp_echo_client& _echo_client;
uint64_t _nr_done{0};
public:
connection(connected_socket&& fd, tcp_echo_client& echo_client)
: _fd(std::move(fd))
, _in(_fd.input())
, _out(_fd.output())
, _echo_client(echo_client)
{
}
~connection() {
}
future<> do_launch_request() {
net::fragment frag { const_cast<char*>(str_txbuf.c_str()), buf_size };
net::packet pack(frag, deleter());
return _out.write(std::move(pack)).then([this] {
return _out.flush();
}).then([this] {
return _in.read_exactly(buf_size).then([this] (auto&& data) {
_nr_done++;
if (_echo_client.done(_nr_done)) {
return make_ready_future<>();
}
return this->do_launch_request();
});
});
}
};
future<uint64_t> total_reqs() {
print("Requests on cpu %2d: %ld\n", engine().cpu_id(), _total_reqs);
return make_ready_future<uint64_t>(_total_reqs);
}
bool done(uint64_t nr_done) {
return nr_done >= _reqs_per_conn;
}
future<> connect(ipv4_addr server_addr) {
for (unsigned i = 0; i < _conn_per_core; i++) {
engine().net().connect(make_ipv4_address(server_addr)).then([this] (connected_socket fd) {
_sockets.push_back(std::move(fd));
print("Established connection %6d on cpu %3d\n", _conn_connected.current(), engine().cpu_id());
_conn_connected.signal();
}).or_terminate();
}
return _conn_connected.wait(_conn_per_core);
}
future<> run() {
print("Established all %6d tcp connections on cpu %3d\n", _conn_per_core, engine().cpu_id());
for (auto&& fd : _sockets) {
auto conn = make_lw_shared<connection>(std::move(fd), *this);
// read
keep_doing([conn, this] () {
return conn->_in.read_exactly(buf_size).then([this] (auto&& data) {
});
}).then([conn] {
conn->_out.close();
});
// write
keep_doing([conn, this] () {
net::fragment frag { const_cast<char*>(str_txbuf.c_str()), buf_size };
net::packet pack(frag, deleter());
return conn->_out.write(std::move(pack)).then([this, conn] {
conn->_out.flush();
});
}).then([conn] {
conn->_out.close();
});
}
return _conn_finished.wait(_conn_per_core);
}
future<> stop() {
return make_ready_future();
}
};
namespace bpo = boost::program_options;
int main(int ac, char** av) {
distributed<tcp_echo_client> shard_echo_client;
app_template app;
app.add_options()
("server,s", bpo::value<std::string>()->default_value("127.0.0.1:10000"), "Server address")
("conn,c", bpo::value<unsigned>()->default_value(100), "total connections")
("reqs,r", bpo::value<unsigned>()->default_value(0), "reqs per connection");
// run app
return app.run(ac, av, [&] () -> future<int> {
auto& config = app.configuration();
auto server = config["server"].as<std::string>();
auto reqs_per_conn = config["reqs"].as<unsigned>();
auto total_conn= config["conn"].as<unsigned>();
if (total_conn % smp::count != 0) {
print("Error: conn needs to be n * cpu_nr\n");
return make_ready_future<int>(-1);
}
return shard_echo_client.start(std::move(total_conn), std::move(reqs_per_conn)).then([&shard_echo_client, server] {
return shard_echo_client.invoke_on_all(&tcp_echo_client::connect, ipv4_addr{server});
}).then([&shard_echo_client] {
return shard_echo_client.invoke_on_all(&tcp_echo_client::run);
}).then([&shard_echo_client] {
return shard_echo_client.map_reduce(adder<uint64_t>(), &tcp_echo_client::total_reqs);
}).then([&shard_echo_client] (auto r) {
return shard_echo_client.stop().then([] {
return make_ready_future<int>(0);
});
});
});
}