예제 #1
0
        /// <inheritdoc />
        public int TestServerLatency(Server server, int retryCount = 3, CancellationToken cancellationToken = default)
        {
            var latencyUri = CreateTestUrl(server, "latency.txt");
            var timer      = new Stopwatch();

            using (var client = new SpeedTestHttpClient())
            {
                for (var i = 0; i < retryCount; i++)
                {
                    string testString;
                    try
                    {
                        timer.Start();
                        testString = client.GetStringAsync(latencyUri, cancellationToken).ConfigureAwait(false).GetAwaiter().GetResult();
                    }
                    catch (Exception)
                    {
                        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);
            }
        }
예제 #2
0
        private static double TestSpeed <T>(IEnumerable <T> testData, Func <SpeedTestHttpClient, T, Task <int> > doWork, int concurrencyCount = 2, CancellationToken cancellationToken = default)
        {
            var timer     = new Stopwatch();
            var throttler = new SemaphoreSlim(concurrencyCount);

            timer.Start();
            var downloadTasks = testData.Select(async data =>
            {
                await throttler.WaitAsync(cancellationToken).ConfigureAwait(false);

                var client = new SpeedTestHttpClient();
                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));
        }
예제 #3
0
        /// <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();

            using (var client = new SpeedTestHttpClient())
            {
                for (var i = 0; i < retryCount; i++)
                {
                    string testString;
                    try
                    {
                        timer.Start();
                        testString = await client.GetStringAsync(latencyUri).ConfigureAwait(false);
                    }
                    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);
        }
예제 #4
0
        private const int MaxUploadSize = 4; // 400 KB

        #region ISpeedTestClient

        /// <inheritdoc />
        /// <exception cref="InvalidOperationException"></exception>
        public Settings GetSettings()
        {
            using (var client = new SpeedTestHttpClient())
            {
                var settings = client.GetConfig <Settings>(ConfigUrl).GetAwaiter().GetResult();

                var serversConfig = new ServersList();
                foreach (var serversUrl in ServersUrls)
                {
                    try
                    {
                        serversConfig = client.GetConfig <ServersList>(serversUrl).GetAwaiter().GetResult();
                        if (serversConfig.Servers.Count > 0)
                        {
                            break;
                        }
                    }
                    catch
                    {
                        //
                    }
                }

                if (serversConfig.Servers.Count <= 0)
                {
                    throw new InvalidOperationException("SpeedTest does not return any server");
                }

                var ignoredIds =
                    settings.ServerConfig.IgnoreIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                serversConfig.CalculateDistances(settings.Client.GeoCoordinate);
                settings.Servers = serversConfig.Servers
                                   .Where(s => !ignoredIds.Contains(s.Id.ToString()))
                                   .OrderBy(s => s.Distance)
                                   .ToList();

                return(settings);
            }
        }