Skip to content

Commit e0fe87b

Browse files
committed
Create Stopwatch only if stats is enabled
1 parent 27ea900 commit e0fe87b

18 files changed

+94
-126
lines changed

src/NHibernate/Async/Engine/Loading/CollectionLoadContext.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,10 @@ private async Task EndLoadingCollectionAsync(
186186
var persistenceContext = LoadContext.PersistenceContext;
187187
var session = persistenceContext.Session;
188188

189-
bool statsEnabled = session.Factory.Statistics.IsStatisticsEnabled;
190-
var stopWath = new Stopwatch();
191-
if (statsEnabled)
189+
Stopwatch stopWatch = null;
190+
if (session.Factory.Statistics.IsStatisticsEnabled)
192191
{
193-
stopWath.Start();
192+
stopWatch = Stopwatch.StartNew();
194193
}
195194

196195
bool hasNoQueuedOperations = lce.Collection.EndRead(persister); // warning: can cause a recursive calls! (proxy initialization)
@@ -228,10 +227,10 @@ private async Task EndLoadingCollectionAsync(
228227
log.Debug("collection fully initialized: {0}", MessageHelper.CollectionInfoString(persister, lce.Collection, lce.Key, session));
229228
}
230229

231-
if (statsEnabled)
230+
if (stopWatch != null)
232231
{
233-
stopWath.Stop();
234-
session.Factory.StatisticsImplementor.LoadCollection(persister.Role, stopWath.Elapsed);
232+
stopWatch.Stop();
233+
session.Factory.StatisticsImplementor.LoadCollection(persister.Role, stopWatch.Elapsed);
235234
}
236235
}
237236

src/NHibernate/Async/Engine/TwoPhaseLoad.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,10 @@ internal static async Task InitializeEntityAsync(object entity, bool readOnly, I
5656
cancellationToken.ThrowIfCancellationRequested();
5757
//TODO: Should this be an InitializeEntityEventListener??? (watch out for performance!)
5858

59-
bool statsEnabled = session.Factory.Statistics.IsStatisticsEnabled;
60-
var stopWath = new Stopwatch();
61-
if (statsEnabled)
59+
Stopwatch stopWatch = null;
60+
if (session.Factory.Statistics.IsStatisticsEnabled)
6261
{
63-
stopWath.Start();
62+
stopWatch = Stopwatch.StartNew();
6463
}
6564

6665
IPersistenceContext persistenceContext = session.PersistenceContext;
@@ -201,10 +200,10 @@ internal static async Task InitializeEntityAsync(object entity, bool readOnly, I
201200
if (log.IsDebugEnabled())
202201
log.Debug("done materializing entity {0}", MessageHelper.InfoString(persister, id, session.Factory));
203202

204-
if (statsEnabled)
203+
if (stopWatch != null)
205204
{
206-
stopWath.Stop();
207-
factory.StatisticsImplementor.LoadEntity(persister.EntityName, stopWath.Elapsed);
205+
stopWatch.Stop();
206+
factory.StatisticsImplementor.LoadEntity(persister.EntityName, stopWatch.Elapsed);
208207
}
209208
}
210209
}

src/NHibernate/Async/Event/Default/DefaultInitializeCollectionEventListener.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ public virtual async Task OnInitializeCollectionAsync(InitializeCollectionEvent
3333
IPersistentCollection collection = @event.Collection;
3434
ISessionImplementor source = @event.Session;
3535

36-
bool statsEnabled = source.Factory.Statistics.IsStatisticsEnabled;
37-
var stopWath = new Stopwatch();
38-
if (statsEnabled)
36+
Stopwatch stopWatch = null;
37+
if (source.Factory.Statistics.IsStatisticsEnabled)
3938
{
40-
stopWath.Start();
39+
stopWatch = Stopwatch.StartNew();
4140
}
4241

4342
CollectionEntry ce = source.PersistenceContext.GetCollectionEntry(collection);
@@ -63,10 +62,10 @@ public virtual async Task OnInitializeCollectionAsync(InitializeCollectionEvent
6362
await (ce.LoadedPersister.InitializeAsync(ce.LoadedKey, source, cancellationToken)).ConfigureAwait(false);
6463
log.Debug("collection initialized");
6564

66-
if (statsEnabled)
65+
if (stopWatch != null)
6766
{
68-
stopWath.Stop();
69-
source.Factory.StatisticsImplementor.FetchCollection(ce.LoadedPersister.Role, stopWath.Elapsed);
67+
stopWatch.Stop();
68+
source.Factory.StatisticsImplementor.FetchCollection(ce.LoadedPersister.Role, stopWatch.Elapsed);
7069
}
7170
}
7271
}

