-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbandwidthBench.cpp
132 lines (115 loc) · 3.84 KB
/
bandwidthBench.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
#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;
static constexpr uint16_t port = 1234;
static const char* ip = "127.0.0.1";
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';
};
template<class Server, class Client>
void doRun(const std::string &name, bool isClient, std::string connection, size_t size) {
std::vector<uint8_t> testdata(size);
if (isClient) {
std::cout << "connecting to " << connection << "..." << std::flush;
sleep(1);
auto client = Client();
for (int i = 0;; ++i) {
try {
client.connect(connection);
break;
} catch (...) {
std::this_thread::sleep_for(std::chrono::milliseconds(20));
if (i > 10) throw;
}
}
std::cout << " connected!\n";
std::cout << "receiving " << size << "B data from the server\n";
client.read(testdata.data(), size);
DoNotOptimize(testdata);
ClobberMemory();
} else { // server
// immediately accept to not block the client
auto server = Server(connection);
server.accept();
RandomString rand;
rand.fill(size, reinterpret_cast<char*>(testdata.data()));
std::cout << name << ", " << std::flush;
bench(testdata.size(), [&] {
server.write(testdata.data(), testdata.size());
}, printResults);
}
}
int main(int argc, char** argv) {
if (argc < 3) {
std::cout << "Usage: " << argv[0] << " <client / server> messagesize <(optional) 127.0.0.1>" << std::endl;
return -1;
}
const auto isClient = argv[1][0] == 'c';
const auto size = [&] {
auto specifiedSize = std::string(argv[2]);
size_t multiplier;
switch (specifiedSize.back()) {
case 'k':
case 'K':
multiplier = 1024;
break;
case 'm':
case 'M':
multiplier = 1024 * 1024;
break;
case 'g':
case 'G':
multiplier = 1024 * 1024 * 1024;
break;
default:
multiplier = 1;
}
specifiedSize.pop_back();
return stoi(specifiedSize) * multiplier;
}();
const auto isLocal = [&] {
if (argc > 3) {
ip = argv[3];
}
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);
}
}();
if (not isClient) std::cout << "connection, MB, time, MB/s, user, system, total\n";
if (isLocal) {
doRun<DomainSocketsTransportServer,
DomainSocketsTransportClient
>("domainSocket", isClient, "/tmp/testSocket", size);
doRun<SharedMemoryTransportServer<512_m>,
SharedMemoryTransportClient<512_m>
>("shared memory", isClient, "/tmp/testSocket", size);
}
doRun<TcpTransportServer,
TcpTransportClient
>("tcp", isClient, connection, size);
if (not isLocal) {
doRun<RdmaTransportServer<512_m>,
RdmaTransportClient<512_m>
>("rdma", isClient, connection, size);
}
}