Example #1
0
        private static async Task RunRequestTreeAgent(HttpRequestTree requestTree, int Concurrency, ResultComparerFactory comparerFactory, string requestSource, string outputFilePath)
        {
            // Draw run header
            Console.WriteLine(CliResultViews.StartRequestString, requestSource, Concurrency);

            var requestResult = await RunRequestComparisonAsync(Concurrency, requestTree, comparerFactory);

            var grades = requestResult.Grades;
            var stats  = requestResult.Stats;

            // Draw Stats
            CliResultViews.DrawGrades(grades);
            CliResultViews.DrawStatusCodes(stats);
            CliResultViews.DrawStats(stats);

            var time   = DateTime.UtcNow.ToString("yyyyMMddTHHmmss");
            var result = new HttpRequestTree()
            {
                Requests    = requestResult.Grades.Requests,
                Description = $"{requestTree.Description} : {time}"
            };

            string outputFile = !string.IsNullOrWhiteSpace(outputFilePath)
                ? outputFilePath
                : $"output-{time}.json";

            // Draw output file path
            Console.WriteLine("Result path: {0}", outputFile);

            await new SaveHttpRequestTreeToJson().Execute(result, outputFile);
        }
Example #2
0
        public RequestTreeAgent(HttpRequestTree requestTree, ResultComparerFactory comparerFactory = null)
        {
            ServicePointManager.ServerCertificateValidationCallback = (message, certificate2, arg3, arg4) => true;
            _client = new HttpClient(new HttpClientHandler
            {
                ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true
            });

            _requestTree     = requestTree;
            _comparerFactory = comparerFactory;
        }
Example #3
0
        public async Task Request(RequestArgs args)
        {
            HttpRequestTree requestTree = null;

            // check if we have piped
            // input
            if (Console.IsInputRedirected)
            {
                using (Stream pipeStream = Console.OpenStandardInput())
                {
                    try
                    {
                        // try load TSV
                        requestTree = new LoadHttpRequestTreeFromTsv().Execute(pipeStream);
                    }
                    catch
                    {
                        // try load Url list
                        requestTree = new LoadHttpRequestTreeFromListOfUrls().Execute(pipeStream);
                    }
                }
            }

            // load from file
            if (requestTree == null && !string.IsNullOrWhiteSpace(args.RequestFilePath))
            {
                try
                {
                    // try to load json
                    requestTree = new LoadHttpRequestTreeFromJson().Execute(args.RequestFilePath);
                }
                catch
                {
                    try
                    {
                        // try to load tsv
                        requestTree = new LoadHttpRequestTreeFromTsv().Execute(args.RequestFilePath);
                    }
                    catch
                    {
                        // try to load url list
                        requestTree = new LoadHttpRequestTreeFromListOfUrls().Execute(args.RequestFilePath);
                    }
                }
            }

            string requestSource = !string.IsNullOrWhiteSpace(args.RequestFilePath)
                ? args.RequestFilePath
                : "Pipe";

            await RunRequestTreeAgent(requestTree, args.Concurrency, null, requestSource, args.OutputFilePath);
        }
        public async Task Execute(HttpRequestTree requestTree, string outputFile)
        {
            using (var stream = new FileStream(outputFile, FileMode.CreateNew))
            {
                var options = new JsonSerializerOptions
                {
                    IgnoreNullValues = true,
                    WriteIndented    = true
                };

                await JsonSerializer.SerializeAsync <HttpRequestTree>(stream, requestTree, options);
            }
        }
Example #5
0
        public async Task Generator(GeneratorArgs args)
        {
            // Load request generator and apply plugin args
            IRequestGenerator     requestGenerator = new LoadGenerator().Execute(args, PluginDir());
            IEnumerable <Request> requests         = requestGenerator.Run();

            HttpRequestTree requestTree = new HttpRequestTree()
            {
                Requests = requests, Description = requestGenerator.Name
            };

            await RunRequestTreeAgent(requestTree, args.Concurrency, requestGenerator.ComparerFactoryOverride, requestGenerator.Name, args.OutputFilePath);
        }
Example #6
0
        private static async Task <AgentResult> RunSimpleLoadTestAsync(Uri uri, int threads, TimeSpan duration, int count)
        {
            var generator = new SimpleLoadTestGenerator();

            var durationOption = generator.Options.First(o => o.Description.Key == "Duration");

            durationOption.Value = duration.Seconds.ToString();

            var countOption = generator.Options.First(o => o.Description.Key == "Count");

            countOption.Value = count.ToString();

            var urlOption = generator.Options.First(o => o.Description.Key == "Url");

            urlOption.Value = uri.AbsoluteUri;

            var tree = new HttpRequestTree()
            {
                Requests = generator.Run()
            };

            return(await RunRequestComparisonAsync(threads, tree, null));
        }
        public IEnumerable <Request> Run()
        {
            Option countOption = null;
            int    count;

            if (!int.TryParse((countOption = Options.FindLast(o => string.Equals(o.Description.Key, "Count"))).Value, out count))
            {
                throw new ArgumentException($"plugin parameter Count is not a valid value: {countOption?.Value}");
            }

            if (count < -1)
            {
                throw new ArgumentException($"plugin parameter Count is not a value grater than or equal to -1: {countOption?.Value}");
            }

            Option durationOption = null;
            int    duration;

            if (!int.TryParse((durationOption = Options.FindLast(o => string.Equals(o.Description.Key, "Duration"))).Value, out duration))
            {
                throw new ArgumentException($"plugin parameter Duration is not a valid value: {durationOption?.Value}");
            }

            if (duration < -1)
            {
                throw new ArgumentException($"plugin parameter Duration is not a value grater than or equal to -1: {durationOption?.Value}");
            }

            List <Request> srcRequests = null;

            Option urlOption = null;
            Uri    uri;

            if (Uri.TryCreate((urlOption = Options.FindLast(o => string.Equals(o.Description.Key, "Url"))).Value, UriKind.Absolute, out uri))
            {
                srcRequests = new List <Request>()
                {
                    new Request
                    {
                        Url    = uri.AbsoluteUri,
                        Method = "Get"
                    }
                };
            }

            Option pathOption = null;
            string path       = (pathOption = Options.FindLast(o => string.Equals(o.Description.Key, "Path"))).Value;

            if (string.IsNullOrWhiteSpace(path) && File.Exists(path))
            {
                string          json        = File.ReadAllText(path);
                HttpRequestTree requestTree = JsonConvert.DeserializeObject <HttpRequestTree>(json);
                srcRequests = requestTree.Requests.ToList();
            }

            if (srcRequests == null)
            {
                throw new ArgumentException($"Uri or Path to request file must be supplied");
            }

            DateTime start = new DateTime(DateTime.Now.Ticks);
            int      i     = 0;

            while (
                (count == -1 || i < count) &&
                (duration == -1 || (DateTime.Now - start).Seconds <= duration))
            {
                if (count != -1)
                {
                    i++;
                }
                yield return(ProvideRequest(srcRequests, i));
            }
        }
Example #8
0
        private static async Task <AgentResult> RunRequestComparisonAsync(int threads, HttpRequestTree requestTree, ResultComparerFactory comparerFactory)
        {
            CancellationTokenSource source = new CancellationTokenSource();
            var token = source.Token;

            Console.CancelKeyPress += delegate {
                source.Cancel();
            };

            var agent = new RequestTreeAgent(requestTree, comparerFactory);

            return(await agent.Run(threads, token));
        }