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); }
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); } }
public void ShouldInitializeExecutionWithCorrectSectionName(string sectionName) { //Arrange & Act var execution = ExecutionTimeCounter.Start(sectionName); //Assert Assert.AreEqual(sectionName, execution.SectionName); }
public void ShouldReturnNotEmptyExecution() { //Arrange & Act var execution = ExecutionTimeCounter.Start("SectionName"); //Assert Assert.IsNotNull(execution); }
public void ShouldReturnStartedExecution() { //Arrange & Act var execution = ExecutionTimeCounter.Start("SectionName"); Thread.Sleep(1); //Assert Assert.Greater(execution.Elapsed, new TimeSpan(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); }
public void ShouldAlwaysReturnNewExecution() { //Arrange var iterations = 5; var executions = new List <Execution>(); //Act for (int i = 0; i < iterations; i++) { executions.Add(ExecutionTimeCounter.Start("SectionName")); } //Assert for (int i = 0; i < iterations; i++) { Assert.AreEqual(1, executions.Count(e => e == executions[i])); } }
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); }
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); } } }
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); }