public void Calculate_Should_Return_Zero_Medians_On_No_results_of_GivenDate_Or_DateBefore_GivenDate()
        {
            //  Arrange

            var currentDate = new DateTime(2020, 2, 12);

            var benchMarkResults = new List <BenchMarkResult>()
            {
                new BenchMarkResult()
                {
                    RequestDuration = 11,
                    CreatedAt       = new DateTimeOffset(new DateTime(2020, 2, 13)), // tomorrow
                },
                new BenchMarkResult()
                {
                    RequestDuration = 200,
                    CreatedAt       = new DateTimeOffset(new DateTime(2020, 2, 10)), // not yesterday but before
                },
            };



            //  Act

            var benchMarkData = MedianCalculator.Calculate(currentDate, benchMarkResults);



            //  Assert

            Assert.NotNull(benchMarkData);
            Assert.Equal(0, benchMarkData.CurrentDay);
            Assert.Equal(0, benchMarkData.PreviousDay);
            Assert.Equal(0, benchMarkData.DifferencePercentage); // for the difference an divide by currentday exists so make so it doesnt crash ;)
        }
        public void Calculate_Should_Return_Zero_Medians_On_No_results()
        {
            //  Arrange

            var currentDate = new DateTime(2020, 2, 2);

            var benchMarkResults = new List <BenchMarkResult>()
            {
            };



            //  Act

            var benchMarkData = MedianCalculator.Calculate(currentDate, benchMarkResults);



            //  Assert

            Assert.NotNull(benchMarkData);
            Assert.Equal(0, benchMarkData.CurrentDay);
            Assert.Equal(0, benchMarkData.PreviousDay);
            Assert.Equal(0, benchMarkData.DifferencePercentage); // for the difference an divide by currentday exists so make so it doesnt crash ;)
        }
