From bdadfcba19fcc7390e4e1868ab502cafe3f05e4a Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Mon, 28 Sep 2020 18:41:57 -0700 Subject: [PATCH] ws gzip / add way to run command N time for benchmarking --- ixwebsocket/IXBench.cpp | 13 +++++++++++++ ixwebsocket/IXBench.h | 2 ++ ws/ws.cpp | 21 ++++++++++++++++++--- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/ixwebsocket/IXBench.cpp b/ixwebsocket/IXBench.cpp index c9ef2a93..75497f85 100644 --- a/ixwebsocket/IXBench.cpp +++ b/ixwebsocket/IXBench.cpp @@ -38,6 +38,19 @@ namespace ix _duration = microseconds.count(); std::cerr << _description << " completed in " << _duration << " us" << std::endl; + setReported(); + } + + void Bench::record() + { + auto now = std::chrono::high_resolution_clock::now(); + auto microseconds = std::chrono::duration_cast(now - _start); + + _duration = microseconds.count(); + } + + void Bench::setReported() + { _reported = true; } diff --git a/ixwebsocket/IXBench.h b/ixwebsocket/IXBench.h index c825bead..c4f904b7 100644 --- a/ixwebsocket/IXBench.h +++ b/ixwebsocket/IXBench.h @@ -18,7 +18,9 @@ namespace ix ~Bench(); void reset(); + void record(); void report(); + void setReported(); uint64_t getDuration() const; private: diff --git a/ws/ws.cpp b/ws/ws.cpp index adb48630..3a254e1b 100644 --- a/ws/ws.cpp +++ b/ws/ws.cpp @@ -1136,7 +1136,7 @@ namespace ix return 0; } - int ws_gzip(const std::string& filename) + int ws_gzip(const std::string& filename, int runCount) { auto res = readAsString(filename); bool found = res.first; @@ -1150,9 +1150,22 @@ namespace ix std::string compressedBytes; + std::vector durations; { Bench bench("compressing file"); - compressedBytes = gzipCompress(res.second); + bench.setReported(); + + for (int i = 0; i < runCount; ++i) + { + bench.reset(); + compressedBytes = gzipCompress(res.second); + bench.record(); + durations.push_back(bench.getDuration()); + } + + size_t medianIdx = durations.size() / 2; + uint64_t medianRuntime = durations[medianIdx]; + spdlog::info("compressing file in {}", medianRuntime); } std::string outputFilename(filename); @@ -2969,6 +2982,7 @@ int main(int argc, char** argv) int msgCount = 1000 * 1000; uint32_t maxWaitBetweenReconnectionRetries; int pingIntervalSecs = 30; + int runCount = 1; auto addGenericOptions = [&pidfile](CLI::App* app) { app->add_option("--pidfile", pidfile, "Pid file"); @@ -3297,6 +3311,7 @@ int main(int argc, char** argv) CLI::App* gzipApp = app.add_subcommand("gzip", "Gzip compressor"); gzipApp->fallthrough(); gzipApp->add_option("filename", filename, "Filename")->required(); + gzipApp->add_option("--run_count", runCount, "Number of time to run the compression"); CLI::App* gunzipApp = app.add_subcommand("gunzip", "Gzip decompressor"); gunzipApp->fallthrough(); @@ -3600,7 +3615,7 @@ int main(int argc, char** argv) } else if (app.got_subcommand("gzip")) { - ret = ix::ws_gzip(filename); + ret = ix::ws_gzip(filename, runCount); } else if (app.got_subcommand("gunzip")) {