示例#1
0
        private static async Task ExecuteRequest(string url, RequestTracker requestTracker)
        {
            var requestStarted = DateTime.Now;
            await _httpClient.GetAsync(url);

            requestTracker.CompleteRequest(DateTime.Now - requestStarted);
        }
示例#2
0
        private static async Task <int> Run(Options options)
        {
            var requestTracker = new RequestTracker();

            await Task.WhenAll(
                PrintResults(requestTracker),
                ExecuteRequests(options.Url, options.RequestsPerSecond, requestTracker));

            return(0);
        }
示例#3
0
        private static async Task PrintResults(RequestTracker requestTracker)
        {
            var last = DateTime.MinValue;

            while (true)
            {
                var now = DateTime.Now;

                var(requestsStarted, requestsCompleted, averageLatency,
                    recentRequestsStarted, recentRequestsCompleted, recentAverageLatency) = requestTracker.GetSummary(last, now);

                Console.WriteLine($"Total: {requestsStarted}/{requestsCompleted}/{Math.Round(averageLatency.TotalMilliseconds)}" +
                                  $", Recent: {recentRequestsStarted}/{recentRequestsCompleted}/{Math.Round(recentAverageLatency.TotalMilliseconds)}");

                last = now;
                await Task.Delay(TimeSpan.FromSeconds(1));
            }
        }
示例#4
0
        private static async Task ExecuteRequests(string url, int requestsPerSecond, RequestTracker requestTracker)
        {
            var start = DateTime.Now;

            while (true)
            {
                var now     = DateTime.Now;
                var elapsed = now - start;
                var expectedRequestsStarted = requestsPerSecond * elapsed.TotalSeconds;
                var requestsStarted         = requestTracker.RequestsStarted;

                if (requestsStarted < expectedRequestsStarted)
                {
                    requestTracker.RequestStarted();
                    ExecuteRequest(url, requestTracker);
                }
                else
                {
                    await Task.Delay(TimeSpan.FromSeconds(1 / requestsPerSecond));
                }
            }
        }