src/NHibernate/Async/Event/Default/DefaultLoadEventListener.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -329,19 +329,18 @@ protected virtual async Task<object> LoadFromDatasourceAsync(LoadEvent @event, I
329329
cancellationToken.ThrowIfCancellationRequested();
330330
ISessionImplementor source = @event.Session;
331331

332-
bool statsEnabled = source.Factory.Statistics.IsStatisticsEnabled;
333-
var stopWath = new Stopwatch();
334-
if (statsEnabled)
332+
Stopwatch stopWatch = null;
333+
if (source.Factory.Statistics.IsStatisticsEnabled)
335334
{
336-
stopWath.Start();
335+
stopWatch = Stopwatch.StartNew();
337336
}
338337

339338
object entity = await (persister.LoadAsync(@event.EntityId, @event.InstanceToLoad, @event.LockMode, source, cancellationToken)).ConfigureAwait(false);
340339

341-
if (@event.IsAssociationFetch && statsEnabled)
340+
if (stopWatch != null && @event.IsAssociationFetch)
342341
{
343-
stopWath.Stop();
344-
source.Factory.StatisticsImplementor.FetchEntity(@event.EntityClassName, stopWath.Elapsed);
342+
stopWatch.Stop();
343+
source.Factory.StatisticsImplementor.FetchEntity(@event.EntityClassName, stopWatch.Elapsed);
345344
}
346345

347346
return entity;

src/NHibernate/Async/Impl/MultiCriteriaImpl.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,10 @@ private async Task<IList> DoListAsync(CancellationToken cancellationToken)
145145
private async Task GetResultsFromDatabaseAsync(IList results, CancellationToken cancellationToken)
146146
{
147147
cancellationToken.ThrowIfCancellationRequested();
148-
bool statsEnabled = session.Factory.Statistics.IsStatisticsEnabled;
149-
var stopWatch = new Stopwatch();
150-
if (statsEnabled)
148+
Stopwatch stopWatch = null;
149+
if (session.Factory.Statistics.IsStatisticsEnabled)
151150
{
152-
stopWatch.Start();
151+
stopWatch = Stopwatch.StartNew();
153152
}
154153
int rowCount = 0;
155154
var cacheBatcher = new CacheBatcher(session);
@@ -219,7 +218,7 @@ private async Task GetResultsFromDatabaseAsync(IList results, CancellationToken
219218
log.Error(sqle, "Failed to execute multi criteria: [{0}]", resultSetsCommand.Sql);
220219
throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, sqle, "Failed to execute multi criteria", resultSetsCommand.Sql);
221220
}
222-
if (statsEnabled)
221+
if (stopWatch != null)
223222
{
224223
stopWatch.Stop();
225224
session.Factory.StatisticsImplementor.QueryExecuted(string.Format("{0} queries (MultiCriteria)", loaders.Count), rowCount, stopWatch.Elapsed);

src/NHibernate/Async/Impl/MultiQueryImpl.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,10 @@ public partial class MultiQueryImpl : IMultiQuery
7676
protected async Task<List<object>> DoListAsync(CancellationToken cancellationToken)
7777
{
7878
cancellationToken.ThrowIfCancellationRequested();
79-
bool statsEnabled = session.Factory.Statistics.IsStatisticsEnabled;
80-
var stopWatch = new Stopwatch();
81-
if (statsEnabled)
79+
Stopwatch stopWatch = null;
80+
if (session.Factory.Statistics.IsStatisticsEnabled)
8281
{
83-
stopWatch.Start();
82+
stopWatch = Stopwatch.StartNew();
8483
}
8584
int rowCount = 0;
8685

@@ -192,7 +191,7 @@ protected async Task<List<object>> DoListAsync(CancellationToken cancellationTok
192191
throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, sqle, "Failed to execute multi query", resultSetsCommand.Sql);
193192
}
194193

195-
if (statsEnabled)
194+
if (stopWatch != null)
196195
{
197196
stopWatch.Stop();
198197
session.Factory.StatisticsImplementor.QueryExecuted(string.Format("{0} queries (MultiQuery)", translators.Count), rowCount, stopWatch.Elapsed);

src/NHibernate/Async/Loader/Hql/QueryLoader.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,10 @@ internal async Task<IEnumerable> GetEnumerableAsync(QueryParameters queryParamet
9191
{
9292
cancellationToken.ThrowIfCancellationRequested();
9393
CheckQuery(queryParameters);
94-
bool statsEnabled = session.Factory.Statistics.IsStatisticsEnabled;
95-
96-
var stopWath = new Stopwatch();
97-
if (statsEnabled)
94+
Stopwatch stopWatch = null;
95+
if (session.Factory.Statistics.IsStatisticsEnabled)
9896
{
99-
stopWath.Start();
97+
stopWatch = Stopwatch.StartNew();
10098
}
10199

102100
var cmd = await (PrepareQueryCommandAsync(queryParameters, false, session, cancellationToken)).ConfigureAwait(false);
@@ -108,13 +106,13 @@ internal async Task<IEnumerable> GetEnumerableAsync(QueryParameters queryParamet
108106
IEnumerable result =
109107
new EnumerableImpl(rs, cmd, session, queryParameters.IsReadOnly(session), _queryTranslator.ReturnTypes, _queryTranslator.GetColumnNames(), queryParameters.RowSelection, resultTransformer, _queryReturnAliases);
110108

111-
if (statsEnabled)
109+
if (stopWatch != null)
112110
{
113-
stopWath.Stop();
114-
session.Factory.StatisticsImplementor.QueryExecuted("HQL: " + _queryTranslator.QueryString, 0, stopWath.Elapsed);
111+
stopWatch.Stop();
112+
session.Factory.StatisticsImplementor.QueryExecuted("HQL: " + _queryTranslator.QueryString, 0, stopWatch.Elapsed);
115113
// NH: Different behavior (H3.2 use QueryLoader in AST parser) we need statistic for orginal query too.
116114
// probably we have a bug some where else for statistic RowCount
117-
session.Factory.StatisticsImplementor.QueryExecuted(QueryIdentifier, 0, stopWath.Elapsed);
115+
session.Factory.StatisticsImplementor.QueryExecuted(QueryIdentifier, 0, stopWatch.Elapsed);
118116
}
119117
return result;
120118
}

src/NHibernate/Async/Loader/Loader.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,11 +1452,10 @@ protected async Task<IList> DoListAsync(ISessionImplementor session, QueryParame
14521452
QueryCacheResultBuilder queryCacheResultBuilder, CancellationToken cancellationToken)
14531453
{
14541454
cancellationToken.ThrowIfCancellationRequested();
1455-
bool statsEnabled = Factory.Statistics.IsStatisticsEnabled;
1456-
var stopWatch = new Stopwatch();
1457-
if (statsEnabled)
1455+
Stopwatch stopWatch = null;
1456+
if (session.Factory.Statistics.IsStatisticsEnabled)
14581457
{
1459-
stopWatch.Start();
1458+
stopWatch = Stopwatch.StartNew();
14601459
}
14611460

14621461
IList result;
@@ -1475,7 +1474,7 @@ protected async Task<IList> DoListAsync(ISessionImplementor session, QueryParame
14751474
throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, sqle, "could not execute query", SqlString,
14761475
queryParameters.PositionalParameterValues, queryParameters.NamedParameters);
14771476
}
1478-
if (statsEnabled)
1477+
if (stopWatch != null)
14791478
{
14801479
stopWatch.Stop();
14811480
Factory.StatisticsImplementor.QueryExecuted(QueryIdentifier, result.Count, stopWatch.Elapsed);

src/NHibernate/Async/Multi/QueryBatch.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,10 @@ protected async Task ExecuteBatchedAsync(CancellationToken cancellationToken)
127127
var resultSetsCommand = Session.Factory.ConnectionProvider.Driver.GetResultSetsCommand(Session);
128128
CombineQueries(resultSetsCommand);
129129

130-
var statsEnabled = Session.Factory.Statistics.IsStatisticsEnabled;
131130
Stopwatch stopWatch = null;
132-
if (statsEnabled)
131+
if (Session.Factory.Statistics.IsStatisticsEnabled)
133132
{
134-
stopWatch = new Stopwatch();
135-
stopWatch.Start();
133+
stopWatch = Stopwatch.StartNew();
136134
}
137135

138136
if (Log.IsDebugEnabled())
@@ -184,14 +182,9 @@ protected async Task ExecuteBatchedAsync(CancellationToken cancellationToken)
184182
resultSetsCommand.Sql);
185183
}
186184

187-
if (!statsEnabled)
188-
{
189-
return;
190-
}
191-
192-
stopWatch.Stop();
193-
if (resultSetsCommand.HasQueries)
185+
if (stopWatch != null && resultSetsCommand.HasQueries)
194186
{
187+
stopWatch.Stop();
195188
Session.Factory.StatisticsImplementor.QueryExecuted(
196189
resultSetsCommand.Sql.ToString(),
197190
rowCount,

src/NHibernate/Engine/Loading/CollectionLoadContext.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,10 @@ private void EndLoadingCollection(
284284
var persistenceContext = LoadContext.PersistenceContext;
285285
var session = persistenceContext.Session;
286286

287-
bool statsEnabled = session.Factory.Statistics.IsStatisticsEnabled;
288-
var stopWath = new Stopwatch();
289-
if (statsEnabled)
287+
Stopwatch stopWatch = null;
288+
if (session.Factory.Statistics.IsStatisticsEnabled)
290289
{
291-
stopWath.Start();
290+
stopWatch = Stopwatch.StartNew();
292291
}
293292

294293
bool hasNoQueuedOperations = lce.Collection.EndRead(persister); // warning: can cause a recursive calls! (proxy initialization)
@@ -326,10 +325,10 @@ private void EndLoadingCollection(
326325
log.Debug("collection fully initialized: {0}", MessageHelper.CollectionInfoString(persister, lce.Collection, lce.Key, session));
327326
}
328327

329-
if (statsEnabled)
328+
if (stopWatch != null)
330329
{
331-
stopWath.Stop();
332-
session.Factory.StatisticsImplementor.LoadCollection(persister.Role, stopWath.Elapsed);
330+
stopWatch.Stop();
331+
session.Factory.StatisticsImplementor.LoadCollection(persister.Role, stopWatch.Elapsed);
333332
}
334333
}
335334

src/NHibernate/Engine/TwoPhaseLoad.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,10 @@ internal static void InitializeEntity(object entity, bool readOnly, ISessionImpl
8585
{
8686
//TODO: Should this be an InitializeEntityEventListener??? (watch out for performance!)
8787

88-
bool statsEnabled = session.Factory.Statistics.IsStatisticsEnabled;
89-
var stopWath = new Stopwatch();
90-
if (statsEnabled)
88+
Stopwatch stopWatch = null;
89+
if (session.Factory.Statistics.IsStatisticsEnabled)
9190
{
92-
stopWath.Start();
91+
stopWatch = Stopwatch.StartNew();
9392
}
9493

9594
IPersistenceContext persistenceContext = session.PersistenceContext;
@@ -230,10 +229,10 @@ internal static void InitializeEntity(object entity, bool readOnly, ISessionImpl
230229
if (log.IsDebugEnabled())
231230
log.Debug("done materializing entity {0}", MessageHelper.InfoString(persister, id, session.Factory));
232231

233-
if (statsEnabled)
232+
if (stopWatch != null)
234233
{
235-
stopWath.Stop();
236-
factory.StatisticsImplementor.LoadEntity(persister.EntityName, stopWath.Elapsed);
234+
stopWatch.Stop();
235+
factory.StatisticsImplementor.LoadEntity(persister.EntityName, stopWatch.Elapsed);
237236
}
238237
}
239238

src/NHibernate/Event/Default/DefaultInitializeCollectionEventListener.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ public virtual void OnInitializeCollection(InitializeCollectionEvent @event)
2222
IPersistentCollection collection = @event.Collection;
2323
ISessionImplementor source = @event.Session;
2424

25-
bool statsEnabled = source.Factory.Statistics.IsStatisticsEnabled;
26-
var stopWath = new Stopwatch();
27-
if (statsEnabled)
25+
Stopwatch stopWatch = null;
26+
if (source.Factory.Statistics.IsStatisticsEnabled)
2827
{
29-
stopWath.Start();
28+
stopWatch = Stopwatch.StartNew();
3029
}
3130

3231
CollectionEntry ce = source.PersistenceContext.GetCollectionEntry(collection);
@@ -52,10 +51,10 @@ public virtual void OnInitializeCollection(InitializeCollectionEvent @event)
5251
ce.LoadedPersister.Initialize(ce.LoadedKey, source);
5352
log.Debug("collection initialized");
5453

55-
if (statsEnabled)
54+
if (stopWatch != null)
5655
{
57-
stopWath.Stop();
58-
source.Factory.StatisticsImplementor.FetchCollection(ce.LoadedPersister.Role, stopWath.Elapsed);
56+
stopWatch.Stop();
57+
source.Factory.StatisticsImplementor.FetchCollection(ce.LoadedPersister.Role, stopWatch.Elapsed);
5958
}
6059
}
6160
}

src/NHibernate/Event/Default/DefaultLoadEventListener.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -344,19 +344,18 @@ protected virtual object LoadFromDatasource(LoadEvent @event, IEntityPersister p
344344
{
345345
ISessionImplementor source = @event.Session;
346346

347-
bool statsEnabled = source.Factory.Statistics.IsStatisticsEnabled;
348-
var stopWath = new Stopwatch();
349-
if (statsEnabled)
347+
Stopwatch stopWatch = null;
348+
if (source.Factory.Statistics.IsStatisticsEnabled)
350349
{
351-
stopWath.Start();
350+
stopWatch = Stopwatch.StartNew();
352351
}
353352

354353
object entity = persister.Load(@event.EntityId, @event.InstanceToLoad, @event.LockMode, source);
355354

356-
if (@event.IsAssociationFetch && statsEnabled)
355+
if (stopWatch != null && @event.IsAssociationFetch)
357356
{
358-
stopWath.Stop();
359-
source.Factory.StatisticsImplementor.FetchEntity(@event.EntityClassName, stopWath.Elapsed);
357+
stopWatch.Stop();
358+
source.Factory.StatisticsImplementor.FetchEntity(@event.EntityClassName, stopWatch.Elapsed);
360359
}
361360

362361
return entity;

src/NHibernate/Impl/MultiCriteriaImpl.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,10 @@ private void CombineCriteriaQueries()
218218

219219
private void GetResultsFromDatabase(IList results)
220220
{
221-
bool statsEnabled = session.Factory.Statistics.IsStatisticsEnabled;
222-
var stopWatch = new Stopwatch();
223-
if (statsEnabled)
221+
Stopwatch stopWatch = null;
222+
if (session.Factory.Statistics.IsStatisticsEnabled)
224223
{
225-
stopWatch.Start();
224+
stopWatch = Stopwatch.StartNew();
226225
}
227226
int rowCount = 0;
228227
var cacheBatcher = new CacheBatcher(session);
@@ -291,7 +290,7 @@ private void GetResultsFromDatabase(IList results)
291290
log.Error(sqle, "Failed to execute multi criteria: [{0}]", resultSetsCommand.Sql);
292291
throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, sqle, "Failed to execute multi criteria", resultSetsCommand.Sql);
293292
}
294-
if (statsEnabled)
293+
if (stopWatch != null)
295294
{
296295
stopWatch.Stop();
297296
session.Factory.StatisticsImplementor.QueryExecuted(string.Format("{0} queries (MultiCriteria)", loaders.Count), rowCount, stopWatch.Elapsed);

0 commit comments

Comments
 (0)