diff options
| author | Per Larsson <[email protected]> | 2021-09-07 20:01:06 +0200 |
|---|---|---|
| committer | Per Larsson <[email protected]> | 2021-09-07 20:01:06 +0200 |
| commit | 6356b19b0f717f40d388b930c63d4811e757fe50 (patch) | |
| tree | 1167d20e43eb1d9aed736520ba7e25bd8cfa9983 /zenserver/upstream/upstreamcache.cpp | |
| parent | Return stats from Zen/Jupiter HTTP client. (diff) | |
| download | zen-6356b19b0f717f40d388b930c63d4811e757fe50.tar.xz zen-6356b19b0f717f40d388b930c63d4811e757fe50.zip | |
Updated upstream stats calculation.
Diffstat (limited to 'zenserver/upstream/upstreamcache.cpp')
| -rw-r--r-- | zenserver/upstream/upstreamcache.cpp | 83 |
1 files changed, 31 insertions, 52 deletions
diff --git a/zenserver/upstream/upstreamcache.cpp b/zenserver/upstream/upstreamcache.cpp index 04ac40a39..c1002a6da 100644 --- a/zenserver/upstream/upstreamcache.cpp +++ b/zenserver/upstream/upstreamcache.cpp @@ -16,8 +16,8 @@ #include <algorithm> #include <atomic> -#include <chrono> #include <deque> +#include <limits> #include <thread> #include <unordered_map> @@ -356,8 +356,7 @@ namespace detail { class UpstreamStats final { - using clock = std::chrono::steady_clock; - using time_point = std::chrono::time_point<clock>; + static constexpr uint64_t MaxSampleCount = 100ull; struct StatCounters { @@ -366,14 +365,16 @@ class UpstreamStats final double Seconds = {}; }; + using StatsMap = std::unordered_map<const UpstreamEndpoint*, StatCounters>; + struct EndpointStats { - mutable std::mutex Lock; - std::unordered_map<const UpstreamEndpoint*, StatCounters> Counters; + mutable std::mutex Lock; + StatsMap Counters; }; public: - UpstreamStats() : m_Log(zen::logging::Get("upstreamstats")) { m_LastTime = clock::now(); } + UpstreamStats() : m_Log(zen::logging::Get("upstream")) {} void Add(const UpstreamEndpoint& Endpoint, const GetUpstreamCacheResult& Result) { @@ -384,7 +385,11 @@ public: Counters.Seconds += Result.ElapsedSeconds; Counters.Count++; - LogStats(); + if (Counters.Count >= MaxSampleCount) + { + LogStats("STATS - (downstream):"sv, m_DownStats.Counters); + Counters = StatCounters{}; + } } void Add(const UpstreamEndpoint& Endpoint, const PutUpstreamCacheResult& Result) @@ -396,62 +401,36 @@ public: Counters.Seconds += Result.ElapsedSeconds; Counters.Count++; - LogStats(); + if (Counters.Count >= MaxSampleCount) + { + LogStats("STATS - (upstream):"sv, m_UpStats.Counters); + Counters = StatCounters{}; + } } private: - void LogStats() + void LogStats(std::string_view What, const std::unordered_map<const UpstreamEndpoint*, StatCounters>& EndpointStats) { - using std::chrono::duration_cast; - using std::chrono::seconds; - - const seconds Duration = duration_cast<seconds>(clock::now() - m_LastTime); - if (Duration < seconds(5)) + for (const auto& Kv : EndpointStats) { - return; + const UpstreamEndpoint& Endpoint = *Kv.first; + const StatCounters& Counters = Kv.second; + const double TotalMb = double(Counters.Bytes) / 1024.0 / 1024.0; + + m_Log.info("{} Endpoint: {}, Bytes: {:.2f} MB, Time: {:.2f} s, Speed: {:.2f} MB/s, Avg: {:.2f} ms/request, Samples: {}", + What, + Kv.first->DisplayName(), + TotalMb, + Counters.Seconds, + TotalMb / Counters.Seconds, + (Counters.Seconds * 1000.0) / double(Counters.Count), + Counters.Count); } - - { - std::unique_lock Lock(m_DownStats.Lock); - for (const auto& Kv : m_DownStats.Counters) - { - const UpstreamEndpoint& Endpoint = *Kv.first; - const StatCounters& Counters = Kv.second; - - m_Log.info("Endpoint: {}, Total downloaded: {}Kb, Total time: {}s, Count: {}, Speed: {} Kb/s, Avg: {} (Req/s)", - Kv.first->DisplayName(), - double(Counters.Bytes) / 1024.0, - Counters.Seconds, - Counters.Count, - (double(Counters.Bytes) / 1024.0) / Counters.Seconds, - double(Counters.Count) / Counters.Seconds); - } - } - - { - std::unique_lock Lock(m_UpStats.Lock); - for (const auto& Kv : m_UpStats.Counters) - { - const UpstreamEndpoint& Endpoint = *Kv.first; - const StatCounters& Counters = Kv.second; - - m_Log.info("Endpoint: {}, Total uploaded: {}Kb, Total time: {}s, Count: {}, Speed: {} Kb/s, Avg: {} (Req/s)", - Kv.first->DisplayName(), - double(Counters.Bytes) / 1024.0, - Counters.Seconds, - Counters.Count, - (double(Counters.Bytes) / 1024.0) / Counters.Seconds, - double(Counters.Count) / Counters.Seconds); - } - } - - m_LastTime = clock::now(); } spdlog::logger& m_Log; EndpointStats m_UpStats; EndpointStats m_DownStats; - time_point m_LastTime = {}; }; ////////////////////////////////////////////////////////////////////////// |