/// <summary> /// Test latency (ping) to server /// </summary> /// <returns>Latency in milliseconds (ms)</returns> public int TestServerLatency(Server server, int retryCount = 3) { var latencyUri = CreateTestUrl(server, "latency.txt"); var timer = new Stopwatch(); using (var client = new SpeedTestWebClient()) { for (var i = 0; i < retryCount; i++) { string testString; try { timer.Start(); testString = client.DownloadString(latencyUri); } catch (WebException) { continue; } finally { timer.Stop(); } if (!testString.StartsWith("test=test")) { throw new InvalidOperationException("Server returned incorrect test string for latency.txt"); } } } return((int)timer.ElapsedMilliseconds / retryCount); }
private static double TestSpeed <T>(IEnumerable <T> testData, Func <WebClient, T, Task <int> > doWork, int concurencyCount = 2) { var timer = new Stopwatch(); var throttler = new SemaphoreSlim(concurencyCount); timer.Start(); var downloadTasks = testData.Select(async data => { await throttler.WaitAsync().ConfigureAwait(false); var client = new SpeedTestWebClient(); try { var size = await doWork(client, data).ConfigureAwait(false); return(size); } finally { client.Dispose(); throttler.Release(); } }).ToArray(); Task.WaitAll(downloadTasks); timer.Stop(); double totalSize = downloadTasks.Sum(task => task.Result); return((totalSize * 8 / 1024) / ((double)timer.ElapsedMilliseconds / 1000)); }
/// <summary> /// Test latency (ping) to server /// </summary> /// <returns>Latency in milliseconds (ms)</returns> public async Task <int> TestServerLatencyAsync(Server server, int retryCount = 3) { var latencyUri = CreateTestUrl(server, "latency.txt"); var timer = new Stopwatch(); for (var i = 0; i < retryCount; i++) { string testString; try { timer.Start(); testString = await SpeedTestWebClient.client.GetAsync(SpeedTestWebClient.AddTimeStamp(new Uri(latencyUri)), HttpCompletionOption.ResponseHeadersRead).GetAwaiter().GetResult().Content.ReadAsStringAsync(); } catch (WebException) { continue; } finally { timer.Stop(); } if (!testString.StartsWith("test=test")) { throw new InvalidOperationException("Server returned incorrect test string for latency.txt"); } } return((int)timer.ElapsedMilliseconds / retryCount); }
/// <summary> /// Test download speed to server /// </summary> /// <returns>Download speed in Kbps</returns> public double TestDownloadSpeed(Server server, int simultaniousDownloads = 2, int retryCount = 2) { var testData = GenerateDownloadUrls(server, retryCount); return(TestSpeed(testData, async(url) => { var data = await SpeedTestWebClient.client.GetAsync(SpeedTestWebClient.AddTimeStamp(new Uri(url)), HttpCompletionOption.ResponseHeadersRead).GetAwaiter().GetResult().Content.ReadAsStringAsync(); return data.Length; }, simultaniousDownloads)); }
private const int MaxUploadSize = 4; // 400 KB #region ISpeedTestClient /// <summary> /// Download speedtest.net settings /// </summary> /// <returns>speedtest.net settings</returns> public async Task <Settings> GetSettingsAsync() { var settings = await SpeedTestWebClient.GetConfigAsync <Settings>(ConfigUrl); var serversConfig = await SpeedTestWebClient.GetConfigAsync <ServersList>(ServersUrl); serversConfig.CalculateDistances(settings.Client.GeoCoordinate); settings.Servers = serversConfig.Servers.OrderBy(s => s.Distance).ToList(); return(settings); }
private const int MaxUploadSize = 40; // 400 KB #region ISpeedTestClient /// <summary> /// Download speedtest.net settings /// </summary> /// <returns>speedtest.net settings</returns> public Settings GetSettings() { using (var client = new SpeedTestWebClient()) { var settings = client.GetConfig <Settings>(ConfigUrl); var serversConfig = client.GetConfig <ServersList>(ServersUrl); serversConfig.CalculateDistances(settings.Client.GeoCoordinate); settings.Servers = serversConfig.Servers.OrderBy(s => s.Distance).ToList(); return(settings); } }