Exemplo n.º 1
0
        private void TrackResponse(Response response)
        {
            var responseTime = (float)_localStopwatch.ElapsedTicks / Stopwatch.Frequency * 1000;
            var length       = 0;//content.Length + response.Headers.ToString().Length + MissingHeaderLength;

            if (response.IsSuccess)
            {
                _workerThreadResult.Add((int)_stopwatch.ElapsedMilliseconds, length, responseTime, _index < 10);
            }

            else
            {
                _workerThreadResult.AddError((int)_stopwatch.ElapsedMilliseconds, responseTime, _index < 10);
            }
        }
Exemplo n.º 2
0
 //[Benchmark]
 public void Add()
 {
     for (var i = 0; i < 1000; i++)
     {
         _result.Add(i / 100, 1337, 10f, 200, false);
     }
 }
Exemplo n.º 3
0
        private static void DoWork(Action action, TimeSpan duration, int?count, ConcurrentQueue <WorkerThreadResult> results, Stopwatch sw, CancellationToken cancellationToken, ManualResetEventSlim resetEvent, int workerIndex)
        {
            var result  = new WorkerThreadResult();
            var sw2     = new Stopwatch();
            var sw3     = new Stopwatch();
            var current = 0;

            // To save memory we only track response times from the first 20 workers
            var trackResponseTime = workerIndex < 20;

            while (!cancellationToken.IsCancellationRequested && duration.TotalMilliseconds > sw.Elapsed.TotalMilliseconds && (!count.HasValue || current < count.Value))
            {
                current++;
                try
                {
                    sw2.Restart();

                    action.Invoke();

                    result.Add((int)(sw.ElapsedTicks / Stopwatch.Frequency), (float)sw2.ElapsedTicks / Stopwatch.Frequency * 1000, trackResponseTime);
                }
                catch (Exception ex)
                {
                    result.AddError((int)(sw.ElapsedTicks / Stopwatch.Frequency), (float)sw2.ElapsedTicks / Stopwatch.Frequency * 1000, ex);
                }
            }

            results.Enqueue(result);
            resetEvent.Set();
        }
Exemplo n.º 4
0
        public ValueTask DoWork()
        {
            _localStopwatch.Restart();
            var(length, statusCode) = _httpWorker.Send();

            if (statusCode < 400)
            {
                _workerThreadResult.Add((int)_stopwatch.ElapsedMilliseconds / 1000, length, (float)_localStopwatch.ElapsedTicks / Stopwatch.Frequency * 1000, statusCode, _index < 10);
            }
            else
            {
                _workerThreadResult.AddError((int)_stopwatch.ElapsedMilliseconds / 1000, (float)_localStopwatch.ElapsedTicks / Stopwatch.Frequency * 1000, statusCode, _index < 10);
            }

            return(new ValueTask());
        }
Exemplo n.º 5
0
        public Task DoWork()
        {
            _localStopwatch.Restart();
            _httpWorker.Write();
            _httpWorker.Flush();
            var length = _httpWorker.Read(out var statusCode);

            if (statusCode < 400)
            {
                _workerThreadResult.Add((int)_stopwatch.ElapsedMilliseconds, length, (float)_localStopwatch.ElapsedTicks / Stopwatch.Frequency * 1000, _index < 10, statusCode);
            }
            else
            {
                _workerThreadResult.AddError((int)_stopwatch.ElapsedMilliseconds, (float)_localStopwatch.ElapsedTicks / Stopwatch.Frequency * 1000, _index < 10, statusCode);
            }

            return(Task.CompletedTask);
        }
Exemplo n.º 6
0
        public async Task DoWork()
        {
            _localStopwatch.Restart();

            using (var response = await _httpClient.GetAsync(_uri))
            {
                var contentStream = await response.Content.ReadAsStreamAsync();

                var length       = contentStream.Length + response.Headers.ToString().Length + MissingHeaderLength;
                var responseTime = (float)_localStopwatch.ElapsedTicks / Stopwatch.Frequency * 1000;

                if ((int)response.StatusCode < 400)
                {
                    _workerThreadResult.Add((int)_stopwatch.ElapsedMilliseconds, length, responseTime, _index < 10);
                }
                else
                {
                    _workerThreadResult.AddError((int)_stopwatch.ElapsedMilliseconds, responseTime, _index < 10);
                }
            }
        }
