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 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})"); }