Exemplo n.º 1
0
        private static void Run(CommandLineOptions commandLineOptions, CancellationTokenSource source,
                                IAsyncRequester requester, ConcurrentBag <HttpStatusCode> statusCodes, ConcurrentBag <double> timeTakens, int total)
        {
            var customThreadPool = new CustomThreadPool(new WorkItemFactory(requester, commandLineOptions.NumberOfRequests, commandLineOptions.DelayInMillisecond),
                                                        commandLineOptions.Concurrency);

            customThreadPool.WorkItemFinished += (sender, args) =>
            {
                if (args.Result.NoWork)
                {
                    return;
                }

                statusCodes.Add((HttpStatusCode)args.Result.Status);
                timeTakens.Add(args.Result.Ticks);
                var n       = Interlocked.Increment(ref total);
                var logData = new LogData()
                {
                    Millis     = (int)args.Result.Ticks / TimeSpan.TicksPerMillisecond,
                    Index      = args.Result.Index,
                    EventDate  = DateTimeOffset.Now,
                    StatusCode = args.Result.Status,
                    Parameters = args.Result.Parameters
                };

                _logDataQueue.Enqueue(logData);

                if (!commandLineOptions.Verbose)
                {
                    Console.Write("\r" + total);
                }
            };

            customThreadPool.Start(commandLineOptions.NumberOfRequests, source);

            while (!source.IsCancellationRequested)
            {
                Thread.Sleep(200);
            }
        }
Exemplo n.º 2
0
        private static Reporter Run(CommandLineOptions commandLineOptions, CancellationTokenSource source,
                                    IAsyncRequester requester, int total, string reportFolder)
        {
            var until = DateTimeOffset.MaxValue;

            if (!commandLineOptions.IsDryRun && commandLineOptions.NumberOfSeconds > 0)
            {
                commandLineOptions.NumberOfRequests = int.MaxValue / 10;
                until = DateTimeOffset.Now.AddSeconds(commandLineOptions.NumberOfSeconds + commandLineOptions.WarmupSeconds);
            }

            var warmUpTotal      = 0;
            var customThreadPool = new CustomThreadPool(new WorkItemFactory(
                                                            requester,
                                                            commandLineOptions.NumberOfRequests,
                                                            commandLineOptions.DelayInMillisecond,
                                                            commandLineOptions.WarmupSeconds),
                                                        source,
                                                        commandLineOptions.Concurrency,
                                                        commandLineOptions.WarmupSeconds);

            var reporter = new Reporter(Environment.CommandLine, commandLineOptions.ReportSliceSeconds, () => customThreadPool.WorkerCount);

            if (commandLineOptions.WarmupSeconds == 0)
            {
                reporter.Start();
            }

            customThreadPool.WarmupFinished += (sender, args) =>
            {
                _stopwatch.Restart();
                reporter.Start();
            };

            customThreadPool.WorkItemFinished += (sender, args) =>
            {
                if (args.Result.NoWork)
                {
                    return;
                }

                if (args.Result.IsWarmUp)
                {
                    var t = Interlocked.Increment(ref warmUpTotal);
                    if (!commandLineOptions.Verbose)
                    {
                        ConsoleWrite(ConsoleColor.Green, "\rWarmup [Users {1}]: {0}", warmUpTotal, customThreadPool.WorkerCount);
                    }

                    return;
                }

                reporter.AddResponse((HttpStatusCode)args.Result.Status, (long)args.Result.Ticks);
                ConsiderUpdatingReport(reporter, reportFolder, !commandLineOptions.DontBrowse);

                var n       = Interlocked.Increment(ref total);
                var logData = new LogData()
                {
                    Millis     = (int)args.Result.Ticks / TimeSpan.TicksPerMillisecond,
                    Index      = args.Result.Index,
                    EventDate  = DateTimeOffset.Now,
                    StatusCode = args.Result.Status,
                    Parameters = args.Result.Parameters
                };

                _logDataQueue.Enqueue(logData);

                if (DateTimeOffset.Now > until)
                {
                    source.Cancel();
                }

                if (!commandLineOptions.Verbose)
                {
                    ConsoleWrite(ConsoleColor.DarkYellow, "\r{0}	(RPS: {1})			", total, Math.Round(total * 1000f / _stopwatch.ElapsedMilliseconds, 1));
                }
            };

            customThreadPool.Start(commandLineOptions.NumberOfRequests);

            // set until - only after started
            if (!commandLineOptions.IsDryRun && commandLineOptions.NumberOfSeconds > 0)
            {
                until = DateTimeOffset.Now.AddSeconds(commandLineOptions.NumberOfSeconds + commandLineOptions.WarmupSeconds);
            }

            while (!source.IsCancellationRequested)
            {
                Thread.Sleep(200);
            }

            return(reporter);
        }