/// <summary> /// Executes & profiles the execution of the specified <see cref="IDbCommand"/>. /// </summary> /// <param name="executeType">The <see cref="DbExecuteType"/>.</param> /// <param name="command">The <see cref="IDbCommand"/> to be executed & profiled.</param> /// <param name="execute"> /// The execute handler /// </param> /// <param name="tags">The tags of the <see cref="DbTiming"/> which will be created internally.</param> public virtual async Task <object> ExecuteCommandAsync(DbExecuteType executeType, IDbCommand command, Func <Task <object> > execute, TagCollection tags) { if (command == null) { return(execute()); } var dbTiming = new DbTiming(_profiler, executeType, command) { Tags = tags }; var result = await execute(); var dataReader = result as DbDataReader; if (dataReader == null) { // if not executing reader, stop the sql timing right after execute() dbTiming.Stop(); return(result); } dbTiming.FirstFetch(); var reader = dataReader as ProfiledDbDataReader ?? new ProfiledDbDataReader(dataReader, this); _inProgressDataReaders[reader] = dbTiming; return(reader); }
/// <summary> /// Executes & profiles the execution of the specified <see cref="IDbCommand"/>. /// </summary> /// <param name="executeType">The <see cref="DbExecuteType"/>.</param> /// <param name="command">The <see cref="IDbCommand"/> to be executed & profiled.</param> /// <param name="execute"> /// The execute handler, /// which should return the <see cref="IDataReader"/> instance if it is an ExecuteReader operation. /// If it is not ExecuteReader, it should return null. /// </param> /// <param name="tags">The tags of the <see cref="DbTiming"/> which will be created internally.</param> public virtual void ExecuteDbCommand(DbExecuteType executeType, IDbCommand command, Func <IDataReader> execute, TagCollection tags) { if (execute == null) { return; } if (command == null) { execute(); return; } var dbTiming = new DbTiming(_profiler, executeType, command) { Tags = tags }; var dataReader = execute(); if (dataReader == null) { // if not executing reader, stop the sql timing right after execute() dbTiming.Stop(); return; } dbTiming.FirstFetch(); var reader = dataReader as ProfiledDbDataReader ?? new ProfiledDbDataReader(dataReader, this); _inProgressDataReaders[reader] = dbTiming; }
/// <summary> /// Executes & profiles the execution of the specified <see cref="IDbCommand"/>. /// </summary> /// <param name="executeType">The <see cref="DbExecuteType"/>.</param> /// <param name="command">The <see cref="IDbCommand"/> to be executed & profiled.</param> /// <param name="execute"> /// The execute handler, /// which should return the <see cref="DbDataReader"/> instance if it is an ExecuteReader operation. /// </param> /// <param name="tags">The tags of the <see cref="DbTiming"/> which will be created internally.</param> public virtual Task <DbDataReader> ExecuteDbDataReaderCommandAsync(DbExecuteType executeType, IDbCommand command, Func <Task <DbDataReader> > execute, TagCollection tags) { if (command == null) { return(execute()); } var dbTiming = new DbTiming(_profiler, executeType, command) { Tags = tags }; return(execute().ContinueWith <DbDataReader>(r => { var dataReader = r.Result; if (dataReader == null) { // if not executing reader, stop the sql timing right after execute() dbTiming.Stop(); return null; } dbTiming.FirstFetch(); var reader = dataReader as ProfiledDbDataReader ?? new ProfiledDbDataReader(dataReader, this); _inProgressDataReaders[reader] = dbTiming; return reader; })); }
public void TestProfiler() { var resultSaved = false; var name = "test"; var stepName = "step1"; var mockStorage = new Mock<IProfilingStorage>(); var target = new Profiler(name, mockStorage.Object, new[] { "test" }) as IProfiler; Assert.AreNotEqual(default(Guid), target.Id); Assert.IsTrue(target.Started.AddMinutes(1) > DateTime.UtcNow); Thread.Sleep(100); Assert.IsTrue(target.DurationMilliseconds > 0); Assert.AreEqual(1, target.StepTimings.Count()); Assert.AreEqual(name, target.Name); Assert.AreEqual("root", target.StepTimings.First().Name); Assert.AreEqual("TEST", target.Tags.First()); Assert.AreEqual(0, target.CustomTimings.Count()); 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, null)) { target.AddCustomTiming(dbTiming); } Assert.AreEqual(2, target.StepTimings.Count()); Assert.AreEqual(stepName, target.StepTimings.Last().Name); Assert.AreEqual(dbTiming, target.CustomTimings.First()); using (target.Ignore()) { } Assert.AreEqual(2, target.StepTimings.Count()); using (var step = target.Step(stepName, null, null)) { step.Discard(); } Assert.AreEqual(2, target.StepTimings.Count()); mockStorage.Setup(storage => storage.SaveResult(target)).Callback<IProfiler>(a => { resultSaved = true; }); target.Stop(); Assert.IsTrue(resultSaved); }
public void TestDbTiming() { var stepId = Guid.NewGuid(); ProfilingSession.ProfilingSessionContainer.CurrentSessionStepId = stepId; var profilerDurationMilliseconds = 10; var mockProfiler = new Mock<IProfiler>(); mockProfiler.Setup(profiler => profiler.DurationMilliseconds).Returns(() => 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; mockProfiler.Setup(profiler => profiler.AddCustomTiming(It.IsAny<DbTiming>())) .Callback<CustomTiming>(a => { Assert.AreEqual(target, a); profilerAddCustomTimingCalled = true; }); target.Stop(); Assert.AreNotEqual(default(Guid), target.Id); Assert.AreEqual(stepId, target.ParentId); Assert.AreEqual(executeType, target.DbExecuteType); Assert.AreEqual(commandString, target.Name); Assert.IsTrue(target.Parameters != null); Assert.AreEqual(10, target.StartMilliseconds); Assert.AreEqual(1, target.OutputStartMilliseconds); Assert.AreEqual(2, target.DurationMilliseconds); Assert.IsTrue(profilerAddCustomTimingCalled); // when firstFetchDurationMilliseconds is not set and stoppped is called, // the value of firstFetchDurationMilliseconds should be copied from durationmilliseconds target.OutputStartMilliseconds = null; target.Stop(); Assert.AreEqual(target.DurationMilliseconds, target.OutputStartMilliseconds); }
/// <summary> /// Executes & profiles the execution of the specified <see cref="IDbCommand"/>. /// </summary> /// <param name="executeType">The <see cref="DbExecuteType"/>.</param> /// <param name="command">The <see cref="IDbCommand"/> to be executed & profiled.</param> /// <param name="execute"> /// The execute handler, /// which should return the number of affected rows if it is an ExecuteNonQuery operation. /// </param> /// <param name="tags">The tags of the <see cref="DbTiming"/> which will be created internally.</param> public virtual Task <int> ExecuteNonQueryCommandAsync(DbExecuteType executeType, IDbCommand command, Func <Task <int> > execute, TagCollection tags) { if (command == null) { return(execute()); } var dbTiming = new DbTiming(_profiler, executeType, command) { Tags = tags }; return(execute().ContinueWith(r => { dbTiming.Stop(); return r.Result; })); }
/// <summary> /// Executes & profiles the execution of the specified <see cref="IDbCommand"/>. /// </summary> /// <param name="executeType">The <see cref="DbExecuteType"/>.</param> /// <param name="command">The <see cref="IDbCommand"/> to be executed & profiled.</param> /// <param name="execute"> /// The execute handler, /// which should return the <see cref="IDataReader"/> instance if it is an ExecuteReader operation. /// If it is not ExecuteReader, it should return null. /// </param> /// <param name="tags">The tags of the <see cref="DbTiming"/> which will be created internally.</param> public virtual void ExecuteDbCommand(DbExecuteType executeType, IDbCommand command, Func<IDataReader> execute, IEnumerable<string> tags) { if (execute == null) { return; } if (command == null) { execute(); return; } var dbTiming = new DbTiming(_profiler, executeType, command); if (tags != null && tags.Any()) { dbTiming.Tags = new TagCollection(tags); } var dataReader = execute(); if (dataReader == null) { // if not executing reader, stop the sql timing right after execute() dbTiming.Stop(); return; } dbTiming.FirstFetch(); var reader = dataReader as ProfiledDbDataReader ?? new ProfiledDbDataReader(dataReader, this); _inProgressDataReaders[reader] = dbTiming; }
public void TestJsonProfilingStorage() { slf4net.LoggerFactory.SetFactoryResolver(new SimpleFactoryResolver(item => Console.WriteLine(item.Message))); var target = new JsonProfilingStorage(ProfilingStorageBase.Inline); // save empty result should not throw exception target.SaveResult(null); var name = "test"; var stepName = "step1"; var profiler = new Profiler(name, target, new [] { "test", "test2" }) as IProfiler; var mockParameter = new Mock<IDataParameter>(); mockParameter.Setup(p => p.ParameterName).Returns("p1"); mockParameter.Setup(p => p.Value).Returns(1); mockParameter.Setup(p => p.Direction).Returns(ParameterDirection.Input); var mockParameterDBNull = new Mock<IDbDataParameter>(); mockParameterDBNull.Setup(p => p.DbType).Returns(DbType.Binary); mockParameterDBNull.Setup(p => p.Value).Returns(DBNull.Value); mockParameterDBNull.Setup(p => p.Direction).Returns(ParameterDirection.Input); var mockParameterBinary = new Mock<IDataParameter>(); mockParameterBinary.Setup(p => p.DbType).Returns(DbType.Binary); mockParameterBinary.Setup(p => p.Value).Returns(new byte[100]); mockParameterBinary.Setup(p => p.Direction).Returns(ParameterDirection.Input); var mockParameterBinaryTooBig = new Mock<IDbDataParameter>(); mockParameterBinaryTooBig.Setup(p => p.DbType).Returns(DbType.Binary); mockParameterBinaryTooBig.Setup(p => p.Value).Returns(new byte[0x200 + 1]); mockParameterBinaryTooBig.Setup(p => p.Direction).Returns(ParameterDirection.Input); var mockParameterDateTime = new Mock<IDbDataParameter>(); mockParameterDateTime.Setup(p => p.DbType).Returns(DbType.DateTime); mockParameterDateTime.Setup(p => p.Value).Returns(DateTime.Now); mockParameterDateTime.Setup(p => p.Direction).Returns(ParameterDirection.Input); var mockParameterEnum = new Mock<IDbDataParameter>(); mockParameterEnum.Setup(p => p.DbType).Returns(DbType.Int32); mockParameterEnum.Setup(p => p.Value).Returns(DbType.Boolean); mockParameterEnum.Setup(p => p.Direction).Returns(ParameterDirection.Input); var mockParameterXml = new Mock<IDbDataParameter>(); mockParameterXml.Setup(p => p.DbType).Returns(DbType.Xml); mockParameterXml.Setup(p => p.Value).Returns("<xml />"); mockParameterXml.Setup(p => p.Direction).Returns(ParameterDirection.Input); var mockParameterCollection = new Mock<IDataParameterCollection>(); mockParameterCollection.Setup(collections => collections.GetEnumerator()).Returns(new IDataParameter[] { mockParameter.Object, mockParameterDBNull.Object, mockParameterBinary.Object, mockParameterBinaryTooBig.Object, mockParameterDateTime.Object, mockParameterEnum.Object, mockParameterXml.Object }.GetEnumerator()); mockParameterCollection.Setup(collection => collection.Count).Returns(1); var mockCommand = new Mock<IDbCommand>(); mockCommand.Setup(cmd => cmd.CommandText).Returns("test sql"); mockCommand.Setup(cmd => cmd.Parameters).Returns(mockParameterCollection.Object); var dbTiming = new DbTiming( profiler, DbExecuteType.Reader, mockCommand.Object); using (profiler.Step(stepName, null, null)) { profiler.AddCustomTiming(dbTiming); profiler.AddCustomTiming(new CustomTiming(profiler, "custom", "custom")); } profiler.Stop(); // save normal result should not throw exception target.SaveResult(profiler); slf4net.LoggerFactory.Reset(); }