Exemplo n.º 7
0
        public async ValueTask DoWorkAsync()
        {
            var rnd = new Random();
            var inx = rnd.Next(0, 1);

            _localStopwatch.Restart();


            var response = await client.PostApi <LoginRequest, LoginResponse>(param[inx], Path);

            var responseTime = (float)_localStopwatch.ElapsedTicks / Stopwatch.Frequency * 1000;


            if (response.Status == 0)
            {
                _workerThreadResult.Add((int)_stopwatch.ElapsedMilliseconds / 1000, 1, responseTime, response.Status, _index < 10);
            }
            else
            {
                _workerThreadResult.AddError((int)_stopwatch.ElapsedMilliseconds / 1000, responseTime, response.Status, _index < 10);
            }
        }
Exemplo n.º 8
0
        private static void DoWork(Uri uri, TimeSpan duration, int?count, int pipelining, ConcurrentQueue <WorkerThreadResult> results, Stopwatch sw, CancellationToken cancellationToken, ManualResetEventSlim resetEvent, int workerIndex, string requestString, string body)
        {
            var result = new WorkerThreadResult();
            var sw2    = new Stopwatch();
            var sw3    = new Stopwatch();

            byte[] bodyByteArray    = null;
            byte[] requestByteArray = null;

            if (!string.IsNullOrEmpty(requestString))
            {
                requestByteArray = Encoding.UTF8.GetBytes(requestString);
            }
            if (!string.IsNullOrEmpty(body))
            {
                bodyByteArray = Encoding.UTF8.GetBytes(body);
            }

            var worker  = new HttpWorker(uri, request: requestByteArray, data: bodyByteArray);
            var current = 0;

            // To save memory we only track response times from the first 20 workers
            var trackResponseTime = workerIndex < 20;

            // Priming connection ...
            if (!count.HasValue)
            {
                try
                {
                    int tmpStatusCode;

                    if (pipelining > 1)
                    {
                        worker.WritePipelined(pipelining);
                        worker.Flush();
                        for (var j = 0; j < pipelining; j++)
                        {
                            worker.ReadPipelined(out tmpStatusCode);
                        }
                    }
                    else
                    {
                        worker.Write();
                        worker.Flush();
                        worker.Read(out tmpStatusCode);
                    }
                }
                catch (Exception ex)
                {
                }
            }

            if (pipelining == 1)
            {
                while (!cancellationToken.IsCancellationRequested && duration.TotalMilliseconds > sw.Elapsed.TotalMilliseconds && (!count.HasValue || current < count.Value))
                {
                    current++;
                    try
                    {
                        sw2.Restart();
                        worker.Write();
                        worker.Flush();
                        int statusCode;
                        var length = worker.Read(out statusCode);
                        result.Add((int)(sw.ElapsedTicks / Stopwatch.Frequency), length, (float)sw2.ElapsedTicks / Stopwatch.Frequency * 1000, statusCode, trackResponseTime);
                    }
                    catch (Exception ex)
                    {
                        result.AddError((int)(sw.ElapsedTicks / Stopwatch.Frequency), (float)sw2.ElapsedTicks / Stopwatch.Frequency * 1000, ex);
                    }
                }
            }
            else
            {
                try
                {
                    sw2.Restart();
                    worker.WritePipelined(pipelining);
                    worker.Flush();
                }
                catch (Exception ex)
                {
                    result.AddError((int)(sw.ElapsedTicks / Stopwatch.Frequency), (float)sw2.ElapsedTicks / Stopwatch.Frequency * 1000, ex);
                }

                while (!cancellationToken.IsCancellationRequested && duration.TotalMilliseconds > sw.Elapsed.TotalMilliseconds)
                {
                    try
                    {
                        for (var j = 0; j < pipelining; j++)
                        {
                            int statusCode;
                            var length = worker.ReadPipelined(out statusCode);
                            result.Add((int)Math.Floor((float)sw.ElapsedTicks / Stopwatch.Frequency), length, (float)sw2.ElapsedTicks / Stopwatch.Frequency * 1000, statusCode, trackResponseTime);

                            if (j == 0 && !cancellationToken.IsCancellationRequested && duration.TotalMilliseconds > sw.Elapsed.TotalMilliseconds)
                            {
                                sw3.Restart();
                                worker.WritePipelined(pipelining);
                                worker.Flush();
                            }
                        }

                        var tmp = sw2;
                        sw2 = sw3;
                        sw3 = tmp;
                    }
                    catch (Exception ex)
                    {
                        result.AddError((int)(sw.ElapsedTicks / Stopwatch.Frequency), (float)sw2.ElapsedTicks / Stopwatch.Frequency * 1000, ex);
                    }
                }
            }

            results.Enqueue(result);
            resetEvent.Set();
        }