Skip to content
Snippets Groups Projects

[ConcurrentNetworkEchoTest] add optional computation per echo

Merged Florian Fischer requested to merge aj46ezos/emper:add-computation-to-echo-test into master
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright © 2020-2021 Florian Fischer
#include <sys/socket.h>
#include <array>
#include <chrono>
#include <compare>
#include <cstdlib>
#include <iostream>
#include <string>
@@ -26,11 +31,12 @@ auto main(int argc, char* argv[]) -> int {
unsigned int client_count = std::thread::hardware_concurrency() * 4;
unsigned int echos = EMPER_LOG_LEVEL > Info ? 250 : 25000;
unsigned int computation = 0;
std::string port = "4241";
std::string host = "127.0.0.1";
if (argc > 3) {
std::cerr << "Usage: " << argv[0] << " [clients] [echos]" << std::endl;
if (argc > 4) {
std::cerr << "Usage: " << argv[0] << " [clients] [echos] [computation]" << std::endl;
exit(EXIT_FAILURE);
}
@@ -43,13 +49,38 @@ auto main(int argc, char* argv[]) -> int {
echos = strtol(argv[2], nullptr, DECIMAL);
}
if (argc > 3) {
computation = strtol(argv[3], nullptr, DECIMAL);
}
std::cout << "ConcurrentNetworkEchoTest with " << client_count << " clients sending " << echos
<< " echos" << std::endl;
Runtime runtime;
auto serve_func = [&](int sock) {
std::array<char, BUF_SIZE> buf;
for (;;) {
ssize_t bytes_recv = emper::io::recvAndWait(sock, buf.data(), buf.size(), 0);
// socket was shutdown
if (bytes_recv == 0) return;
if (bytes_recv == -1) DIE_MSG_ERRNO("server read failed");
if (computation) {
const auto start = std::chrono::steady_clock::now();
const auto deadline = start + std::chrono::microseconds(computation);
// NOLINTNEXTLINE(modernize-use-nullptr)
while (std::chrono::steady_clock::now() < deadline) {
}
}
if (emper::io::sendAndWait(sock, buf.data(), bytes_recv, MSG_NOSIGNAL, true) == -1)
DIE_MSG_ERRNO("send failed");
}
};
Fiber* listener =
emper::io::tcp_listener(host, port, echo_serve, 1024, {emper::io::SockOpt::ReusePort});
emper::io::tcp_listener(host, port, serve_func, 1024, {emper::io::SockOpt::ReusePort});
if (!listener) {
DIE_MSG("failed to create listener");
}
Loading