Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the HttpClient timeout to the fastest latency found so far #8

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 20 additions & 6 deletions src/SpeedTestSharp/Client/SpeedTestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -44,7 +45,7 @@ public async Task<SpeedTestResult> TestSpeedAsync(SpeedUnit speedUnit,
throw new InvalidOperationException("No server was found");
}

var latency = testLatency ? await TestServerLatencyAsync(server) : -1;
var latency = testLatency ? await TestServerLatencyAsync(server, Constants.DefaultHttpTimeoutMilliseconds) : -1;
var downloadSpeed = testDownload ? await TestDownloadSpeedAsync(server, parallelTasks) : -1;
var uploadSpeed = testUpload ? await TestUploadSpeedAsync(server, parallelTasks) : -1;

Expand All @@ -59,21 +60,33 @@ public async Task<SpeedTestResult> TestSpeedAsync(SpeedUnit speedUnit,
private async Task<Server?> GetBestServerByLatency()
{
var servers = await FetchServersAsync();
var serverLatency = new Dictionary<Server, int>();

var fastestLatency = Constants.DefaultHttpTimeoutMilliseconds;
Server? fastestServer = null;

foreach (var server in servers)
{
// Reduce the HttpClient timeout to the fastest latency found so far
// (ie. do not wait for servers where the response time is above the fastest)

try
{
var latency = await TestServerLatencyAsync(server);
serverLatency.TryAdd(server, latency);
var latency = await TestServerLatencyAsync(server, fastestLatency);

if (latency < fastestLatency)
{
// A new fastest server is found
fastestLatency = latency;
fastestServer = server;
}
}
catch
{
// ignore this server
}
}

return serverLatency.OrderBy(x => x.Value).Select(x => x.Key).FirstOrDefault();
return fastestServer;
}

private async Task<Server[]> FetchServersAsync()
Expand All @@ -83,7 +96,7 @@ private async Task<Server[]> FetchServersAsync()
return serversXml.DeserializeFromXml<ServersList>().Servers ?? Array.Empty<Server>();
}

private async Task<int> TestServerLatencyAsync(Server server, int tests = 4)
private async Task<int> TestServerLatencyAsync(Server server, int httpTimeoutMilliseconds, int tests = 4)
{
SetStage(TestStage.Latency);

Expand All @@ -95,6 +108,7 @@ private async Task<int> TestServerLatencyAsync(Server server, int tests = 4)
var latencyUrl = GetBaseUrl(server.Url).Append("latency.txt");
var stopwatch = new Stopwatch();
using var httpClient = GetHttpClient();
httpClient.Timeout = TimeSpan.FromMilliseconds(httpTimeoutMilliseconds);

var test = 1;
do
Expand Down
4 changes: 4 additions & 0 deletions src/SpeedTestSharp/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ public class Constants
public const string Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public const int MaxUploadSize = 6;
public static readonly int[] DownloadSizes = { 1500, 2000, 3000, 3500, 4000 };

// The default timeout for HttpClient is 100 seconds.
// ref: https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.timeout?view=net-9.0
public const int DefaultHttpTimeoutMilliseconds = 100000;
}
}