Exemple #3
0
        public BenchMarkData ConvertToBenchMarkData(List <BenchMarkResult> resultDataPoints)
        {
            var benchmarkData = new BenchMarkData()
            {
                CloudProvider      = Enum.ToObject(typeof(CloudProvider), resultDataPoints.First().CloudProvider).ToString(),
                HostingEnvironment =
                    Enum.ToObject(typeof(HostingEnvironment), resultDataPoints.First().HostingEnvironment).ToString(),
                Runtime = Enum.ToObject(typeof(Runtime), resultDataPoints.First().Runtime).ToString()
            };

            var currentDate = resultDataPoints.OrderByDescending(c => c.CreatedAt).First().CreatedAt.Date;


            var coldMedians = MedianCalculator.Calculate(currentDate, resultDataPoints, true);

            benchmarkData.ColdMedianExecutionTime = coldMedians.currentDay;

            var coldDifference =
                Math.Round(
                    ((coldMedians.currentDay - coldMedians.previousDay) /
                     Math.Abs(coldMedians.currentDay)) * 100, 2);

            benchmarkData.ColdPreviousDayDifference = coldDifference;
            benchmarkData.ColdPreviousDayPositive   = benchmarkData.ColdPreviousDayDifference < 0;

            var warmMedians = MedianCalculator.Calculate(currentDate, resultDataPoints, false);

            benchmarkData.WarmMedianExecutionTime = warmMedians.currentDay;

            var warmDifference =
                Math.Round(
                    ((warmMedians.currentDay - warmMedians.previousDay) /
                     Math.Abs(warmMedians.currentDay)) * 100, 2);

            benchmarkData.WarmPreviousDayDifference = warmDifference;
            benchmarkData.WarmPreviousDayPositive   = benchmarkData.WarmPreviousDayDifference < 0;

            var dates = new List <DateTime>();

            for (var i = 0; i < Convert.ToInt32(Environment.GetEnvironmentVariable("dayRange")); i++)
            {
                dates.Add(currentDate - TimeSpan.FromDays(i));
            }

            dates = dates.OrderBy(c => c.Date).ToList();

            foreach (var date in dates)
            {
                benchmarkData.ColdDataPoints.Add(new DataPoint(date.ToString("yyyy MMMM dd"),
                                                               resultDataPoints.Where(c => c.CreatedAt.Date == date.Date && c.IsColdRequest)
                                                               .Select(c => c.RequestDuration).ToList()));

                benchmarkData.WarmDataPoints.Add(new DataPoint(date.ToString("yyyy MMMM dd"),
                                                               resultDataPoints.Where(c => c.CreatedAt.Date == date.Date && !c.IsColdRequest)
                                                               .Select(c => c.RequestDuration).ToList()));
            }

            return(benchmarkData);
        }
        public BenchMarkData ConvertToBenchMarkData(List <BenchMarkResult> resultDataPoints)
        {
            var firstResult   = resultDataPoints.OrderByDescending(c => c.CreatedAt).First();
            var benchmarkData = new BenchMarkData()
            {
                CloudProvider      = firstResult.CloudProvider.ToString(),
                HostingEnvironment = firstResult.HostingEnvironment.ToString(),
                Runtime            = firstResult.Runtime.ToString()
            };

            var currentDate = firstResult.CreatedAt.Date;


            var coldDataPoints = resultDataPoints.Where(c => c.IsColdRequest).ToList();
            var coldMedians    = MedianCalculator.Calculate(currentDate, coldDataPoints);

            benchmarkData.ColdMedianExecutionTime   = coldMedians.CurrentDay;
            benchmarkData.ColdPreviousDayDifference = coldMedians.DifferencePercentage;
            benchmarkData.ColdPreviousDayPositive   = benchmarkData.ColdPreviousDayDifference < 0;

            var warmDataPoints = resultDataPoints.Where(c => !c.IsColdRequest).ToList();
            var warmMedians    = MedianCalculator.Calculate(currentDate, warmDataPoints);

            benchmarkData.WarmMedianExecutionTime   = warmMedians.CurrentDay;
            benchmarkData.WarmPreviousDayDifference = warmMedians.DifferencePercentage;
            benchmarkData.WarmPreviousDayPositive   = benchmarkData.WarmPreviousDayDifference < 0;


            var dates = new List <DateTime>();

            for (var i = 0; i < Convert.ToInt32(Environment.GetEnvironmentVariable("dayRange")); i++)
            {
                dates.Add(currentDate - TimeSpan.FromDays(i));
            }

            dates = dates.OrderBy(c => c.Date).ToList();

            foreach (var date in dates)
            {
                benchmarkData.ColdDataPoints.Add(new DataPoint(date.ToString("yyyy MMMM dd"),
                                                               coldDataPoints.Where(c => c.CreatedAt.Date == date.Date)
                                                               .Select(c => c.RequestDuration).ToList()));

                benchmarkData.WarmDataPoints.Add(new DataPoint(date.ToString("yyyy MMMM dd"),
                                                               warmDataPoints.Where(c => c.CreatedAt.Date == date.Date)
                                                               .Select(c => c.RequestDuration).ToList()));
            }

            return(benchmarkData);
        }
        public void Calculate_Should_Return_Calculated_Medians_From_Results_By_Dates_()
        {
            //  Arrange

            var currentDate = new DateTime(2020, 2, 11);

            var benchMarkResults = new List <BenchMarkResult>()
            {
                new BenchMarkResult()
                {
                    RequestDuration = 80,
                    CreatedAt       = new DateTimeOffset(new DateTime(2020, 2, 11)), // today
                },
                new BenchMarkResult()
                {
                    RequestDuration = 20,
                    CreatedAt       = new DateTimeOffset(new DateTime(2020, 2, 11)), // today
                },
                new BenchMarkResult()
                {
                    RequestDuration = 11,
                    CreatedAt       = new DateTimeOffset(new DateTime(2020, 2, 10)), // yesterday
                },
                new BenchMarkResult()
                {
                    RequestDuration = 33,
                    CreatedAt       = new DateTimeOffset(new DateTime(2020, 2, 10)), // yesterday
                },
            };



            //  Act

            var benchMarkData = MedianCalculator.Calculate(currentDate, benchMarkResults);



            //  Assert

            Assert.NotNull(benchMarkData);
            Assert.Equal(50, benchMarkData.CurrentDay);
            Assert.Equal(22, benchMarkData.PreviousDay);
            Assert.Equal(56, benchMarkData.DifferencePercentage);
        }
        public void Calculate_Should_Return_Calculated_CurrentMedian_From_Results_By_Dates_Matching_Given()
        {
            //  Arrange

            var currentDate = new DateTime(2020, 2, 11);

            var benchMarkResults = new List <BenchMarkResult>()
            {
                new BenchMarkResult()
                {
                    RequestDuration = 11,
                    CreatedAt       = new DateTimeOffset(new DateTime(2020, 2, 11)), // today
                },
                new BenchMarkResult()
                {
                    RequestDuration = 33,
                    CreatedAt       = new DateTimeOffset(new DateTime(2020, 2, 11)), // today
                },
                new BenchMarkResult()
                {
                    RequestDuration = 200,
                    CreatedAt       = new DateTimeOffset(new DateTime(2020, 2, 10)), // yesterday
                },
            };



            //  Act

            var benchMarkData = MedianCalculator.Calculate(currentDate, benchMarkResults);



            //  Assert

            Assert.NotNull(benchMarkData);
            Assert.Equal(22, benchMarkData.CurrentDay);
        }