forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryStatistics.cs
91 lines (79 loc) · 2.31 KB
/
QueryStatistics.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Text;
namespace NHibernate.Stat
{
/// <summary> Query statistics (HQL and SQL) </summary>
/// <remarks>Note that for a cached query, the cache miss is equals to the db count</remarks>
[Serializable]
public class QueryStatistics : CategorizedStatistics
{
internal long cacheHitCount;
internal long cacheMissCount;
internal long cachePutCount;
private long executionCount;
private long executionRowCount;
private TimeSpan executionAvgTime;
private TimeSpan executionMaxTime;
private TimeSpan executionMinTime = TimeSpan.MaxValue;
public QueryStatistics(string categoryName) : base(categoryName) { }
public long CacheHitCount
{
get { return cacheHitCount; }
}
public long CacheMissCount
{
get { return cacheMissCount; }
}
public long CachePutCount
{
get { return cachePutCount; }
}
public long ExecutionCount
{
get { return executionCount; }
}
public long ExecutionRowCount
{
get { return executionRowCount; }
}
public TimeSpan ExecutionAvgTime
{
get { return executionAvgTime; }
}
public TimeSpan ExecutionMaxTime
{
get { return executionMaxTime; }
}
public TimeSpan ExecutionMinTime
{
get { return executionMinTime; }
}
/// <summary> Add statistics report of a DB query </summary>
/// <param name="rows">rows count returned </param>
/// <param name="time">time taken </param>
internal void Executed(long rows, TimeSpan time)
{
if (time < executionMinTime)
executionMinTime = time;
if (time > executionMaxTime)
executionMaxTime = time;
executionCount++;
executionRowCount += rows;
executionAvgTime = TimeSpan.FromTicks((executionAvgTime.Ticks * (executionCount - 1) + time.Ticks) / executionCount);
}
public override string ToString()
{
return new StringBuilder()
.Append("QueryStatistics[")
.Append("cacheHitCount=").Append(cacheHitCount)
.Append(",cacheMissCount=").Append(cacheMissCount)
.Append(",cachePutCount=").Append(cachePutCount)
.Append(",executionCount=").Append(executionCount)
.Append(",executionRowCount=").Append(executionRowCount)
.Append(",executionAvgTime=").Append(executionAvgTime)
.Append(",executionMaxTime=").Append(executionMaxTime)
.Append(",executionMinTime=").Append(executionMinTime)
.Append(']').ToString();
}
}
}