private void PrintSummary(RunResultBase runResult)
 {
     if (runResult is RunResult)
     {
         PrintRunSummary(runResult as RunResult);
     }
     else if (runResult is ThreadedRunResult)
     {
         PrintRunSummary(runResult as ThreadedRunResult);
     }
 }
        private long GetPercentile(RunResultBase results, double percentile, Func <IterationCounterBase, long> propertyAccessor, bool lowerIsBetter)
        {
            Debug.Assert(percentile > 0 && percentile < 1);
            var sortedDataPoints = lowerIsBetter ?
                                   results.IterationCounters.OrderBy(propertyAccessor) :
                                   results.IterationCounters.OrderByDescending(propertyAccessor);
            var total           = sortedDataPoints.Count();
            var percentileIndex = (int)(total * percentile);

            return(propertyAccessor(sortedDataPoints.ElementAt(percentileIndex)));
        }
        private IEnumerable <PerformanceMetric> ConvertResultToMetrics(RunResultBase runResult)
        {
            var metrics = new List <PerformanceMetric>();

            if (runResult.Successful)
            {
                metrics.Add(
                    new PerformanceMetric
                {
                    Scenario = string.Format("{0} {1}", runResult.TestName, TestConfig.Instance.RuntimeFlavor).Trim(),
                    Metric   = "total",
                    Unit     = "Milliseconds",
                    Value    = runResult.ElapsedMillis
                });

                if (runResult.IterationCounters.Count > 1)
                {
                    foreach (var i in new[] { 0.95, 0.99, 0.999 })
                    {
                        var  percentile       = (i * 100).ToString(CultureInfo.InvariantCulture);
                        long resultPercentile = 0;

                        if (runResult.IterationCounters.First() is ThreadedIterationCounter)
                        {
                            resultPercentile = GetPercentile(runResult, i,
                                                             c => ((ThreadedIterationCounter)c).RequestsPerSecond, true);
                        }
                        else if (runResult.IterationCounters.First() is IterationCounter)
                        {
                            resultPercentile = GetPercentile(runResult, i,
                                                             c => ((IterationCounter)c).ElapsedMillis, true);
                        }
                        var metric = string.Format("{0}th percentile", percentile);

                        metrics.Add(
                            new PerformanceMetric
                        {
                            Scenario = string.Format("{0} {1}", runResult.TestName, TestConfig.Instance.RuntimeFlavor).Trim(),
                            Metric   = metric,
                            Unit     = "Milliseconds",
                            Value    = resultPercentile
                        });
                    }
                }
            }
            return(metrics);
        }