-
Notifications
You must be signed in to change notification settings - Fork 12
/
blockedBandwidthBench.cpp
159 lines (135 loc) · 5.07 KB
/
blockedBandwidthBench.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
#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 = 10_g;
static constexpr size_t BUFFER_SIZE = 256_m;
struct TestData {
std::vector<uint8_t> testdata;
TestData(bool isClient) : testdata(DATA_SIZE) {
if (not isClient) {
RandomString rand;
rand.fill(DATA_SIZE, reinterpret_cast<char*>(testdata.data()));
}
}
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 (...) {
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;
}
// 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);
}
}
// usually the server sends the data "en bloc" -> client can only read after the server finishes (buffersize) memcopy
// in this test, we do a separate measurement with fixed buffersize (e.g. 256M) and varying blocksizes
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 [MB], MB transmitted, time, MB/s, user, system, total\n" << std::flush;
for (auto i : {1_k, 2_k, 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}) {
if (isLocal) {
doRun<SharedMemoryTransportServer<BUFFER_SIZE>,
SharedMemoryTransportClient<BUFFER_SIZE>
>("shared memory", isClient, "/tmp/testSocket", testdata, i);
doRun<DomainSocketsTransportServer,
DomainSocketsTransportClient
>("domain socket", isClient, "/tmp/testSocket", testdata, i);
doRun<TcpTransportServer,
TcpTransportClient
>(" tcp", isClient, connection, testdata, i);
} else {
doRun<RdmaTransportServer<BUFFER_SIZE>,
RdmaTransportClient<BUFFER_SIZE>
>("rdma", isClient, connection, testdata, i);
}
}
}