Ejemplo n.º 1
0
        public void ForThreeExecutions_ShouldComputeMedianCorrectly()
        {
            //Act
            List <double> executions = new List <double>();

            var execution1 = ExecutionTimeCounter.Start("TEST");

            Thread.Sleep(1);
            ExecutionTimeCounter.Stop(execution1);

            executions.Add(execution1.Elapsed.TotalMilliseconds);

            var execution2 = ExecutionTimeCounter.Start("TEST");

            Thread.Sleep(6);
            ExecutionTimeCounter.Stop(execution2);

            executions.Add(execution2.Elapsed.TotalMilliseconds);

            var execution3 = ExecutionTimeCounter.Start("TEST");

            Thread.Sleep(1);
            ExecutionTimeCounter.Stop(execution3);

            executions.Add(execution3.Elapsed.TotalMilliseconds);

            var medianExpected = executions.OrderBy(x => x).ElementAt(1);
            var median         = ExecutionTimeCounter.Results().Single(r => r.SectionName == "TEST").Median;

            //Assert
            Assert.AreEqual(medianExpected, median.TotalMilliseconds, 0.001);
        }
Ejemplo n.º 2
0
        public void ForOddExecutionsCount_ShouldComputeMedianCorrectly()
        {
            //Act
            for (int j = 0; j < 32; j++)
            {
                var execution = ExecutionTimeCounter.Start("0");
                Thread.Sleep(1);
                ExecutionTimeCounter.Stop(execution);

                executionsDictionary["0"].Add(execution.Elapsed.TotalMilliseconds);
            }

            var results = ExecutionTimeCounter.Results();

            //Assert
            var medianExpected = executionsDictionary["0"].OrderBy(x => x).ElementAt(23);

            Assert.AreEqual(medianExpected, results.Single(r => r.SectionName == "0").Median.TotalMilliseconds, 0.001);

            for (int i = 1; i < iterations; i++)
            {
                medianExpected = executionsDictionary[i.ToString()].OrderBy(x => x).ElementAt(7);
                Assert.AreEqual(medianExpected, results.Single(r => r.SectionName == i.ToString()).Median.TotalMilliseconds, 0.001);
            }
        }
Ejemplo n.º 3
0
        public void ForOneExecution_ShouldComputeMedianCorrectly()
        {
            //Act
            var execution = ExecutionTimeCounter.Start("TEST");

            Thread.Sleep(4);
            ExecutionTimeCounter.Stop(execution);

            var median = ExecutionTimeCounter.Results().Single(r => r.SectionName == "TEST").Median;

            //Assert
            Assert.AreEqual(execution.Elapsed, median);
        }
        public void ShouldStopStopwatch()
        {
            //Arrange & Act
            var execution = ExecutionTimeCounter.Start("SectionName");

            ExecutionTimeCounter.Stop(execution);

            var elapsed = execution.Elapsed;

            Thread.Sleep(1);

            //Assert
            Assert.AreEqual(elapsed, execution.Elapsed);
        }
Ejemplo n.º 5
0
        public void ForTwoExecutions_ShouldComputeMedianCorrectly()
        {
            //Act
            var execution1 = ExecutionTimeCounter.Start("TEST");

            Thread.Sleep(3);
            ExecutionTimeCounter.Stop(execution1);

            var execution2 = ExecutionTimeCounter.Start("TEST");

            Thread.Sleep(5);
            ExecutionTimeCounter.Stop(execution2);

            var medianExpected = (execution1.Elapsed.TotalMilliseconds + execution2.Elapsed.TotalMilliseconds) / 2.0;
            var median         = ExecutionTimeCounter.Results().Single(r => r.SectionName == "TEST").Median;

            //Assert
            Assert.AreEqual(medianExpected, median.TotalMilliseconds, 0.001);
        }
Ejemplo n.º 6
0
        protected void CreateExecutions(int iterations, int sleepTime)
        {
            for (int i = 0; i < iterations; i++)
            {
                if (!executionsDictionary.ContainsKey(i.ToString()))
                {
                    executionsDictionary.Add(i.ToString(), new List <double>());
                }

                for (int j = 0; j < iterations; j++)
                {
                    var execution = ExecutionTimeCounter.Start(i.ToString());
                    Thread.Sleep(sleepTime);
                    ExecutionTimeCounter.Stop(execution);

                    executionsDictionary[i.ToString()].Add(execution.Elapsed.TotalMilliseconds);
                }
            }
        }
Ejemplo n.º 7
0
        public void ForEvenUnsortedExecutionsCount_ShouldComputeMedianCorrectly()
        {
            //Act
            List <double> executions = new List <double>();

            var execution = ExecutionTimeCounter.Start("TEST");

            Thread.Sleep(5);
            ExecutionTimeCounter.Stop(execution);

            executions.Add(execution.Elapsed.TotalMilliseconds);

            execution = ExecutionTimeCounter.Start("TEST");
            Thread.Sleep(1);
            ExecutionTimeCounter.Stop(execution);

            executions.Add(execution.Elapsed.TotalMilliseconds);

            execution = ExecutionTimeCounter.Start("TEST");
            Thread.Sleep(3);
            ExecutionTimeCounter.Stop(execution);

            executions.Add(execution.Elapsed.TotalMilliseconds);

            execution = ExecutionTimeCounter.Start("TEST");
            Thread.Sleep(7);
            ExecutionTimeCounter.Stop(execution);

            executions.Add(execution.Elapsed.TotalMilliseconds);

            var orderedExecutions = executions.OrderBy(x => x);

            var medianExpected = (orderedExecutions.ElementAt(1) + orderedExecutions.ElementAt(2)) / 2.0;
            var medianActual   = ExecutionTimeCounter.Results().Single(r => r.SectionName == "TEST").Median;

            //Assert
            Assert.AreEqual(medianExpected, medianActual.TotalMilliseconds, 0.001);
        }