public void MeasureWithMultipleGroups() { ITimerInterval storedInterval = null; var storage = new Mock <IStoreIntervals>(); { storage.Setup(r => r.AddBaseInterval(It.IsAny <ITimerInterval>())) .Callback <ITimerInterval>(i => storedInterval = i); } var profiler = new Profiler(storage.Object); var description = "description"; for (int i = 0; i < 10; i++) { var group = new TimingGroup(); var interval = profiler.MeasureInterval(group, description); using (interval) { Thread.Sleep(10); } Assert.AreSame(interval, storedInterval); storage.Verify(r => r.AddChildInterval(It.IsAny <ITimerInterval>(), It.IsAny <ITimerInterval>()), Times.Never()); } }
public void FromStartTillEndWithSingleBase() { var owner = new Mock <IIntervalOwner>(); { owner.Setup(o => o.CurrentTicks) .Returns(10L); } var group = new TimingGroup(); var parents = new ITimerInterval[] { new TimerInterval(owner.Object, group, "a"), }; var children = new[] { new ITimerInterval[] { new TimerInterval(owner.Object, group, "aa"), new TimerInterval(owner.Object, group, "ab"), new TimerInterval(owner.Object, group, "ac"), }, }; var storage = BuildStorage(parents, children); var report = storage.FromStartTillEnd(); CheckReport(report, parents, children); CleanUpIntervals(parents, children); }
public void CopyTreeWithStartAndEndInDifferentGroups() { var owner = new Mock <IIntervalOwner>(); { owner.Setup(o => o.CurrentTicks) .Returns(10L); } var parents = new ITimerInterval[] { new TimerInterval(owner.Object, new TimingGroup(), "a"), new TimerInterval(owner.Object, new TimingGroup(), "b"), new TimerInterval(owner.Object, new TimingGroup(), "c"), }; var children = new[] { new ITimerInterval[0], new ITimerInterval[0], new ITimerInterval[0], }; var tree = BuildTree(parents, children); Assert.Throws <NonMatchingTimingGroupsException>(() => new TimingTree(tree, parents[0], parents[1])); CleanUpIntervals(parents, children); }
public void CopyTreeCompletely() { var owner = new Mock <IIntervalOwner>(); { owner.Setup(o => o.CurrentTicks) .Returns(10L); } var group = new TimingGroup(); var parents = new ITimerInterval[] { new TimerInterval(owner.Object, group, "a"), new TimerInterval(owner.Object, group, "b"), new TimerInterval(owner.Object, group, "c"), }; var children = new[] { new ITimerInterval[] { new TimerInterval(owner.Object, group, "aa"), new TimerInterval(owner.Object, group, "ab"), new TimerInterval(owner.Object, group, "ac"), }, new ITimerInterval[] { new TimerInterval(owner.Object, group, "ba"), new TimerInterval(owner.Object, group, "bb"), new TimerInterval(owner.Object, group, "bc"), }, new ITimerInterval[] { new TimerInterval(owner.Object, group, "ca"), new TimerInterval(owner.Object, group, "cb"), new TimerInterval(owner.Object, group, "cc"), }, }; var tree = BuildTree(parents, children); var otherTree = new TimingTree(tree); Assert.That(otherTree.BaseIntervals(group), Is.EquivalentTo(parents)); Assert.That(otherTree.ChildIntervals(parents[0]), Is.EquivalentTo(children[0])); Assert.That(otherTree.ChildIntervals(parents[1]), Is.EquivalentTo(children[1])); Assert.That(otherTree.ChildIntervals(parents[2]), Is.EquivalentTo(children[2])); CleanUpIntervals(parents, children); }
public void ForInterval() { var owner = new Mock <IIntervalOwner>(); { owner.Setup(o => o.CurrentTicks) .Returns(10L); } var group = new TimingGroup(); var parents = new ITimerInterval[] { new TimerInterval(owner.Object, group, "a"), new TimerInterval(owner.Object, group, "b"), new TimerInterval(owner.Object, group, "c"), }; var children = new[] { new ITimerInterval[] { new TimerInterval(owner.Object, group, "aa"), new TimerInterval(owner.Object, group, "ab"), new TimerInterval(owner.Object, group, "ac"), }, new ITimerInterval[] { new TimerInterval(owner.Object, group, "ba"), new TimerInterval(owner.Object, group, "bb"), new TimerInterval(owner.Object, group, "bc"), }, new ITimerInterval[] { new TimerInterval(owner.Object, group, "ca"), new TimerInterval(owner.Object, group, "cb"), new TimerInterval(owner.Object, group, "cc"), }, }; var storage = BuildStorage(parents, children); var report = storage.ForInterval(parents[1]); CheckReport(report, new[] { parents[1] }, new[] { children[1] }); CleanUpIntervals(parents, children); }
/// <summary> /// Initializes a new instance of the <see cref="TimingIntervalHelper"/> class. /// </summary> /// <param name="diagnostics">The object that provides access to the system wide diagnostics.</param> /// <param name="timingStorage">The object that generates reports for a stored interval.</param> /// <param name="collection">The object that stores the generated reports.</param> /// <param name="reportTransformer">The function which transforms a report into a string.</param> /// <param name="description">The description for the current interval.</param> public TimingIntervalHelper( SystemDiagnostics diagnostics, IGenerateTimingReports timingStorage, TimingReportCollection collection, Func<TimingReport, string> reportTransformer, string description) { { Debug.Assert(diagnostics != null, "The diagnostics object should not be a null reference."); Debug.Assert(timingStorage != null, "The storage object should not be a null reference."); Debug.Assert(collection != null, "The collection object should not be a null reference."); Debug.Assert(reportTransformer != null, "The report transformer object should not be a null reference."); Debug.Assert(!string.IsNullOrWhiteSpace(description), "The description string should not be a null reference or an empty string."); } m_Storage = timingStorage; m_Collection = collection; m_Transformer = reportTransformer; m_Interval = (ITimerInterval)diagnostics.Profiler.Measure(BaseConstants.TimingGroup, description); }
/// <summary> /// Initializes a new instance of the <see cref="TimingIntervalHelper"/> class. /// </summary> /// <param name="diagnostics">The object that provides access to the system wide diagnostics.</param> /// <param name="timingStorage">The object that generates reports for a stored interval.</param> /// <param name="collection">The object that stores the generated reports.</param> /// <param name="reportTransformer">The function which transforms a report into a string.</param> /// <param name="description">The description for the current interval.</param> public TimingIntervalHelper( SystemDiagnostics diagnostics, IGenerateTimingReports timingStorage, TimingReportCollection collection, Func <TimingReport, string> reportTransformer, string description) { { Debug.Assert(diagnostics != null, "The diagnostics object should not be a null reference."); Debug.Assert(timingStorage != null, "The storage object should not be a null reference."); Debug.Assert(collection != null, "The collection object should not be a null reference."); Debug.Assert(reportTransformer != null, "The report transformer object should not be a null reference."); Debug.Assert(!string.IsNullOrWhiteSpace(description), "The description string should not be a null reference or an empty string."); } m_Storage = timingStorage; m_Collection = collection; m_Transformer = reportTransformer; m_Interval = (ITimerInterval)diagnostics.Profiler.Measure(BaseConstants.TimingGroup, description); }
public void TraversePreOrderWithSingleRoot() { var owner = new Mock <IIntervalOwner>(); { owner.Setup(o => o.CurrentTicks) .Returns(10L); } var group = new TimingGroup(); var parents = new ITimerInterval[] { new TimerInterval(owner.Object, group, "a"), }; var children = new[] { new ITimerInterval[] { new TimerInterval(owner.Object, group, "aa"), new TimerInterval(owner.Object, group, "ab"), new TimerInterval(owner.Object, group, "ac"), }, }; var tree = BuildTree(parents, children); var list = new List <Tuple <ITimerInterval, int> >(); tree.TraversePreOrder((node, level) => list.Add(new Tuple <ITimerInterval, int>(node, level))); Assert.AreEqual(parents[0], list[0].Item1); Assert.AreEqual(0, list[0].Item2); Assert.AreEqual(children[0][0], list[1].Item1); Assert.AreEqual(1, list[1].Item2); Assert.AreEqual(children[0][1], list[2].Item1); Assert.AreEqual(1, list[2].Item2); Assert.AreEqual(children[0][2], list[3].Item1); Assert.AreEqual(1, list[3].Item2); CleanUpIntervals(parents, children); }
public void SubscribeInterval() { using var timeProvider = TestTimeProvider.SetDefault(new DateTime(2020, 03, 15, 17, 0, 0)); var timerService = new TimerService(); int intervalsCalled = 0; ITimerInterval timerInterval = timerService.SubscribeInterval(TimeSpan.FromHours(1), (ITimerService sender, ITimerInterval interval) => { Assert.AreEqual(timerService, sender); intervalsCalled++; if (intervalsCalled > 1) { interval.Unsubscribe(); } }); timerService.Check(); Assert.AreEqual(0, intervalsCalled); timeProvider.Now += TimeSpan.FromMinutes(31); timerService.Check(); Assert.AreEqual(0, intervalsCalled, "1 hour interval should not have been called after 31 minutes"); timeProvider.Now += TimeSpan.FromMinutes(31); timerService.Check(); Assert.AreEqual(1, intervalsCalled, "1 hour interval should have been called after 62 minutes"); timeProvider.Now += TimeSpan.FromMinutes(60); timerService.Check(); Assert.AreEqual(2, intervalsCalled, "should have been called again after 60 minutes"); timeProvider.Now += TimeSpan.FromMinutes(60); timerService.Check(); Assert.AreEqual(2, intervalsCalled, "should not have been called after unsubscribe"); }
public void TraversePreOrderWithMultipleGroups() { var owner = new Mock <IIntervalOwner>(); { owner.Setup(o => o.CurrentTicks) .Returns(10L); } var firstGroup = new TimingGroup(); var secondGroup = new TimingGroup(); var thirdGroup = new TimingGroup(); var parents = new ITimerInterval[] { new TimerInterval(owner.Object, firstGroup, "a"), new TimerInterval(owner.Object, secondGroup, "b"), new TimerInterval(owner.Object, thirdGroup, "c"), }; var children = new[] { new ITimerInterval[] { new TimerInterval(owner.Object, firstGroup, "aa"), new TimerInterval(owner.Object, firstGroup, "ab"), new TimerInterval(owner.Object, firstGroup, "ac"), }, new ITimerInterval[] { new TimerInterval(owner.Object, secondGroup, "ba"), new TimerInterval(owner.Object, secondGroup, "bb"), new TimerInterval(owner.Object, secondGroup, "bc"), }, new ITimerInterval[] { new TimerInterval(owner.Object, thirdGroup, "ca"), new TimerInterval(owner.Object, thirdGroup, "cb"), new TimerInterval(owner.Object, thirdGroup, "cc"), }, }; var tree = BuildTree(parents, children); var firstList = new List <Tuple <ITimerInterval, int> >(); tree.TraversePreOrder(firstGroup, (node, level) => firstList.Add(new Tuple <ITimerInterval, int>(node, level))); Assert.AreEqual(parents[0], firstList[0].Item1); Assert.AreEqual(0, firstList[0].Item2); Assert.AreEqual(children[0][0], firstList[1].Item1); Assert.AreEqual(1, firstList[1].Item2); Assert.AreEqual(children[0][1], firstList[2].Item1); Assert.AreEqual(1, firstList[2].Item2); Assert.AreEqual(children[0][2], firstList[3].Item1); Assert.AreEqual(1, firstList[3].Item2); var secondList = new List <Tuple <ITimerInterval, int> >(); tree.TraversePreOrder(secondGroup, (node, level) => secondList.Add(new Tuple <ITimerInterval, int>(node, level))); Assert.AreEqual(parents[1], secondList[0].Item1); Assert.AreEqual(0, secondList[0].Item2); Assert.AreEqual(children[1][0], secondList[1].Item1); Assert.AreEqual(1, secondList[1].Item2); Assert.AreEqual(children[1][1], secondList[2].Item1); Assert.AreEqual(1, secondList[2].Item2); Assert.AreEqual(children[1][2], secondList[3].Item1); Assert.AreEqual(1, secondList[3].Item2); var thirdList = new List <Tuple <ITimerInterval, int> >(); tree.TraversePreOrder(thirdGroup, (node, level) => thirdList.Add(new Tuple <ITimerInterval, int>(node, level))); Assert.AreEqual(parents[2], thirdList[0].Item1); Assert.AreEqual(0, thirdList[0].Item2); Assert.AreEqual(children[2][0], thirdList[1].Item1); Assert.AreEqual(1, thirdList[1].Item2); Assert.AreEqual(children[2][1], thirdList[2].Item1); Assert.AreEqual(1, thirdList[2].Item2); Assert.AreEqual(children[2][2], thirdList[3].Item1); Assert.AreEqual(1, thirdList[3].Item2); CleanUpIntervals(parents, children); }