-
Notifications
You must be signed in to change notification settings - Fork 12
/
bufferBandwidthBench.cpp
192 lines (162 loc) · 6.19 KB
/
bufferBandwidthBench.cpp
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
188
189
190
191
192
#include <include/DomainSocketsTransport.h>
#include <include/SharedMemoryTransport.h>
#include "include/RdmaTransport.h"
#include <thread>
#include <include/TcpTransport.h>
#include <util/doNotOptimize.h>
#include <util/ycsb.h>
#include "util/bench.h"
using namespace l5::transport;
constexpr size_t operator "" _k(unsigned long long i) { return i * 1024; }
constexpr size_t operator "" _m(unsigned long long i) { return i * 1024 * 1024; }
constexpr size_t operator "" _g(unsigned long long i) { return i * 1024 * 1024 * 1024; }
static constexpr auto printResults = []
(double workSize, auto avgTime, auto userPercent, auto systemPercent, auto totalPercent) {
std::cout << workSize / 1e6 << ", "
<< avgTime << ", "
<< (workSize / 1e6 / avgTime) << ", "
<< userPercent << ", "
<< systemPercent << ", "
<< totalPercent << '\n';
};
static constexpr uint16_t port = 1234;
static const char* ip = "127.0.0.1";
static constexpr size_t DATA_SIZE = 1_g;
static constexpr size_t BUFFER_SIZE = 256_m;
struct TestData {
std::vector<uint8_t> testdata;
explicit TestData(bool isClient) : testdata(DATA_SIZE) {
if (not isClient) {
RandomString rand;
rand.fill(DATA_SIZE, reinterpret_cast<char*>(testdata.data()));
}
}
TestData(const TestData &other) = delete;
TestData(TestData &&other) noexcept = delete;
TestData &operator=(const TestData &other) = delete;
TestData &operator=(TestData &&other) noexcept = delete;
auto begin() {
return testdata.begin();
}
auto end() {
return testdata.end();
}
auto size() {
return testdata.size();
}
auto data() {
return testdata.data();
}
};
template<class Server, class Client>
void
doRun(const std::string &name, bool isClient, const std::string &connection, TestData &testdata, size_t chunkSize) {
if (isClient) {
std::cout << "connecting to " << connection << "..." << std::flush;
auto client = Client();
for (int i = 0;; ++i) {
try {
client.connect(connection);
break;
} catch (...) {
client.reset();
std::cout << "." << std::flush;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
if (i > 10) throw;
}
}
std::cout << " connected!\n";
std::cout << "receiving " << DATA_SIZE / 1014. / 1024. / 1024. << "GB data from the server\n";
DoNotOptimize(testdata);
for (auto it = testdata.begin(); it < testdata.end();) {
auto toRead = std::min(chunkSize, static_cast<size_t>(std::distance(it, testdata.end())));
client.read(it.base(), toRead);
it += toRead;
}
ClobberMemory();
} else { // server
// immediately accept to not block the client
auto server = Server(connection);
server.accept();
if (chunkSize < 1_m) {
std::cout << name << ", " << chunkSize / 1024. << "KB, " << std::flush;
} else if (chunkSize < 1_g) {
std::cout << name << ", " << chunkSize / 1024. / 1024. << "MB, " << std::flush;
} else {
std::cout << name << ", " << chunkSize / 1024. / 1024. / 1024. << "GB, " << std::flush;
}
if (Server::buffer_size < 1_m) {
std::cout << Server::buffer_size / 1024. << "KB, " << std::flush;
} else if (Server::buffer_size < 1_g) {
std::cout << Server::buffer_size / 1024. / 1024. << "MB, " << std::flush;
} else {
std::cout << Server::buffer_size / 1024. / 1024. / 1024. << "GB, " << std::flush;
}
// cut the data into chunks
bench(testdata.size(), [&] {
for (auto it = testdata.begin(); it < testdata.end();) {
auto toSend = std::min(chunkSize, static_cast<size_t>(std::distance(it, testdata.end())));
server.write(it.base(), toSend);
it += toSend;
}
}, printResults);
}
}
template<template<size_t> typename Server,
template<size_t> typename Client, size_t bufferSize, size_t ...bufferSizes>
typename std::enable_if_t<sizeof...(bufferSizes) == 0>
doRunHelper(const std::string &name, bool isClient, const std::string &connection, TestData &testdata,
size_t chunkSize) {
if (bufferSize > chunkSize)
doRun<Server<bufferSize>, Client<bufferSize> >(name, isClient, connection, testdata, chunkSize);
}
template<template<size_t> typename Server,
template<size_t> typename Client, size_t bufferSize, size_t ...bufferSizes>
typename std::enable_if_t<sizeof...(bufferSizes) != 0>
doRunHelper(const std::string &name, bool isClient, const std::string &connection, TestData &testdata,
size_t chunkSize) {
doRunHelper<Server, Client, bufferSize>(name, isClient, connection, testdata, chunkSize);
doRunHelper<Server, Client, bufferSizes...>(name, isClient, connection, testdata, chunkSize);
}
int main(int argc, char** argv) {
if (argc < 2) {
std::cout << "Usage: " << argv[0] << " <client / server> <(optional) 127.0.0.1>" << std::endl;
return -1;
}
const auto isClient = argv[1][0] == 'c';
const auto isLocal = [&] {
if (argc > 2) {
ip = argv[2];
}
return strcmp("127.0.0.1", ip) == 0;
}();
const auto connection = [&] {
if (isClient) {
return ip + std::string(":") + std::to_string(port);
} else {
return std::to_string(port);
}
}();
auto testdata = TestData(isClient);
if (not isClient)
std::cout << "connection, chunk size, buffer size, MB transmitted, time, MB/s, user, system, total\n"
<< std::flush;
#define SIZES 4_k, 8_k, 16_k, 32_k, 64_k, 128_k, 256_k, 512_k, \
1_m, 2_m, 4_m, 8_m, 16_m, 32_m, 64_m, 128_m, 256_m, 512_m, \
1_g, 2_g
for (auto chunksize : {SIZES}) {
if (isLocal) {
doRunHelper<
SharedMemoryTransportServer,
SharedMemoryTransportClient,
SIZES
>("shared memory", isClient, "/tmp/testSocket", testdata, chunksize);
} else {
doRunHelper<
RdmaTransportServer,
RdmaTransportClient,
SIZES
>("rdma", isClient, connection, testdata, chunksize);
}
}
}