예제 #1
0
파일: Program.cs 프로젝트: prasannavl/Wrex
        private static async Task RunWrexAsync(CmdOptions cmdOptions)
        {
            var validation = cmdOptions.Validate();

            if (!validation.IsValid)
            {
                var indent = new string(' ', 2);
                var errors = String.Join(Environment.NewLine, validation.ErrorMessages.Select(x => indent + x));
                throw new Exception(cmdOptions.CustomErrorWithUsage(errors));
            }

            Console.WriteLine(cmdOptions.GetHeader(new HelpText()));
            Console.WriteLine();

            try
            {
                var wrex = new Wrex(cmdOptions.GetWrexOptions());
                var consoleOut = new ConsoleOutput();

                if (cmdOptions.Verbose)
                {
                    const string OptionsText = "Options:";
                    Console.WriteLine(OptionsText);
                    Console.WriteLine(new string('-', OptionsText.Length));
                    Console.WriteLine();
                    consoleOut.PrintWrexOptions(wrex.Options);
                    Console.WriteLine();
                    const string ProgressText = "Progress:";
                    Console.WriteLine(ProgressText);
                    Console.WriteLine(new string('-', ProgressText.Length));
                    Console.WriteLine();
                }

                var cancelSource = new CancellationTokenSource();
                var progressDisplayTask = Task.Run(() => consoleOut.StartProgressWriter(wrex, cancelSource.Token));
                await wrex.RunAsync(
                    null,
                    ex =>
                        {
                            if (cmdOptions.Verbose)
                            {
                                consoleOut.HandleError(ex);
                            }
                            else
                            {
                                NoOp();
                            }
                        });

                cancelSource.Cancel();
                await progressDisplayTask;

                var analyzer = new WrexAnalyzer(wrex);
                var summary = analyzer.GetSummary();
                consoleOut.PrintSummary(summary, wrex);
                consoleOut.PrintStatusDistribution(analyzer.GetStatusDistribution(), wrex);
                consoleOut.PrintResponseTimeHistogram(analyzer.GetReponseTimeDistribution(summary), wrex);
                if (cmdOptions.Verbose)
                {
                    consoleOut.PrintSampleResponse(wrex);
                }
                Console.WriteLine();
            }
            catch (Exception ex)
            {
                lock (Console.Out)
                {
                    Console.WriteLine();
                    Console.WriteLine();
                    ExtendedConsole.WriteErrorLine("Error: " + ex.Message);
                    if (cmdOptions.Verbose)
                    {
                        Console.WriteLine();
                        Console.WriteLine("Details: ");
                        Console.WriteLine(ex.StackTrace);
                    }
                }
            }
        }