From: <aye...@us...> - 2009-06-28 13:22:26
|
Revision: 4538 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4538&view=rev Author: ayenderahien Date: 2009-06-28 12:19:32 +0000 (Sun, 28 Jun 2009) Log Message: ----------- More fixes for NH 1850 Will now also report timing for batched statements execution Modified Paths: -------------- branches/2.1.x/nhibernate/src/NHibernate/AdoNet/AbstractBatcher.cs branches/2.1.x/nhibernate/src/NHibernate/AdoNet/NonBatchingBatcher.cs branches/2.1.x/nhibernate/src/NHibernate/AdoNet/OracleDataClientBatchingBatcher.cs branches/2.1.x/nhibernate/src/NHibernate/AdoNet/SqlClientBatchingBatcher.cs branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1850/Fixture.cs Modified: branches/2.1.x/nhibernate/src/NHibernate/AdoNet/AbstractBatcher.cs =================================================================== --- branches/2.1.x/nhibernate/src/NHibernate/AdoNet/AbstractBatcher.cs 2009-06-27 14:50:53 UTC (rev 4537) +++ branches/2.1.x/nhibernate/src/NHibernate/AdoNet/AbstractBatcher.cs 2009-06-28 12:19:32 UTC (rev 4538) @@ -397,7 +397,7 @@ InvalidateBatchCommand(); try { - DoExecuteBatch(ps); + ExecuteBatchWithTiming(ps); } finally { @@ -406,12 +406,27 @@ } } + protected void ExecuteBatchWithTiming(IDbCommand ps) + { + Stopwatch duration = null; + if (log.IsDebugEnabled) + duration = Stopwatch.StartNew(); + var countBeforeExecutingBatch = CountOfStatementsInCurrentBatch; + DoExecuteBatch(ps); + if (log.IsDebugEnabled && duration != null) + log.DebugFormat("ExecuteBatch for {0} statements took {1} ms", + countBeforeExecutingBatch, + duration.ElapsedMilliseconds); + } + /// <summary> /// /// </summary> /// <param name="ps"></param> protected abstract void DoExecuteBatch(IDbCommand ps); + protected abstract int CountOfStatementsInCurrentBatch { get; } + /// <summary> /// Gets or sets the size of the batch, this can change dynamically by /// calling the session's SetBatchSize. Modified: branches/2.1.x/nhibernate/src/NHibernate/AdoNet/NonBatchingBatcher.cs =================================================================== --- branches/2.1.x/nhibernate/src/NHibernate/AdoNet/NonBatchingBatcher.cs 2009-06-27 14:50:53 UTC (rev 4537) +++ branches/2.1.x/nhibernate/src/NHibernate/AdoNet/NonBatchingBatcher.cs 2009-06-28 12:19:32 UTC (rev 4538) @@ -9,7 +9,7 @@ /// An implementation of the <see cref="IBatcher" /> /// interface that does no batching. /// </summary> - internal class NonBatchingBatcher : AbstractBatcher + public class NonBatchingBatcher : AbstractBatcher { /// <summary> /// Initializes a new instance of the <see cref="NonBatchingBatcher"/> class. @@ -50,7 +50,12 @@ { } + protected override int CountOfStatementsInCurrentBatch + { + get { return 1; } + } + public override int BatchSize { get { return 1; } Modified: branches/2.1.x/nhibernate/src/NHibernate/AdoNet/OracleDataClientBatchingBatcher.cs =================================================================== --- branches/2.1.x/nhibernate/src/NHibernate/AdoNet/OracleDataClientBatchingBatcher.cs 2009-06-27 14:50:53 UTC (rev 4537) +++ branches/2.1.x/nhibernate/src/NHibernate/AdoNet/OracleDataClientBatchingBatcher.cs 2009-06-28 12:19:32 UTC (rev 4538) @@ -72,7 +72,7 @@ if (countOfCommands >= batchSize) { - DoExecuteBatch(currentBatch); + ExecuteBatchWithTiming(currentBatch); } } @@ -108,6 +108,11 @@ } } + protected override int CountOfStatementsInCurrentBatch + { + get { return countOfCommands; } + } + private void SetObjectParam(Object obj, string paramName, object paramValue) { System.Type objType = obj.GetType(); Modified: branches/2.1.x/nhibernate/src/NHibernate/AdoNet/SqlClientBatchingBatcher.cs =================================================================== --- branches/2.1.x/nhibernate/src/NHibernate/AdoNet/SqlClientBatchingBatcher.cs 2009-06-27 14:50:53 UTC (rev 4537) +++ branches/2.1.x/nhibernate/src/NHibernate/AdoNet/SqlClientBatchingBatcher.cs 2009-06-28 12:19:32 UTC (rev 4538) @@ -4,10 +4,12 @@ namespace NHibernate.AdoNet { + using System; + /// <summary> /// Summary description for SqlClientBatchingBatcher. /// </summary> - internal class SqlClientBatchingBatcher : AbstractBatcher + public class SqlClientBatchingBatcher : AbstractBatcher { private int batchSize; private int totalExpectedRowsAffected; @@ -32,6 +34,11 @@ set { batchSize = value; } } + protected override int CountOfStatementsInCurrentBatch + { + get { return currentBatch.CountOfCommands; } + } + public override void AddToBatch(IExpectation expectation) { totalExpectedRowsAffected += expectation.ExpectedRowCount; @@ -39,7 +46,7 @@ string lineWithParameters = null; var sqlStatementLogger = Factory.Settings.SqlStatementLogger; - if (sqlStatementLogger.IsDebugEnabled) + if (sqlStatementLogger.IsDebugEnabled || log.IsDebugEnabled) { lineWithParameters = sqlStatementLogger.GetCommandLineWithParameters(batchUpdate); var formatStyle = sqlStatementLogger.DetermineActualStyle(FormatStyle.Basic); @@ -57,13 +64,13 @@ if (currentBatch.CountOfCommands >= batchSize) { - DoExecuteBatch(batchUpdate); + ExecuteBatchWithTiming(batchUpdate); } } protected override void DoExecuteBatch(IDbCommand ps) { - log.Debug("Executing batch"); + log.DebugFormat("Executing batch"); CheckReaders(); Prepare(currentBatch.BatchCommand); if (Factory.Settings.SqlStatementLogger.IsDebugEnabled) Modified: branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1850/Fixture.cs =================================================================== --- branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1850/Fixture.cs 2009-06-27 14:50:53 UTC (rev 4537) +++ branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1850/Fixture.cs 2009-06-28 12:19:32 UTC (rev 4538) @@ -1,16 +1,19 @@ -using System; -using System.Collections.Generic; -using System.Text; -using NHibernate.Criterion; -using NUnit.Framework; +using NUnit.Framework; namespace NHibernate.Test.NHSpecificTest.NH1850 { + using System; using AdoNet; + using Environment=NHibernate.Cfg.Environment; [TestFixture] public class Fixture:BugTestCase { + protected override void Configure(NHibernate.Cfg.Configuration configuration) + { + configuration.SetProperty(Environment.BatchSize, "1"); + } + [Test] public void CanGetQueryDurationForDelete() { @@ -30,6 +33,38 @@ } [Test] + public void CanGetQueryDurationForBatch() + { + using (LogSpy spy = new LogSpy(typeof(AbstractBatcher))) + using (ISession session = OpenSession()) + using (ITransaction tx = session.BeginTransaction()) + { + for (int i = 0; i < 3; i++) + { + var customer = new Customer + { + Name = "foo" + }; + session.Save(customer); + session.Delete(customer); + } + session.Flush(); + + var wholeLog = spy.GetWholeLog(); + var lines = wholeLog.Split(new[]{System.Environment.NewLine},StringSplitOptions.RemoveEmptyEntries); + int batches = 0; + foreach (var line in lines) + { + if (line.Contains("ExecuteBatch for 1 statements took ")) + batches += 1; + } + Assert.AreEqual(3, batches); + + tx.Rollback(); + } + } + + [Test] public void CanGetQueryDurationForSelect() { using (LogSpy spy = new LogSpy(typeof(AbstractBatcher))) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |