Exemplo n.º 1
0
        private static void DoWork(TimeSpan duration, int?count, int pipelining, ConcurrentQueue <EndpointResult> results, Stopwatch sw, CancellationToken cancellationToken, ManualResetEventSlim resetEvent, int workerIndex)
        {
            var sw2 = new Stopwatch();
            var sw3 = new Stopwatch();

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

            var request = MultipleRequestsStore.Get();

            if (!string.IsNullOrEmpty(request.Item2))
            {
                requestByteArray = Encoding.UTF8.GetBytes(request.Item2);
            }
            if (!string.IsNullOrEmpty(request.Item3))
            {
                bodyByteArray = Encoding.UTF8.GetBytes(request.Item3);
            }

            Uri uri = null;

            if (!Uri.TryCreate(request.Item1, UriKind.Absolute, out uri))
            {
                resetEvent.Set();
                return;
            }

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

            // 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);

                        var result = createResult(request);
                        result.elapsed        = (float)sw2.ElapsedTicks / Stopwatch.Frequency * 1000;
                        result.responselength = length;
                        result.statuscode     = statusCode.ToString();
                        results.Enqueue(result);
                    }
                    catch (Exception ex)
                    {
                        var result = createResult(request);
                        result.exception = ex.ToString();
                        result.error     = true;
                        results.Enqueue(result);
                    }
                }
            }
            else
            {
                try
                {
                    sw2.Restart();
                    worker.WritePipelined(pipelining);
                    worker.Flush();
                }
                catch (Exception ex)
                {
                    var result = createResult(request);
                    result.exception = ex.ToString();
                    result.error     = true;
                    results.Enqueue(result);
                }

                while (!cancellationToken.IsCancellationRequested && duration.TotalMilliseconds > sw.Elapsed.TotalMilliseconds)
                {
                    try
                    {
                        for (var j = 0; j < pipelining; j++)
                        {
                            int statusCode;
                            var length = worker.ReadPipelined(out statusCode);

                            var result = createResult(request);
                            result.elapsed        = (float)sw2.ElapsedTicks / Stopwatch.Frequency * 1000;
                            result.responselength = length;
                            result.statuscode     = statusCode.ToString();
                            results.Enqueue(result);

                            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)
                    {
                        var result = createResult(request);
                        result.exception = ex.ToString();
                        result.error     = true;
                        results.Enqueue(result);
                    }
                }
            }

            resetEvent.Set();
        }
Exemplo n.º 2
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();
        }
Exemplo n.º 3
0
        private static void DoWork(Uri uri, TimeSpan duration, int? count, int pipelining, ConcurrentQueue<WorkerThreadResult> results, Stopwatch sw, CancellationToken cancellationToken, ManualResetEventSlim resetEvent, int workerIndex)
        {
            var result = new WorkerThreadResult();
            var sw2 = new Stopwatch();
            var sw3 = new Stopwatch();
            var worker = new HttpWorker(uri);
            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) { }
            }

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