Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
3ffe1cf
Refactor: Dashboard: Speed test calculations in SpeedTestCalculator w…
BornToBeRoot Nov 26, 2026
7db2930
Fix: Dashboard: Speed test server time like the official library (U1)
BornToBeRoot Nov 26, 2026
18c7c46
Fix: Dashboard: Speed test continues without the ISP / server (B3)
BornToBeRoot Nov 26, 2026
61d0bbf
Fix: Dashboard: Speed test download value no longer jumps above the r…
BornToBeRoot Nov 26, 2026
b4e0132
Fix: Dashboard: Speed test sequence and end of a direction like the o…
BornToBeRoot Nov 26, 2026
9c7ba07
Fix: Dashboard: Speed test download includes the headers like the off…
BornToBeRoot Nov 26, 2026
f5f73e0
Fix: Dashboard: Speed test request fails after 15 seconds without dat…
BornToBeRoot Nov 26, 2026
88d5301
Fix: Dashboard: Enter / Esc no longer start / stop the speed test (B2)
BornToBeRoot Nov 26, 2026
573eb61
Fix: Dashboard: Speed test status message for cancel / error, values …
BornToBeRoot Nov 26, 2026
7e8d17f
Fix: Dashboard: Speed test skips a failed request like the official l…
BornToBeRoot Nov 26, 2026
66f5f6d
Fix: Dashboard: Hiding the speed test widget cancels a running test (H1)
BornToBeRoot Nov 26, 2026
bb20766
Fix: Dashboard: Speed test server shown as -/- when unknown (C2)
BornToBeRoot Nov 26, 2026
1094698
Fix: Dashboard: Speed test sparkline no longer shows the first sample…
BornToBeRoot Nov 26, 2026
82a70f6
Feature: Dashboard: Speed test shows the time of the last test (M2)
BornToBeRoot Nov 26, 2026
81d6874
Feature: Dashboard: Copy all button in the speed test (M1)
BornToBeRoot Nov 26, 2026
cfd26b6
Refactor: Dashboard: Remove the unused live upload value of the speed…
BornToBeRoot Nov 26, 2026
d8a0308
Refactor: Dashboard: Remove the unused client location of the speed t…
BornToBeRoot Nov 26, 2026
b493f60
Docs: Dashboard: Speed test buttons, status, data volume and duration…
BornToBeRoot Nov 26, 2026
da1ee3b
Fix: Dashboard: Speed test time not shown for the values of a cancele…
BornToBeRoot Nov 26, 2026
6a60e57
Fix: Dashboard: Speed test copy all enabled right after the test (CR2)
BornToBeRoot Nov 26, 2026
35a4a1c
Fix: Dashboard: Speed test stopped during a transfer is shown as canc…
BornToBeRoot Nov 26, 2026
6a35487
Fix: Dashboard: Speed test skips the larger sizes when a proxy reject…
BornToBeRoot Nov 26, 2026
979b613
Fix: Dashboard: Speed test error and hint in one paragraph
BornToBeRoot Nov 26, 2026
a725c69
Update next-release.md
BornToBeRoot Nov 26, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Source/NETworkManager.Localization/Resources/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Source/NETworkManager.Localization/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -5758,4 +5758,13 @@ Do you want to start the scan?</value>
<data name="AllWidgetsAreHiddenMessage" xml:space="preserve">
<value>All widgets are hidden. You can show them in the settings of the dashboard.</value>
</data>
<data name="XXXDidNotRespondWithinXXXSecondsMessage" xml:space="preserve">
<value>{0} did not respond within {1} seconds.</value>
</data>
<data name="XRequestsFailedAndWereSkippedMessage" xml:space="preserve">
<value>{0} requests failed and were skipped. The result may be less accurate.</value>
</data>
<data name="LastTestAtX" xml:space="preserve">
<value>Last test at {0}</value>
</data>
</root>
173 changes: 173 additions & 0 deletions Source/NETworkManager.Models/Cloudflare/SpeedTestCalculator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;

namespace NETworkManager.Models.Cloudflare;

