public void TestDbTiming() { var stepId = Guid.NewGuid(); ProfilingSession.ProfilingSessionContainer.CurrentSessionStepId = stepId; var profilerDurationMilliseconds = 10; var mockProfiler = new Mock <IProfiler>(); mockProfiler.Setup(profiler => profiler.Elapsed).Returns(() => TimeSpan.FromMilliseconds(profilerDurationMilliseconds++)); var executeType = DbExecuteType.Reader; var commandString = "test sql"; var mockParameters = new Mock <IDataParameterCollection>(); mockParameters.Setup(p => p.GetEnumerator()).Returns(new IDataParameter[0].GetEnumerator()); var mockCommand = new Mock <IDbCommand>(); mockCommand.Setup(cmd => cmd.CommandText).Returns(commandString); mockCommand.Setup(cmd => cmd.Parameters).Returns(mockParameters.Object); var target = new DbTiming(mockProfiler.Object, executeType, mockCommand.Object); target.FirstFetch(); var profilerAddCustomTimingCalled = false; var mockSession = new Mock <ITimingSession>(); mockProfiler.Setup(p => p.GetTimingSession()).Returns(mockSession.Object); mockSession.Setup(s => s.AddTiming(It.IsAny <ITiming>())) .Callback <ITiming>(a => { Assert.AreEqual(target, a); profilerAddCustomTimingCalled = true; }); target.Stop(); Assert.AreNotEqual(default(Guid), target.Id); Assert.AreEqual(stepId, target.ParentId); Assert.AreEqual(executeType.ToString().ToLowerInvariant(), target.Data["executeType"]); Assert.AreEqual(commandString, target.Name); Assert.AreEqual(10, target.StartMilliseconds); Assert.AreEqual("2", target.Data["readStart"]); Assert.AreEqual(3, target.DurationMilliseconds); Assert.IsTrue(profilerAddCustomTimingCalled); // when firstFetchDurationMilliseconds is not set and stoppped is called, // the value of firstFetchDurationMilliseconds should be copied from durationmilliseconds string temp; target.Data.TryRemove("readStart", out temp); target.Stop(); Assert.AreEqual(target.DurationMilliseconds.ToString(), target.Data["readStart"]); }
public void TestProfiler() { var resultSaved = false; var name = "test"; var stepName = "step1"; var tag = "tag1"; var mockStorage = new Mock <IProfilingStorage>(); var target = new Profiler(name, mockStorage.Object, new TagCollection(new[] { tag })) as IProfiler; Assert.AreNotEqual(default(Guid), target.Id); Assert.IsTrue(target.GetTimingSession().Started.AddMinutes(1) > DateTime.UtcNow); Thread.Sleep(100); Assert.IsTrue(target.GetTimingSession().DurationMilliseconds > 0); Assert.AreEqual(1, target.GetTimingSession().Timings.Count()); Assert.AreEqual(name, target.GetTimingSession().Name); Assert.AreEqual("root", target.GetTimingSession().Timings.First().Name); Assert.AreEqual(tag, target.GetTimingSession().Tags.First()); Assert.AreEqual(0, target.GetTimingSession().Timings.Count(t => t.Type != "step")); var mockCommand = new Mock <IDbCommand>(); mockCommand.Setup(cmd => cmd.CommandText).Returns("test"); var dbTiming = new DbTiming( target, DbExecuteType.Reader, mockCommand.Object); using (target.Step(stepName, null)) { target.GetTimingSession().AddTiming(dbTiming); } Assert.AreEqual(2, target.GetTimingSession().Timings.Count(t => t.Type == "step")); Assert.AreEqual(stepName, target.GetTimingSession().Timings.Last(t => t.Type == "step").Name); Assert.AreEqual(dbTiming, target.GetTimingSession().Timings.First(t => t.Type != "step")); using (target.Ignore()) { } Assert.AreEqual(2, target.GetTimingSession().Timings.Count(t => t.Type == "step")); using (var step = target.Step(stepName, null)) { step.Discard(); } Assert.AreEqual(2, target.GetTimingSession().Timings.Count(t => t.Type == "step")); mockStorage.Setup(storage => storage.SaveSession(target.GetTimingSession())).Callback <ITimingSession>(a => { resultSaved = true; }); target.Stop(); Assert.IsTrue(resultSaved); }