internal async Task <DownloadSpeed> GetDownloadSpeed(Server server = null, SpeedTestUnit unit = SpeedTestUnit.KiloBytesPerSecond) { try { if (server == null) { server = await GetServer(); } if (string.IsNullOrEmpty(server?.Url?.Trim())) { throw new Exception("Failed to get download speed"); } var downloadUrls = GenerateDownloadUrls(server, 3); double totalSize = 0; var startTime = DateTime.Now; foreach (var url in downloadUrls) { try { var bytes = await GetByteArrayAsync(url); totalSize += bytes.Length; } catch { } } var elapsedSeconds = (DateTime.Now - startTime).TotalSeconds; var bytesPerSecond = totalSize / elapsedSeconds; return(new DownloadSpeed { Server = server, Speed = bytesPerSecond.FromBytesPerSecondTo(unit), Unit = unit.ToShortIdentifier() }); } catch (Exception ex) { throw new Exception("Failed to get download speed", ex); } }
internal static string ToShortIdentifier(this SpeedTestUnit unit) { if (unit == SpeedTestUnit.MegaBytesPerSecond) { return("MB/s"); } if (unit == SpeedTestUnit.MegaBitsPerSecond) { return("Mb/s"); } if (unit == SpeedTestUnit.KiloBitsPerSecond) { return("Kb/s"); } return("KB/s"); }
internal async Task <DownloadSpeed> GetDownloadSpeed(SpeedTestUnit unit = SpeedTestUnit.KiloBytesPerSecond) { if (string.IsNullOrEmpty(Token)) { var jsonFilePath = await GetJsonFilePath(); Token = await GetToken(jsonFilePath); } var urls = await GetUrls(Token); var speed = await GetDownloadSpeed(urls?.Select(x => x.Url.AbsoluteUri), unit); return(new DownloadSpeed { Speed = speed.Speed, Unit = speed.Unit, Source = SpeedTestSource.Fast.ToSourceString() }); }
internal static double FromBytesPerSecondTo(this double speed, SpeedTestUnit unit) { switch (unit) { case SpeedTestUnit.BytesPerSecond: return(speed); case SpeedTestUnit.KiloBytesPerSecond: return(speed / 1024); case SpeedTestUnit.KiloBitsPerSecond: return(speed * 8 / 1024); case SpeedTestUnit.MegaBytesPerSecond: return(speed / (1024 * 1024)); case SpeedTestUnit.MegaBitsPerSecond: return(speed * 8 / (1024 * 1024)); default: return(speed); } }
internal async Task <DownloadSpeed> GetDownloadSpeed(Server server = null, SpeedTestUnit unit = SpeedTestUnit.KiloBytesPerSecond) { try { if (server == null) { server = await GetServer(); } if (string.IsNullOrEmpty(server?.Url?.Trim())) { throw new Exception("Failed to get download speed"); } var downloadUrls = GenerateDownloadUrls(server, 3); if (downloadUrls?.Any() != true) { throw new Exception("Couldn't fetch downloadable urls"); } var speed = await GetDownloadSpeed(downloadUrls, unit); return(new DownloadSpeed { Server = server, Speed = speed.Speed, Unit = speed.Unit, Source = SpeedTestSource.Speedtest.ToSourceString() }); } catch (Exception ex) { throw new Exception("Failed to get download speed", ex); } }
internal async Task <DownloadSpeed> GetDownloadSpeed(IEnumerable <string> downloadUrls, SpeedTestUnit unit, int timeout = 5000) { var bytes = 0D; var startTime = DateTime.Now; foreach (var url in downloadUrls) { bytes += await GetDownloadedBytes(url, timeout); } var elapsedSeconds = (DateTime.Now - startTime).TotalSeconds; var bytesPerSecond = bytes / elapsedSeconds; return(new DownloadSpeed { Speed = bytesPerSecond.FromBytesPerSecondTo(unit), Unit = unit.ToShortIdentifier() }); }
public string ToString(SpeedTestUnit unit) { return($"{Speed.FromBytesPerSecondTo(unit)}{unit.ToShortIdentifier()} Up ({Server.Host})"); }
/// <summary> /// Calculates download speed using the provided server /// </summary> /// <param name="server">The server object used for downloading files</param> /// <param name="unit">Unit in which Speed Test response should be returned</param> /// <returns>Download speed in KB/s</returns> public static async Task <DownloadSpeed> GetDownloadSpeed(Server server = null, SpeedTestUnit unit = SpeedTestUnit.KiloBytesPerSecond) => await Client.GetDownloadSpeed(server, unit);
/// <summary> /// Calculates download speed using the provided server /// </summary> /// <param name="unit">Specifies in which unit download speed should be returned</param> /// <returns>An instance of type DownloadSpeed</returns> public static async Task <DownloadSpeed> GetDownloadSpeed(SpeedTestUnit unit = SpeedTestUnit.KiloBytesPerSecond) => await Client?.GetDownloadSpeed(unit);