diff --git a/apps/EchoClient.cpp b/apps/EchoClient.cpp index ea4b354c2e34c7590a39f76a1f71bef325581816..957bdd2bd6b885d2f672dfe1782ecdd32322ed3d 100644 --- a/apps/EchoClient.cpp +++ b/apps/EchoClient.cpp @@ -1,7 +1,10 @@ // SPDX-License-Identifier: LGPL-3.0-or-later // Copyright © 2021 Florian Fischer +#include <fcntl.h> #include <netdb.h> //for getaddrinfo #include <sys/socket.h> // for shutdown, socket, AF_INET +#include <sys/stat.h> +#include <unistd.h> #include <algorithm> // for find #include <atomic> @@ -158,7 +161,7 @@ static auto existsOption(int argc, char** argv, const std::string& option) -> bo static void printUsage(char* name) { std::cerr << "Usage: " << name << "[-h] [-p <port>] [-c <clients>] [-i <iterations>] [-a <address>] [-s <size>] [-b " - "<server backlog>]" + "<server backlog>] [-f <output-file>]" << std::endl; } @@ -198,6 +201,17 @@ auto main(int argc, char* argv[]) -> int { server_backlog = strtol(server_backlog_s, nullptr, DECIMAL); } + int out_fd = STDOUT_FILENO; + + char* output_file = getOption(argc, argv, "-f"); + if (output_file) { + out_fd = open(output_file, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); + + if (out_fd < 0) { + DIE_MSG_ERRNO("opening output file failed"); + } + } + int err = getaddrinfo(host.c_str(), port.c_str(), nullptr, &server); if (err) { if (err == EAI_SYSTEM) { @@ -246,8 +260,19 @@ auto main(int argc, char* argv[]) -> int { avg_ns /= 2; } - std::cout << "clients,iterations,size,time" << std::endl; - std::cout << clients << "," << iterations << "," << size << "," << avg_ns << std::endl; + std::stringstream sst; + sst << "clients,iterations,size,time" << std::endl; + sst << clients << "," << iterations << "," << size << "," << avg_ns << std::endl; + + auto output = sst.str(); + if (emper::io::writeFileAndWait(out_fd, output.c_str(), output.size()) < 0) { + DIE_MSG_ERRNO("writing results failed"); + } + + if (output_file) { + emper::io::closeAndForget(out_fd); + } + delete[] client_avgs; exit(EXIT_SUCCESS);