/// <summary>
/// Calculations of the speed test, like the official
/// <a href="https://github.com/cloudflare/speedtest">cloudflare/speedtest</a> library.
/// </summary>
public static class SpeedTestCalculator
{
/// <summary>
/// Server time of the request, e.g. <c>cfRequestDuration;dur=12.3</c>.
/// </summary>
private static readonly Regex RequestDurationRegex = new(@"(?:^|,\s*)cfReq(?:uest)?Dur(?:ation)?;\s*dur=([0-9.]+)",
RegexOptions.Compiled | RegexOptions.IgnoreCase);

/// <summary>
/// Parts of the server time (summed up if there is no request duration), e.g. <c>cfSpeedXyz;dur=1.2</c>.
/// </summary>
private static readonly Regex SpeedDurationRegex = new(@"(?:^|,\s*)cfSpeed[a-zA-Z]*;\s*dur=([0-9.]+)",
RegexOptions.Compiled | RegexOptions.IgnoreCase);

/// <summary>
/// Server times up to this value (ms) are ignored.
/// </summary>
private const double ServerTimeMinDuration = 0.01;

/// <summary>
/// Longest delay before a request that failed with HTTP 429 is repeated.
/// </summary>
private static readonly TimeSpan MaxRetryDelay = TimeSpan.FromSeconds(60);

/// <summary>
/// Percentile of the values with linear interpolation between the two closest ranks.
/// </summary>
/// <param name="values">Values (unsorted).</param>
/// <param name="p">Percentile between 0 and 1 (e.g. 0.9).</param>
/// <returns>Percentile, 0 if there are no values.</returns>
public static double Percentile(IReadOnlyCollection<double> values, double p)
{
if (values == null || values.Count == 0)
return 0.0;

var sorted = values.OrderBy(v => v).ToList();

if (sorted.Count == 1)
return sorted[0];

var rank = p * (sorted.Count - 1);
var lower = (int)Math.Floor(rank);
var upper = (int)Math.Ceiling(rank);

if (lower == upper)
return sorted[lower];

var weight = rank - lower;

return sorted[lower] * (1 - weight) + sorted[upper] * weight;
}

/// <summary>
/// Bandwidth shown while the test runs: the 90th percentile of the completed requests (like the result). Before the
/// first request has completed, the throughput of the running request.
/// </summary>
/// <param name="samplesBps">Bandwidth of the completed requests (bit/s).</param>
/// <param name="liveBps">Throughput of the running request (bit/s), <c>null</c> if none.</param>
/// <returns>Bandwidth in bit/s, <c>null</c> if there is no value yet.</returns>
public static double? CurrentBandwidth(IReadOnlyCollection<double> samplesBps, double? liveBps)
{
return samplesBps.Count > 0 ? Percentile(samplesBps, 0.9) : liveBps;
}

/// <summary>
/// A direction (download / upload) is finished when even the fastest request of a set took longer than the finish
/// duration: the larger sets would take too long (<c>bandwidthFinishRequestDuration</c> of the official library).
/// </summary>
/// <param name="durationsMs">Duration of the requests of the set (ms).</param>
/// <param name="finishRequestDurationMs">Finish duration (ms).</param>
/// <returns><c>true</c> if the larger sets of the direction are skipped.</returns>
public static bool IsDirectionFinished(IReadOnlyCollection<double> durationsMs, double finishRequestDurationMs)
{
return durationsMs.Count > 0 && durationsMs.Min() > finishRequestDurationMs;
}

/// <summary>
/// Delay before a request that failed with HTTP 429 is repeated, from the <c>Retry-After</c> header (like
/// <c>getRetryDelay</c> of the official library).
/// </summary>
/// <param name="delta">Delay of the header (seconds).</param>
/// <param name="date">Date of the header.</param>
/// <param name="now">Current time.</param>
/// <returns>Delay, 5 seconds without a header, at most 60 seconds (the test should not wait endlessly).</returns>
public static TimeSpan GetRetryDelay(TimeSpan? delta, DateTimeOffset? date, DateTimeOffset now)
{
var delay = delta ?? (date.HasValue ? date.Value - now : TimeSpan.FromSeconds(5));

if (delay < TimeSpan.Zero)
return TimeSpan.Zero;

return delay > MaxRetryDelay ? MaxRetryDelay : delay;
}

/// <summary>
/// A request failed without a connection to the server (no response, connection lost, timeout), not because the
/// server or a proxy rejected it with an HTTP error.
/// </summary>
/// <param name="ex">Error of the request.</param>
/// <returns><c>true</c> if there was no response.</returns>
public static bool IsConnectionFailure(Exception ex)
{
return ex is not HttpRequestException { StatusCode: not null };
}

/// <summary>
/// Jitter: average absolute difference between consecutive latency samples.
/// </summary>
/// <param name="pings">Latency samples in the order they were measured.</param>
/// <returns>Jitter, 0 if there are fewer than two samples.</returns>
public static double Jitter(IReadOnlyList<double> pings)
{
if (pings == null || pings.Count < 2)
return 0.0;

double sum = 0;

for (var i = 1; i < pings.Count; i++)
sum += Math.Abs(pings[i] - pings[i - 1]);

return sum / (pings.Count - 1);
}

/// <summary>
/// Time the server needed to process the request, from the <c>server-timing</c> header: the request duration
/// (<c>cfRequestDuration</c>), otherwise the sum of the <c>cfSpeed*</c> durations (like <c>cfGetServerTime</c> of the
/// official library).
/// </summary>
/// <param name="values">Values of the <c>server-timing</c> header.</param>
/// <returns>Server time in milliseconds, <c>null</c> if the header has no server time.</returns>
public static double? ParseServerTiming(IEnumerable<string> values)
{
if (values == null)
return null;

var header = string.Join(", ", values);

if (header.Length == 0)
return null;

var match = RequestDurationRegex.Match(header);

if (match.Success && TryParseDuration(match, out var requestDuration) &&
requestDuration > ServerTimeMinDuration)
return requestDuration;

double sum = 0;

foreach (Match speedMatch in SpeedDurationRegex.Matches(header))
{
if (TryParseDuration(speedMatch, out var duration))
sum += duration;
}

return sum > ServerTimeMinDuration ? sum : null;
}

private static bool TryParseDuration(Match match, out double duration) =>
double.TryParse(match.Groups[1].Value, NumberStyles.Float, CultureInfo.InvariantCulture, out duration);
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class SpeedTestMetaInfo
/// Autonomous System Number of the client's ISP.
/// </summary>
[JsonProperty("asn")]
public int Asn { get; set; }
public int? Asn { get; set; }

/// <summary>
/// Human-readable ISP name (e.g. "innogy TelNet").
Expand Down
32 changes: 18 additions & 14 deletions Source/NETworkManager.Models/Cloudflare/SpeedTestResult.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Linq;

namespace NETworkManager.Models.Cloudflare;

/// <summary>
Expand Down Expand Up @@ -34,16 +36,6 @@ public class SpeedTestResult
/// </summary>
public string Isp { get; set; }

/// <summary>
/// City of the client (Cloudflare meta <c>city</c>).
/// </summary>
public string ClientCity { get; set; }

/// <summary>
/// ISO 3166-1 alpha-2 country code of the client (Cloudflare meta <c>country</c>).
/// </summary>
public string ClientCountry { get; set; }

/// <summary>
/// City of the Cloudflare PoP serving the test, e.g. "Frankfurt-am-Main".
/// </summary>
Expand All @@ -60,12 +52,24 @@ public class SpeedTestResult
public string ServerIata { get; set; }

/// <summary>
/// Indicates that the speed test run failed.
/// Cloudflare server for the UI, e.g. "Frankfurt-am-Main, DE (FRA)". <c>null</c> if unknown.
/// </summary>
public bool HasError { get; set; }
public string Server
{
get
{
var location = string.Join(", ",
new[] { ServerCity, ServerCountry }.Where(x => !string.IsNullOrWhiteSpace(x)));

if (string.IsNullOrWhiteSpace(ServerIata))
return location.Length > 0 ? location : null;

return location.Length > 0 ? $"{location} ({ServerIata})" : ServerIata;
}
}

/// <summary>
/// Error message when <see cref="HasError"/> is <c>true</c>.
/// Number of requests that failed and were skipped (e.g. a short interruption of the connection).
/// </summary>
public string ErrorMessage { get; set; }
public int FailedRequests { get; set; }
}
Loading