From c3d5a06082dfe75bcf4ac065a72d6a3b71b35c02 Mon Sep 17 00:00:00 2001 From: Paul Ferrand Date: Sun, 12 Jan 2020 11:00:43 +0100 Subject: [PATCH] Print statistic on destruction --- src/sfizz/Logger.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/sfizz/Logger.cpp b/src/sfizz/Logger.cpp index 5214bc5a..451b84f9 100644 --- a/src/sfizz/Logger.cpp +++ b/src/sfizz/Logger.cpp @@ -1,9 +1,71 @@ #include "Logger.h" +#include "absl/algorithm/container.h" +#include "ghc/fs_std.hpp" +#include +#include + using namespace std::chrono_literals; + +template +void printStatistics(std::vector& data) +{ + absl::c_sort(data); + const auto maxToTake = std::min(data.size(), size_t(10)); + std::vector maxValues; + auto it = data.rbegin(); + for (size_t i = 0; i < maxToTake; ++i) + maxValues.push_back(*it++); + + const auto sum = absl::c_accumulate(data, 0.0); + const auto size = static_cast(data.size()); + const auto mean = sum / size; + std::vector squares; + absl::c_transform(data, std::back_inserter(squares), [mean](T x) { return x * x; }); + const auto sumOfSquares = absl::c_accumulate(squares, 0.0); + const auto variance = sumOfSquares / (size - 1.0) - (mean * mean) / size / (size - 1.0); + std::cout << "Mean: " << mean << '\n'; + std::cout << "Variance: " << variance << '\n'; + std::cout << "(Biased) deviation: " << std::sqrt(variance) << '\n'; + std::cout << "Maximum values:"; + for (auto& value: maxValues) + std::cout << value << ' '; + std::cout << '\n'; +} + sfz::Logger::~Logger() { keepRunning.clear(); loggingThread.join(); + + if (!loadTimes.empty()) { + std::vector loadTimesStats; + std::vector normLoadTimesStats; + absl::c_transform(loadTimes, std::back_inserter(loadTimesStats), [](auto x) { return x.value.count(); }); + absl::c_transform(loadTimes, std::back_inserter(normLoadTimesStats), [](auto x) { + if (x.fileSize == 0) + return x.value.count(); + + return x.value.count() / static_cast(x.fileSize); + }); + std::cout << "\nFile load times" << '\n'; + printStatistics(loadTimesStats); + std::cout << "\nNormalized file load times (per sample)" << '\n'; + printStatistics(normLoadTimesStats); + } + + if (!waitTimes.empty()) { + std::vector waitTimesStats; + absl::c_transform(waitTimes, std::back_inserter(waitTimesStats), [](auto x) { return x.count(); }); + std::cout << "\nWaiting times" << '\n'; + printStatistics(waitTimesStats); + } + + if (!callbackTimes.empty()) { + std::vector callbackTimesStats; + absl::c_transform(callbackTimes, std::back_inserter(callbackTimesStats), [](auto x) { return x.count(); }); + std::cout << "\nCallback times" << '\n'; + printStatistics(callbackTimesStats); + } }