Skip to content
Snippets Groups Projects
Commit d7d109a1 authored by Florian Fischer's avatar Florian Fischer
Browse files

[EchoServer] implement random computation in range

parent 9527d2f4
No related branches found
No related tags found
No related merge requests found
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "Common.hpp" #include "Common.hpp"
#include "Debug.hpp" #include "Debug.hpp"
#include "Runtime.hpp" #include "Runtime.hpp"
#include "Worker.hpp"
#include "emper-config.h" #include "emper-config.h"
#include "io.hpp" #include "io.hpp"
...@@ -26,6 +27,7 @@ static const std::string PORT = "12345"; ...@@ -26,6 +27,7 @@ static const std::string PORT = "12345";
static const int BACKLOG = 1024; static const int BACKLOG = 1024;
static unsigned int computations_us = 0; static unsigned int computations_us = 0;
static unsigned int max_computations_us = 0;
static std::atomic<bool> quit = false; static std::atomic<bool> quit = false;
...@@ -33,8 +35,9 @@ auto main(int argc, char* argv[]) -> int { ...@@ -33,8 +35,9 @@ auto main(int argc, char* argv[]) -> int {
std::string host = HOST; std::string host = HOST;
std::string port = PORT; std::string port = PORT;
if (argc > 3) { if (argc > 4) {
std::cerr << "Usage: " << argv[0] << " [port] [computation_us]" << std::endl; std::cerr << "Usage: " << argv[0] << " [port] [computations_us] [max_computations_us]"
<< std::endl;
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
...@@ -46,7 +49,19 @@ auto main(int argc, char* argv[]) -> int { ...@@ -46,7 +49,19 @@ auto main(int argc, char* argv[]) -> int {
computations_us = std::stoi(argv[2]); 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; Runtime runtime;
auto serverFunc = [](int socket) { auto serverFunc = [](int socket) {
...@@ -68,12 +83,20 @@ auto main(int argc, char* argv[]) -> int { ...@@ -68,12 +83,20 @@ auto main(int argc, char* argv[]) -> int {
break; break;
} }
const auto start = std::chrono::steady_clock::now(); if (computations_us) {
const auto deadline = start + std::chrono::microseconds(computations_us); unsigned int computation = computations_us;
// TODO: The suppressed linter error below may be a false positive if (max_computations_us) {
// reported by clang-tidy. // Worker::rand is always positiv. Why does it return int?
// NOLINTNEXTLINE(modernize-use-nullptr) computation += Worker::rand() % (max_computations_us - computations_us);
while (std::chrono::steady_clock::now() < deadline) { }
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); ssize_t bytes_send = emper::io::sendAndWait(socket, buf, bytes_recv, MSG_NOSIGNAL, true);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment