From d7d109a15650b0840a7f814c25fe67dcc6056c24 Mon Sep 17 00:00:00 2001 From: Florian Fischer <florian.fischer@muhq.space> Date: Wed, 13 Oct 2021 12:14:07 +0200 Subject: [PATCH] [EchoServer] implement random computation in range --- apps/EchoServer.cpp | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/apps/EchoServer.cpp b/apps/EchoServer.cpp index c64a5949..f179dc60 100644 --- a/apps/EchoServer.cpp +++ b/apps/EchoServer.cpp @@ -14,6 +14,7 @@ #include "Common.hpp" #include "Debug.hpp" #include "Runtime.hpp" +#include "Worker.hpp" #include "emper-config.h" #include "io.hpp" @@ -26,6 +27,7 @@ static const std::string PORT = "12345"; static const int BACKLOG = 1024; static unsigned int computations_us = 0; +static unsigned int max_computations_us = 0; static std::atomic<bool> quit = false; @@ -33,8 +35,9 @@ auto main(int argc, char* argv[]) -> int { std::string host = HOST; std::string port = PORT; - if (argc > 3) { - std::cerr << "Usage: " << argv[0] << " [port] [computation_us]" << std::endl; + if (argc > 4) { + std::cerr << "Usage: " << argv[0] << " [port] [computations_us] [max_computations_us]" + << std::endl; exit(EXIT_FAILURE); } @@ -46,7 +49,19 @@ auto main(int argc, char* argv[]) -> int { computations_us = std::stoi(argv[2]); } - std::cout << "Echoserver listening on " << host << ":" << port << std::endl; + if (argc > 3) { + max_computations_us = std::stoi(argv[3]); + if (max_computations_us < computations_us) + DIE_MSG("max_computations_us must be bigger than computations_us"); + } + + std::cout << "Echoserver listening on " << host << ":" << port; + if (computations_us) { + std::cout << " with " << computations_us; + if (max_computations_us) std::cout << " - " << max_computations_us; + std::cout << " us computations"; + } + std::cout << std::endl; Runtime runtime; auto serverFunc = [](int socket) { @@ -68,12 +83,20 @@ auto main(int argc, char* argv[]) -> int { break; } - const auto start = std::chrono::steady_clock::now(); - const auto deadline = start + std::chrono::microseconds(computations_us); - // TODO: The suppressed linter error below may be a false positive - // reported by clang-tidy. - // NOLINTNEXTLINE(modernize-use-nullptr) - while (std::chrono::steady_clock::now() < deadline) { + if (computations_us) { + unsigned int computation = computations_us; + if (max_computations_us) { + // Worker::rand is always positiv. Why does it return int? + computation += Worker::rand() % (max_computations_us - computations_us); + } + + const auto start = std::chrono::steady_clock::now(); + const auto deadline = start + std::chrono::microseconds(computation); + // TODO: The suppressed linter error below may be a false positive + // reported by clang-tidy. + // NOLINTNEXTLINE(modernize-use-nullptr) + while (std::chrono::steady_clock::now() < deadline) { + } } ssize_t bytes_send = emper::io::sendAndWait(socket, buf, bytes_recv, MSG_NOSIGNAL, true); -- GitLab