-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
StatisticalLoggingSessionEventListener.java
323 lines (249 loc) · 9.75 KB
/
StatisticalLoggingSessionEventListener.java
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.engine.internal;
import org.hibernate.BaseSessionEventListener;
import org.jboss.logging.Logger;
/**
* @see org.hibernate.cfg.AvailableSettings#LOG_SESSION_METRICS
*
* @author Steve Ebersole
*/
public class StatisticalLoggingSessionEventListener extends BaseSessionEventListener {
private static final Logger log = Logger.getLogger( StatisticalLoggingSessionEventListener.class );
/**
* Used by SettingsFactory (in conjunction with stats being enabled) to determine whether to apply this listener
*
* @return {@code true} if logging is enabled for this listener.
*/
public static boolean isLoggingEnabled() {
return log.isInfoEnabled();
}
// cumulative state ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private int jdbcConnectionAcquisitionCount;
private long jdbcConnectionAcquisitionTime;
private int jdbcConnectionReleaseCount;
private long jdbcConnectionReleaseTime;
private int jdbcPrepareStatementCount;
private long jdbcPrepareStatementTime;
private int jdbcExecuteStatementCount;
private long jdbcExecuteStatementTime;
private int jdbcExecuteBatchCount;
private long jdbcExecuteBatchTime;
private int cachePutCount;
private long cachePutTime;
private int cacheHitCount;
private long cacheHitTime;
private int cacheMissCount;
private long cacheMissTime;
private int flushCount;
private long flushEntityCount;
private long flushCollectionCount;
private long flushTime;
private int partialFlushCount;
private long partialFlushEntityCount;
private long partialFlushCollectionCount;
private long partialFlushTime;
private int prePartialFlushCount;
private long prePartialFlushTime;
// JDBC Connection acquisition ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private long jdbcConnectionAcquisitionStart = -1;
@Override
public void jdbcConnectionAcquisitionStart() {
assert jdbcConnectionAcquisitionStart < 0 : "Nested calls to jdbcConnectionAcquisitionStart";
jdbcConnectionAcquisitionStart = System.nanoTime();
}
@Override
public void jdbcConnectionAcquisitionEnd() {
assert jdbcConnectionAcquisitionStart > 0:
"Unexpected call to jdbcConnectionAcquisitionEnd; expecting jdbcConnectionAcquisitionStart";
jdbcConnectionAcquisitionCount++;
jdbcConnectionAcquisitionTime += ( System.nanoTime() - jdbcConnectionAcquisitionStart );
jdbcConnectionAcquisitionStart = -1;
}
// JDBC Connection release ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private long jdbcConnectionReleaseStart = -1;
@Override
public void jdbcConnectionReleaseStart() {
assert jdbcConnectionReleaseStart < 0 : "Nested calls to jdbcConnectionReleaseStart";
jdbcConnectionReleaseStart = System.nanoTime();
}
@Override
public void jdbcConnectionReleaseEnd() {
assert jdbcConnectionReleaseStart > 0:
"Unexpected call to jdbcConnectionReleaseEnd; expecting jdbcConnectionReleaseStart";
jdbcConnectionReleaseCount++;
jdbcConnectionReleaseTime += ( System.nanoTime() - jdbcConnectionReleaseStart );
jdbcConnectionReleaseStart = -1;
}
// JDBC statement preparation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private long jdbcPrepStart = -1;
@Override
public void jdbcPrepareStatementStart() {
assert jdbcPrepStart < 0 : "Nested calls to jdbcPrepareStatementStart";
jdbcPrepStart = System.nanoTime();
}
@Override
public void jdbcPrepareStatementEnd() {
assert jdbcPrepStart > 0 : "Unexpected call to jdbcPrepareStatementEnd; expecting jdbcPrepareStatementStart";
jdbcPrepareStatementCount++;
jdbcPrepareStatementTime += ( System.nanoTime() - jdbcPrepStart );
jdbcPrepStart = -1;
}
// JDBC statement execution ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private long jdbcExecutionStart = -1;
@Override
public void jdbcExecuteStatementStart() {
assert jdbcExecutionStart < 0 : "Nested calls to jdbcExecuteStatementStart";
jdbcExecutionStart = System.nanoTime();
}
@Override
public void jdbcExecuteStatementEnd() {
assert jdbcExecutionStart > 0 : "Unexpected call to jdbcExecuteStatementEnd; expecting jdbcExecuteStatementStart";
jdbcExecuteStatementCount++;
jdbcExecuteStatementTime += ( System.nanoTime() - jdbcExecutionStart );
jdbcExecutionStart = -1;
}
// JDBC batch execution ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private long jdbcBatchExecutionStart = -1;
@Override
public void jdbcExecuteBatchStart() {
assert jdbcBatchExecutionStart < 0 : "Nested calls to jdbcExecuteBatchStart";
jdbcBatchExecutionStart = System.nanoTime();
}
@Override
public void jdbcExecuteBatchEnd() {
assert jdbcBatchExecutionStart > 0 : "Unexpected call to jdbcExecuteBatchEnd; expecting jdbcExecuteBatchStart";
jdbcExecuteBatchCount++;
jdbcExecuteBatchTime += ( System.nanoTime() - jdbcBatchExecutionStart );
jdbcBatchExecutionStart = -1;
}
// Caching - put ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private long cachePutStart = -1;
@Override
public void cachePutStart() {
assert cachePutStart < 0 : "Nested calls to cachePutStart";
cachePutStart = System.nanoTime();
}
@Override
public void cachePutEnd() {
assert cachePutStart > 0 : "Unexpected call to cachePutEnd; expecting cachePutStart";
cachePutCount++;
cachePutTime += ( System.nanoTime() - cachePutStart );
cachePutStart = -1;
}
// Caching - get ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private long cacheGetStart = -1;
@Override
public void cacheGetStart() {
assert cacheGetStart < 0 : "Nested calls to cacheGetStart";
cacheGetStart = System.nanoTime();
}
@Override
public void cacheGetEnd(boolean hit) {
assert cacheGetStart > 0 : "Unexpected call to cacheGetEnd; expecting cacheGetStart";
if ( hit ) {
cacheHitCount++;
cacheHitTime += ( System.nanoTime() - cacheGetStart );
}
else {
cacheMissCount++;
cacheMissTime += ( System.nanoTime() - cacheGetStart );
}
cacheGetStart = -1;
}
// Flushing ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private long flushStart = -1;
@Override
public void flushStart() {
assert flushStart < 0 : "Nested calls to flushStart";
flushStart = System.nanoTime();
}
@Override
public void flushEnd(int numberOfEntities, int numberOfCollections) {
assert flushStart > 0 : "Unexpected call to flushEnd; expecting flushStart";
flushCount++;
flushEntityCount += numberOfEntities;
flushCollectionCount += numberOfCollections;
flushTime += ( System.nanoTime() - flushStart );
flushStart = -1;
}
// Partial-flushing ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private long partialFlushStart = -1;
private long prePartialFlushStart = -1;
@Override
public void prePartialFlushStart() {
assert prePartialFlushStart < 0 : "Nested calls to prePartialFlushStart";
prePartialFlushStart = System.nanoTime();
}
@Override
public void prePartialFlushEnd() {
assert prePartialFlushStart > 0 : "Unexpected call to partialFlushEnd; expecting partialFlushStart";
prePartialFlushCount++;
prePartialFlushTime += ( System.nanoTime() - partialFlushStart );
prePartialFlushStart = -1;
}
@Override
public void partialFlushStart() {
assert partialFlushStart < 0 : "Nested calls to partialFlushStart";
partialFlushStart = System.nanoTime();
}
@Override
public void partialFlushEnd(int numberOfEntities, int numberOfCollections) {
assert partialFlushStart > 0 : "Unexpected call to partialFlushEnd; expecting partialFlushStart";
partialFlushCount++;
partialFlushEntityCount += numberOfEntities;
partialFlushCollectionCount += numberOfCollections;
partialFlushTime += ( System.nanoTime() - partialFlushStart );
partialFlushStart = -1;
}
@Override
public void end() {
if ( log.isInfoEnabled() ) {
log.infof(
"Session Metrics {\n" +
" %s nanoseconds spent acquiring %s JDBC connections;\n" +
" %s nanoseconds spent releasing %s JDBC connections;\n" +
" %s nanoseconds spent preparing %s JDBC statements;\n" +
" %s nanoseconds spent executing %s JDBC statements;\n" +
" %s nanoseconds spent executing %s JDBC batches;\n" +
" %s nanoseconds spent performing %s L2C puts;\n" +
" %s nanoseconds spent performing %s L2C hits;\n" +
" %s nanoseconds spent performing %s L2C misses;\n" +
" %s nanoseconds spent executing %s flushes (flushing a total of %s entities and %s collections);\n" +
" %s nanoseconds spent executing %s pre-partial-flushes;\n" +
" %s nanoseconds spent executing %s partial-flushes (flushing a total of %s entities and %s collections)\n" +
"}",
jdbcConnectionAcquisitionTime,
jdbcConnectionAcquisitionCount,
jdbcConnectionReleaseTime,
jdbcConnectionReleaseCount,
jdbcPrepareStatementTime,
jdbcPrepareStatementCount,
jdbcExecuteStatementTime,
jdbcExecuteStatementCount,
jdbcExecuteBatchTime,
jdbcExecuteBatchCount,
cachePutTime,
cachePutCount,
cacheHitTime,
cacheHitCount,
cacheMissTime,
cacheMissCount,
flushTime,
flushCount,
flushEntityCount,
flushCollectionCount,
prePartialFlushTime,
prePartialFlushCount,
partialFlushTime,
partialFlushCount,
partialFlushEntityCount,
partialFlushCollectionCount
);
}
}
}