public void EnumCounters() { var collection = new CounterCollection <TestCounters>(); XAssert.IsTrue(CounterCollection <TestCounters> .IsStopwatch(TestCounters.SomeTime)); XAssert.IsFalse(CounterCollection <TestCounters> .IsStopwatch(TestCounters.SomeCount)); // Increase the time using (collection.StartStopwatch(TestCounters.SomeTime)) { Thread.Sleep(1); } TimeSpan elapsed = collection.GetElapsedTime(TestCounters.SomeTime); XAssert.IsTrue(elapsed.Ticks > 0); XAssert.AreEqual <long>(0, collection.GetCounterValue(TestCounters.SomeCount)); collection.AddToCounter(TestCounters.SomeCount, 2); XAssert.AreEqual(elapsed, collection.GetElapsedTime(TestCounters.SomeTime)); XAssert.AreEqual <long>(2, collection.GetCounterValue(TestCounters.SomeCount)); TimeSpan delta = new TimeSpan(100); collection.AddToCounter(TestCounters.SomeTime, delta); TimeSpan newElapsed = collection.GetElapsedTime(TestCounters.SomeTime); // note: must not check if `newElapsed == elapsed.Add(delta)` because // `AddToCounter(); GetElapsedTime()` is lossy due to float-point arithmetic XAssert.IsTrue(newElapsed > elapsed); }
/// <summary> /// Appends <paramref name="other"/> into a current collection instance. /// </summary> public void Append(CounterCollection <TEnum> other) { foreach (var value in EnumTraits <TEnum> .EnumerateValues()) { if (IsStopwatch(value)) { AddToCounter(value, other.GetElapsedTime(value)); } AddToCounter(value, other.GetCounterValue(value)); } }
public void Temporal() { var collection = new CounterCollection <TestCounters>(); Stopwatch sw = Stopwatch.StartNew(); while (sw.ElapsedMilliseconds < 300) { using (collection.StartStopwatch(TestCounters.SomeTime)) { Thread.Sleep(30); } } sw.Stop(); // These times won't be exactly the same since the counter stopwatch is only measuring the sleep, not the loop and counter manipulation etc. // In any of those steps we can get unlucky and be descheduled. But a large discrepancy is in high likelihood some serious fault. TimeSpan delta = sw.Elapsed - collection.GetElapsedTime(TestCounters.SomeTime); XAssert.IsTrue( delta < 100.MillisecondsToTimeSpan(), "Difference between System.Diagnostics.Stopwatch and the Counter stopwatch was very large"); }