Skip to content

Commit f8a62a5

Browse files
committed
tests/os-namedsocket/raw-data: Slightly modify test for higher throughput
1 parent 55e5a8d commit f8a62a5

File tree

2 files changed

+49
-20
lines changed

2 files changed

+49
-20
lines changed

tests/os-namedsocket/raw-data/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 3.5)
2-
project(os-namedsocket_test_raw-data)
2+
project(test_os-namedsocket_raw-data)
33

44
ADD_SUBDIRECTORY("../../../" "lib-streamlabs-ipc")
55

tests/os-namedsocket/raw-data/test.cpp

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static void blog(const char* format, ...) {
2323
va_start(args, format);
2424
std::string text = varlog(format, args);
2525
va_end(args);
26-
26+
2727
auto timeSinceStart = (std::chrono::high_resolution_clock::now() - tp);
2828
auto hours = std::chrono::duration_cast<std::chrono::hours>(timeSinceStart);
2929
timeSinceStart -= hours;
@@ -36,7 +36,7 @@ static void blog(const char* format, ...) {
3636
auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(timeSinceStart);
3737
timeSinceStart -= microseconds;
3838
auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(timeSinceStart);
39-
39+
4040
std::vector<char> timebuf(65535, '\0');
4141
std::string timeformat = "%.2d:%.2d:%.2d.%.3d.%.3d.%.3d: %*s\n";// "%*s";
4242
sprintf_s(
@@ -177,22 +177,30 @@ static int server(int argc, char* argv[]);
177177
static int client(int argc, char* argv[]);
178178

179179
int main(int argc, char* argv[]) {
180-
if ((argc == 2) || (strcmp(argv[0], "client") == 0)) {
180+
if ((argc >= 2) || (strcmp(argv[0], "client") == 0)) {
181181
client(argc, argv);
182182
} else {
183183
server(argc, argv);
184184
}
185185
}
186186

187187
int serverInstanceThread(std::shared_ptr<os::named_socket_connection> ptr) {
188+
size_t msgCount = 0;
189+
std::vector<char> buf;
190+
size_t avail;
188191
while (ptr->good()) {
189-
size_t msg = ptr->read_avail();
190-
if (msg > 0) {
191-
ptr->write(ptr->read());
192-
//blog("Reflected message with side %" PRIu64 ".", msg);
192+
while ((avail = ptr->read_avail()) > 0) {
193+
buf.resize(avail);
194+
size_t msg = ptr->read(buf.data(), avail);
195+
if (ptr->write(buf) != msg) {
196+
blog("%llu: Send Buffer full at %llu messages.", ptr->get_client_id(), msgCount);
197+
break;
198+
};
199+
msgCount++;
193200
}
194201
if (ptr->read_avail() == 0)
195202
std::this_thread::sleep_for(std::chrono::milliseconds(1));
203+
//blog("%llu: Total messages: %llu", ptr->get_client_id(), msgCount);
196204
}
197205
return 0;
198206
}
@@ -209,7 +217,7 @@ int server(int argc, char* argv[]) {
209217

210218
blog("Spawning %lld clients.", (int64_t)CLIENTCOUNT);
211219
for (size_t idx = 0; idx < CLIENTCOUNT; idx++) {
212-
spawn(argv[0], std::string(argv[0]) + "client", get_working_directory());
220+
spawn(std::string(argv[0]), '"' + std::string(argv[0]) + '"' + " client", get_working_directory());
213221
}
214222

215223
blog("Waiting for data...");
@@ -245,32 +253,42 @@ int client(int argc, char* argv[]) {
245253
std::unique_ptr<os::named_socket> socket = os::named_socket::create();
246254
if (!socket->connect(CONN)) {
247255
blog("Failed starting client.");
248-
std::cin.get();
249256
return -1;
250257
}
251258

252259
uint64_t inbox = 0;
253260
uint64_t outbox = 0;
254-
uint64_t total = 10000;
261+
uint64_t total = 100000;
262+
auto tpstart = std::chrono::high_resolution_clock::now();
263+
std::vector<char> buf;
255264
while (socket->get_connection()->good()) {
256265
//std::cout << inbox << ", " << outbox << ", " << total << "." << std::endl;
257266
if (outbox < total) {
258-
if (socket->get_connection()->write("Hello World\0", 11) == 11) {
259-
outbox++;
260-
if (outbox % 100 == 0) {
261-
blog("Sent %lld messages so far.", outbox, 0, 0, 0, 0, 0);
267+
for (size_t idx = 0; idx < 1000; idx++) {
268+
if (socket->get_connection()->write("Hello World\0", 11) == 11) {
269+
outbox++;
270+
} else {
271+
break;
262272
}
273+
274+
if (outbox >= total) {
275+
break;
276+
}
277+
//if (outbox % 100 == 0) {
278+
// blog("Sent %lld messages so far.", outbox, 0, 0, 0, 0, 0);
279+
//}
263280
}
264281
}
265282

266283
if (inbox < total) {
267284
size_t msg = socket->get_connection()->read_avail();
268285
while (msg > 0) {
269-
auto buf = socket->get_connection()->read();
286+
buf.resize(msg);
287+
auto rbytes = socket->get_connection()->read(buf.data(), buf.size());
270288
inbox++;
271-
if (inbox % 100 == 0) {
272-
blog("Received %lld messages so far.", inbox, 0, 0, 0, 0, 0);
273-
}
289+
//if (inbox % 100 == 0) {
290+
// blog("Received %lld messages so far.", inbox, 0, 0, 0, 0, 0);
291+
//}
274292
msg = socket->get_connection()->read_avail();
275293
}
276294
}
@@ -281,9 +299,20 @@ int client(int argc, char* argv[]) {
281299
}
282300
}
283301

302+
//std::this_thread::sleep_for(std::chrono::milliseconds(1));
303+
}
304+
auto tpend = std::chrono::high_resolution_clock::now();
305+
306+
auto tpdurns = std::chrono::duration_cast<std::chrono::nanoseconds>(tpend - tpstart);
307+
auto tpdurms = std::chrono::duration_cast<std::chrono::milliseconds>(tpend - tpstart);
308+
309+
blog("Sent & Received %llu messages in %llu milliseconds.", total, tpdurms.count());
310+
blog("Average %llu ns per message.", tpdurns.count() / total);
311+
312+
while (socket->get_connection()->good()) {
284313
std::this_thread::sleep_for(std::chrono::milliseconds(1));
285314
}
286-
socket->close();
287315

316+
socket->close();
288317
return 0;
289318
}

0 commit comments

Comments
 (0)