forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatsFixture.cs
262 lines (234 loc) · 8.18 KB
/
StatsFixture.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using System.Collections;
using Iesi.Collections.Generic;
using NHibernate.Criterion;
using NHibernate.Stat;
using NUnit.Framework;
namespace NHibernate.Test.Stats
{
[TestFixture]
public class StatsFixture : TestCase
{
protected override string MappingsAssembly
{
get { return "NHibernate.Test"; }
}
protected override IList Mappings
{
get { return new string[] { "Stats.Continent.hbm.xml" }; }
}
protected override void Configure(Cfg.Configuration configuration)
{
configuration.SetProperty(Cfg.Environment.GenerateStatistics, "true");
}
private static Continent FillDb(ISession s)
{
Continent europe = new Continent();
europe.Name="Europe";
Country france = new Country();
france.Name="France";
europe.Countries=new HashedSet<Country>();
europe.Countries.Add(france);
s.Save(france);
s.Save(europe);
return europe;
}
private static void CleanDb(ISession s)
{
s.Delete("from Locality");
s.Delete("from Country");
s.Delete("from Continent");
}
[Test]
public void CollectionFetchVsLoad()
{
IStatistics stats = sessions.Statistics;
stats.Clear();
ISession s = OpenSession();
ITransaction tx = s.BeginTransaction();
Continent europe = FillDb(s);
tx.Commit();
s.Clear();
tx = s.BeginTransaction();
Assert.AreEqual(0, stats.CollectionLoadCount);
Assert.AreEqual(0, stats.CollectionFetchCount);
Continent europe2 = s.Get<Continent>(europe.Id);
Assert.AreEqual(0, stats.CollectionLoadCount, "Lazy true: no collection should be loaded");
Assert.AreEqual(0, stats.CollectionFetchCount);
int cc = europe2.Countries.Count;
Assert.AreEqual(1, stats.CollectionLoadCount);
Assert.AreEqual(1, stats.CollectionFetchCount, "Explicit fetch of the collection state");
tx.Commit();
s.Close();
s = OpenSession();
tx = s.BeginTransaction();
stats.Clear();
europe = FillDb(s);
tx.Commit();
s.Clear();
tx = s.BeginTransaction();
Assert.AreEqual(0, stats.CollectionLoadCount);
Assert.AreEqual(0, stats.CollectionFetchCount);
europe2 = s.CreateQuery("from Continent a join fetch a.Countries where a.id = " + europe.Id).UniqueResult<Continent>();
Assert.AreEqual(1, stats.CollectionLoadCount);
Assert.AreEqual(0, stats.CollectionFetchCount, "collection should be loaded in the same query as its parent");
tx.Commit();
s.Close();
Mapping.Collection coll = cfg.GetCollectionMapping("NHibernate.Test.Stats.Continent.Countries");
coll.FetchMode = FetchMode.Join;
coll.IsLazy = false;
ISessionFactory sf = cfg.BuildSessionFactory();
stats = sf.Statistics;
stats.Clear();
stats.IsStatisticsEnabled = true;
s = sf.OpenSession();
tx = s.BeginTransaction();
europe = FillDb(s);
tx.Commit();
s.Clear();
tx = s.BeginTransaction();
Assert.AreEqual(0, stats.CollectionLoadCount);
Assert.AreEqual(0, stats.CollectionFetchCount);
europe2 = s.Get<Continent>(europe.Id);
Assert.AreEqual(1, stats.CollectionLoadCount);
Assert.AreEqual(0, stats.CollectionFetchCount,
"Should do direct load, not indirect second load when lazy false and JOIN");
tx.Commit();
s.Close();
sf.Close();
coll = cfg.GetCollectionMapping("NHibernate.Test.Stats.Continent.Countries");
coll.FetchMode = FetchMode.Select;
coll.IsLazy = false;
sf = cfg.BuildSessionFactory();
stats = sf.Statistics;
stats.Clear();
stats.IsStatisticsEnabled = true;
s = sf.OpenSession();
tx = s.BeginTransaction();
europe = FillDb(s);
tx.Commit();
s.Clear();
tx = s.BeginTransaction();
Assert.AreEqual(0, stats.CollectionLoadCount);
Assert.AreEqual(0, stats.CollectionFetchCount);
europe2 = s.Get<Continent>(europe.Id);
Assert.AreEqual(1, stats.CollectionLoadCount);
Assert.AreEqual(1, stats.CollectionFetchCount, "Should do explicit collection load, not part of the first one");
foreach (Country country in europe2.Countries)
{
s.Delete(country);
}
CleanDb(s);
tx.Commit();
s.Close();
}
[Test]
public void QueryStatGathering()
{
IStatistics stats = sessions.Statistics;
stats.Clear();
ISession s = OpenSession();
ITransaction tx = s.BeginTransaction();
FillDb(s);
tx.Commit();
s.Close();
s = OpenSession();
tx = s.BeginTransaction();
string continents = "from Continent";
int results = s.CreateQuery(continents).List().Count;
QueryStatistics continentStats = stats.GetQueryStatistics(continents);
Assert.IsNotNull(continentStats, "stats were null");
Assert.AreEqual(1, continentStats.ExecutionCount, "unexpected execution count");
Assert.AreEqual(results, continentStats.ExecutionRowCount, "unexpected row count");
var maxTime = continentStats.ExecutionMaxTime;
Assert.AreEqual(maxTime, stats.QueryExecutionMaxTime);
Assert.AreEqual( continents, stats.QueryExecutionMaxTimeQueryString );
IEnumerable itr = s.CreateQuery(continents).Enumerable();
// Enumerable() should increment the execution count
Assert.AreEqual(2, continentStats.ExecutionCount, "unexpected execution count");
// but should not effect the cumulative row count
Assert.AreEqual(results, continentStats.ExecutionRowCount, "unexpected row count");
NHibernateUtil.Close(itr);
tx.Commit();
s.Close();
// explicitly check that statistics for "split queries" get collected
// under the original query
stats.Clear();
s = OpenSession();
tx = s.BeginTransaction();
string localities = "from Locality";
results = s.CreateQuery(localities).List().Count;
QueryStatistics localityStats = stats.GetQueryStatistics(localities);
Assert.IsNotNull(localityStats, "stats were null");
// ...one for each split query
Assert.AreEqual(2, localityStats.ExecutionCount, "unexpected execution count");
Assert.AreEqual(results, localityStats.ExecutionRowCount, "unexpected row count");
maxTime = localityStats.ExecutionMaxTime;
Assert.AreEqual(maxTime, stats.QueryExecutionMaxTime);
Assert.AreEqual( localities, stats.QueryExecutionMaxTimeQueryString );
tx.Commit();
s.Close();
Assert.IsFalse(s.IsOpen);
// native sql queries
stats.Clear();
s = OpenSession();
tx = s.BeginTransaction();
string sql = "select Id, Name from Country";
results = s.CreateSQLQuery(sql).AddEntity(typeof (Country)).List().Count;
QueryStatistics sqlStats = stats.GetQueryStatistics(sql);
Assert.IsNotNull(sqlStats, "sql stats were null");
Assert.AreEqual(1, sqlStats.ExecutionCount, "unexpected execution count");
Assert.AreEqual(results, sqlStats.ExecutionRowCount, "unexpected row count");
maxTime = sqlStats.ExecutionMaxTime;
Assert.AreEqual(maxTime, stats.QueryExecutionMaxTime);
Assert.AreEqual( sql, stats.QueryExecutionMaxTimeQueryString);
tx.Commit();
s.Close();
s = OpenSession();
tx = s.BeginTransaction();
CleanDb(s);
tx.Commit();
s.Close();
}
[Test]
public void IncrementQueryExecutionCount_WhenExplicitQueryIsExecuted()
{
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
FillDb(s);
tx.Commit();
}
IStatistics stats = sessions.Statistics;
stats.Clear();
using (ISession s = OpenSession())
{
var r = s.CreateCriteria<Country>().List();
}
Assert.AreEqual(1, stats.QueryExecutionCount);
stats.Clear();
using (ISession s = OpenSession())
{
var r = s.CreateQuery("from Country").List();
}
Assert.AreEqual(1, stats.QueryExecutionCount);
stats.Clear();
using (ISession s = OpenSession())
{
var r = s.CreateMultiQuery().Add("from Country").Add("from Continent").List();
}
Assert.AreEqual(1, stats.QueryExecutionCount);
stats.Clear();
using (ISession s = OpenSession())
{
var r = s.CreateMultiCriteria().Add(DetachedCriteria.For<Country>()).Add(DetachedCriteria.For<Continent>()).List();
}
Assert.AreEqual(1, stats.QueryExecutionCount);
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
CleanDb(s);
tx.Commit();
}
}
}
}