Skip to content

Play your Steam library's Windows games on a jailbroken PS5.

C++

Stop the download speed reading up to 20% low

Leonardo Lobato8affe3394873
diff --git a/src/steam/download_rate.h b/src/steam/download_rate.h
index dce4eaf..3cd36b4 100644
--- a/src/steam/download_rate.h
+++ b/src/steam/download_rate.h
@@ -31,7 +31,9 @@ class DownloadRate {
for (const auto& bucket : buckets_)
if (bucket.second >= 0 && now >= bucket.second * 1000 && now - bucket.second * 1000 < 5000)
bytes += bucket.bytes;
- const auto elapsed = std::min<std::int64_t>(5000, now - start_ms_);
+ // The counted buckets start at a whole second, so they span 4 to 5 seconds, not always 5.
+ const auto window = 4000 + now % 1000;
+ const auto elapsed = std::min<std::int64_t>(window, now - start_ms_);
const long double per_second = static_cast<long double>(bytes) * 1000 / elapsed;
DownloadEstimate result;
result.bytes_per_second = per_second >= std::numeric_limits<std::uint64_t>::max()
diff --git a/tests/download_rate_test.cpp b/tests/download_rate_test.cpp
index 4b26727..526d864 100644
--- a/tests/download_rate_test.cpp
+++ b/tests/download_rate_test.cpp
@@ -31,4 +31,16 @@ int main() {
CHECK(rate.Estimate(11000, 6, std::numeric_limits<std::uint64_t>::max()).eta_seconds == std::numeric_limits<int>::max());
rate.Record(10, 500); // a changed clock resets too
CHECK(!rate.Estimate(500, 10, 20).bytes_per_second);
+ // A steady 10 MB/s reads as about 10 MB/s whatever the time within the second.
+ DownloadRate steady;
+ steady.Record(0, 0);
+ std::uint64_t done = 0;
+ for (std::int64_t now = 100; now <= 12900; now += 100) {
+ done += 1000000;
+ steady.Record(done, now);
+ if (now >= 10000) {
+ const auto speed = *steady.Estimate(now, done, done * 2).bytes_per_second;
+ CHECK(speed >= 9500000 && speed <= 10500000);
+ }
+ }
}