Skip to content

Commit a14c19e

Browse files
authored
Fix QueryStatistics.ExecutionAvgTime calculation (#2968)
1 parent ecfa478 commit a14c19e

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/NHibernate.Test/Async/Stats/StatsFixture.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,14 @@ public async Task QueryStatGatheringAsync()
214214
maxTime = sqlStats.ExecutionMaxTime;
215215
Assert.AreEqual(maxTime, stats.QueryExecutionMaxTime);
216216
Assert.AreEqual( sql, stats.QueryExecutionMaxTimeQueryString);
217+
218+
// check that 2nd query correctly updates query statistics
219+
results = (await (s.CreateSQLQuery(sql).AddEntity(typeof(Country)).ListAsync())).Count;
220+
var queryTime1 = maxTime;
221+
var queryTime2 = sqlStats.ExecutionMaxTime == maxTime ? sqlStats.ExecutionMinTime : sqlStats.ExecutionMaxTime;
222+
Assert.AreEqual(2, sqlStats.ExecutionCount, "unexpected execution count");
223+
Assert.AreEqual((queryTime1 + queryTime2).Ticks / 2, sqlStats.ExecutionAvgTime.Ticks, 2, "unexpected average time");
224+
217225
await (tx.CommitAsync());
218226
s.Close();
219227

src/NHibernate.Test/Stats/StatsFixture.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ public void QueryStatGathering()
202202
maxTime = sqlStats.ExecutionMaxTime;
203203
Assert.AreEqual(maxTime, stats.QueryExecutionMaxTime);
204204
Assert.AreEqual( sql, stats.QueryExecutionMaxTimeQueryString);
205+
206+
// check that 2nd query correctly updates query statistics
207+
results = s.CreateSQLQuery(sql).AddEntity(typeof(Country)).List().Count;
208+
var queryTime1 = maxTime;
209+
var queryTime2 = sqlStats.ExecutionMaxTime == maxTime ? sqlStats.ExecutionMinTime : sqlStats.ExecutionMaxTime;
210+
Assert.AreEqual(2, sqlStats.ExecutionCount, "unexpected execution count");
211+
Assert.AreEqual((queryTime1 + queryTime2).Ticks / 2, sqlStats.ExecutionAvgTime.Ticks, 2, "unexpected average time");
212+
205213
tx.Commit();
206214
s.Close();
207215

src/NHibernate/Stat/QueryStatistics.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ internal void Executed(long rows, TimeSpan time)
7070
executionMaxTime = time;
7171
executionCount++;
7272
executionRowCount += rows;
73-
executionAvgTime = TimeSpan.FromTicks((executionAvgTime.Ticks * executionCount + time.Ticks) / executionCount);
73+
executionAvgTime = TimeSpan.FromTicks((executionAvgTime.Ticks * (executionCount - 1) + time.Ticks) / executionCount);
7474
}
7575

7676
public override string ToString()

0 commit comments

Comments
 (0)