Пример #1
0
        private static List <RequestResult> ExecuteConnection(DrillOptions drillOptions)
        {
            var restClient = new RestClient(new Uri(drillOptions.Url));

            var drillResults = new List <RequestResult>();

            var request = CreateRequest(drillOptions);

            ExecutePreDrillProcesses(drillOptions, request);

            var sessionStopWatch = new Stopwatch();

            sessionStopWatch.Start();

            while (sessionStopWatch.Elapsed < TimeSpan.FromMilliseconds(drillOptions.MillisecondsToDrill))
            {
                drillResults.Add(SendRequest(request, restClient));

                if (drillOptions.MillisecondsToWaitAfterRequest.HasValue)
                {
                    Thread.Sleep(drillOptions.MillisecondsToWaitAfterRequest.Value);
                }
            }

            return(drillResults);
        }
Пример #2
0
        private static RestRequest CreateRequest(DrillOptions drillOptions)
        {
            var request = new RestRequest(GetMethod(drillOptions.RequestMethod));

            if (drillOptions.Body != null)
            {
                if (drillOptions.BodyType == BodyType.Json)
                {
                    request.AddJsonBody(drillOptions.Body);
                }
                else
                {
                    request.AddXmlBody(drillOptions.Body);
                }
            }

            if (drillOptions.QueryStringParameters != null && drillOptions.QueryStringParameters.Any())
            {
                foreach (var queryStringParameter in drillOptions.QueryStringParameters)
                {
                    request.AddQueryParameter(queryStringParameter.Key, queryStringParameter.Value);
                }
            }

            if (drillOptions.RequestHeaders != null && drillOptions.RequestHeaders.Any())
            {
                foreach (var requestHeader in drillOptions.RequestHeaders)
                {
                    request.AddHeader(requestHeader.Key, requestHeader.Value);
                }
            }

            return(request);
        }
Пример #3
0
        public DrillStats DrillUrl(DrillOptions drillOptions)
        {
            if (drillOptions.Body != null && drillOptions.BodyType == null)
            {
                throw new ArgumentException("You supplied a Body object, so you must supply a BodyType");
            }

            return(ExecuteConnections(drillOptions));
        }
Пример #4
0
        private static void ExecutePreDrillProcesses(DrillOptions drillOptions, RestRequest request)
        {
            if (drillOptions.PreDrillProcesses == null || !drillOptions.PreDrillProcesses.Any())
            {
                return;
            }

            foreach (var drillOptionsDrillPreProcessor in drillOptions.PreDrillProcesses)
            {
                drillOptionsDrillPreProcessor.Execute(drillOptions, request);
            }
        }
Пример #5
0
        private static DrillStats ExecuteConnections(DrillOptions drillOptions)
        {
            ThreadPool.SetMinThreads(drillOptions.ConnectionCount,
                                     drillOptions.ConnectionCount);

            ServicePointManager.DefaultConnectionLimit = drillOptions.ConnectionCount;

            var aggregatedResults = new List <RequestResult>();

            List <RequestResult> RequestFunc(DrillOptions options)
            {
                return(ExecuteConnection(options));
            }

            var executionDataflowBlockOptions = new ExecutionDataflowBlockOptions
            {
                MaxDegreeOfParallelism = drillOptions.ConnectionCount
            };

            var transformBlock = new TransformBlock <DrillOptions, List <RequestResult> >(
                RequestFunc, executionDataflowBlockOptions);

            for (var i = 0; i < drillOptions.ConnectionCount; i++)
            {
                transformBlock.Post(drillOptions);
            }

            for (var i = 0; i < drillOptions.ConnectionCount; i++)
            {
                var requestResults = transformBlock.Receive();

                aggregatedResults.AddRange(requestResults);
            }

            var drillStats = new DrillStats
            {
                AverageResponseTime = aggregatedResults.Average(a => a.ResponseMilliseconds),
                ConnectionCount     = drillOptions.ConnectionCount,
                TotalRequestCount   = aggregatedResults.Count,
                FailureCount        = aggregatedResults.Count(c => !c.IsSuccessful),
                RequestResults      = aggregatedResults
            };

            drillOptions.Recorder?.RecordDrill(drillOptions, drillStats);

            return(drillStats);
        }