KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > stat > StatisticsImpl


1 //$Id: StatisticsImpl.java,v 1.15 2005/06/09 06:33:06 oneovthafew Exp $
2
package org.hibernate.stat;
3 import java.util.HashMap JavaDoc;
4 import java.util.Map JavaDoc;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.hibernate.cache.Cache;
9 import org.hibernate.engine.SessionFactoryImplementor;
10 import org.hibernate.util.ArrayHelper;
11
12 /**
13  * @see org.hibernate.stat.Statistics
14  *
15  * @author Gavin King
16  */

17 public class StatisticsImpl implements Statistics, StatisticsImplementor {
18     
19     //TODO: we should provide some way to get keys of collection of statistics to make it easier to retrieve from a GUI perspective
20

21     private static final Log log = LogFactory.getLog(StatisticsImpl.class);
22
23     private SessionFactoryImplementor sessionFactory;
24
25     private boolean isStatisticsEnabled;
26     private long startTime;
27     private long sessionOpenCount;
28     private long sessionCloseCount;
29     private long flushCount;
30     private long connectCount;
31     
32     private long prepareStatementCount;
33     private long closeStatementCount;
34     
35     private long entityLoadCount;
36     private long entityUpdateCount;
37     private long entityInsertCount;
38     private long entityDeleteCount;
39     private long entityFetchCount;
40     private long collectionLoadCount;
41     private long collectionUpdateCount;
42     private long collectionRemoveCount;
43     private long collectionRecreateCount;
44     private long collectionFetchCount;
45     
46     private long secondLevelCacheHitCount;
47     private long secondLevelCacheMissCount;
48     private long secondLevelCachePutCount;
49     
50     private long queryExecutionCount;
51     private long queryExecutionMaxTime;
52     private long queryCacheHitCount;
53     private long queryCacheMissCount;
54     private long queryCachePutCount;
55     
56     private long commitedTransactionCount;
57     private long transactionCount;
58     
59     /** second level cache statistics per region */
60     private final Map JavaDoc secondLevelCacheStatistics = new HashMap JavaDoc();
61     /** entity statistics per name */
62     private final Map JavaDoc entityStatistics = new HashMap JavaDoc();
63     /** collection statistics per name */
64     private final Map JavaDoc collectionStatistics = new HashMap JavaDoc();
65     /** entity statistics per query string (HQL or SQL) */
66     private final Map JavaDoc queryStatistics = new HashMap JavaDoc();
67
68     public StatisticsImpl() {
69         clear();
70     }
71
72     public StatisticsImpl(SessionFactoryImplementor sessionFactory) {
73         clear();
74         this.sessionFactory = sessionFactory;
75     }
76     
77     /**
78      * reset all statistics
79      */

80     public synchronized void clear() {
81         secondLevelCacheHitCount = 0;
82         secondLevelCacheMissCount = 0;
83         secondLevelCachePutCount = 0;
84         
85         sessionCloseCount = 0;
86         sessionOpenCount = 0;
87         flushCount = 0;
88         connectCount = 0;
89         
90         prepareStatementCount = 0;
91         closeStatementCount = 0;
92         
93         entityDeleteCount = 0;
94         entityInsertCount = 0;
95         entityUpdateCount = 0;
96         entityLoadCount = 0;
97         entityFetchCount = 0;
98         
99         collectionRemoveCount = 0;
100         collectionUpdateCount = 0;
101         collectionRecreateCount = 0;
102         collectionLoadCount = 0;
103         collectionFetchCount = 0;
104         
105         queryExecutionCount = 0;
106         queryCacheHitCount = 0;
107         queryExecutionMaxTime = 0;
108         queryCacheMissCount = 0;
109         queryCachePutCount = 0;
110         
111         secondLevelCacheStatistics.clear();
112         entityStatistics.clear();
113         collectionStatistics.clear();
114         queryStatistics.clear();
115         
116         startTime = System.currentTimeMillis();
117     }
118     
119     public synchronized void openSession() {
120         sessionOpenCount++;
121     }
122     
123     public synchronized void closeSession() {
124         sessionCloseCount++;
125     }
126     
127     public synchronized void flush() {
128         flushCount++;
129     }
130     
131     public synchronized void connect() {
132         connectCount++;
133     }
134     
135     public synchronized void loadEntity(String JavaDoc entityName) {
136         entityLoadCount++;
137         getEntityStatistics(entityName).loadCount++;
138     }
139
140     public synchronized void fetchEntity(String JavaDoc entityName) {
141         entityFetchCount++;
142         getEntityStatistics(entityName).fetchCount++;
143     }
144
145     /**
146      * find entity statistics per name
147      *
148      * @param entityName entity name
149      * @return EntityStatistics object
150      */

151     public synchronized EntityStatistics getEntityStatistics(String JavaDoc entityName) {
152         EntityStatistics es = (EntityStatistics) entityStatistics.get(entityName);
153         if (es==null) {
154             es = new EntityStatistics(entityName);
155             entityStatistics.put(entityName, es);
156         }
157         return es;
158     }
159     
160     public synchronized void updateEntity(String JavaDoc entityName) {
161         entityUpdateCount++;
162         EntityStatistics es = getEntityStatistics(entityName);
163         es.updateCount++;
164     }
165
166     public synchronized void insertEntity(String JavaDoc entityName) {
167         entityInsertCount++;
168         EntityStatistics es = getEntityStatistics(entityName);
169         es.insertCount++;
170     }
171
172     public synchronized void deleteEntity(String JavaDoc entityName) {
173         entityDeleteCount++;
174         EntityStatistics es = getEntityStatistics(entityName);
175         es.deleteCount++;
176     }
177
178     /**
179      * Get collection statistics per role
180      *
181      * @param role collection role
182      * @return CollectionStatistics
183      */

184     public synchronized CollectionStatistics getCollectionStatistics(String JavaDoc role) {
185         CollectionStatistics cs = (CollectionStatistics) collectionStatistics.get(role);
186         if (cs==null) {
187             cs = new CollectionStatistics(role);
188             collectionStatistics.put(role, cs);
189         }
190         return cs;
191     }
192     
193     public synchronized void loadCollection(String JavaDoc role) {
194         collectionLoadCount++;
195         getCollectionStatistics(role).loadCount++;
196     }
197
198     public synchronized void fetchCollection(String JavaDoc role) {
199         collectionFetchCount++;
200         getCollectionStatistics(role).fetchCount++;
201     }
202
203     public synchronized void updateCollection(String JavaDoc role) {
204         collectionUpdateCount++;
205         getCollectionStatistics(role).updateCount++;
206     }
207
208     public synchronized void recreateCollection(String JavaDoc role) {
209         collectionRecreateCount++;
210         getCollectionStatistics(role).recreateCount++;
211     }
212
213     public synchronized void removeCollection(String JavaDoc role) {
214         collectionRemoveCount++;
215         getCollectionStatistics(role).removeCount++;
216     }
217     
218     /**
219      * Second level cache statistics per region
220      *
221      * @param regionName region name
222      * @return SecondLevelCacheStatistics
223      */

224     public synchronized SecondLevelCacheStatistics getSecondLevelCacheStatistics(String JavaDoc regionName) {
225         SecondLevelCacheStatistics slcs = (SecondLevelCacheStatistics) secondLevelCacheStatistics.get(regionName);
226         if (slcs==null) {
227             if (sessionFactory == null) return null;
228             Cache cache = sessionFactory.getSecondLevelCacheRegion(regionName);
229             if (cache==null) return null;
230             slcs = new SecondLevelCacheStatistics(cache);
231             secondLevelCacheStatistics.put(regionName, slcs);
232         }
233         return slcs;
234     }
235
236     public synchronized void secondLevelCachePut(String JavaDoc regionName) {
237         secondLevelCachePutCount++;
238         getSecondLevelCacheStatistics(regionName).putCount++;
239     }
240
241     public synchronized void secondLevelCacheHit(String JavaDoc regionName) {
242         secondLevelCacheHitCount++;
243         getSecondLevelCacheStatistics(regionName).hitCount++;
244     }
245
246     public synchronized void secondLevelCacheMiss(String JavaDoc regionName) {
247         secondLevelCacheMissCount++;
248         getSecondLevelCacheStatistics(regionName).missCount++;
249     }
250
251     public synchronized void queryExecuted(String JavaDoc hql, int rows, long time) {
252         queryExecutionCount++;
253         if (queryExecutionMaxTime<time) queryExecutionMaxTime=time;
254         if (hql!=null) {
255             QueryStatistics qs = getQueryStatistics(hql);
256             qs.executed(rows, time);
257         }
258     }
259     
260     public synchronized void queryCacheHit(String JavaDoc hql, String JavaDoc regionName) {
261         queryCacheHitCount++;
262         if (hql!=null) {
263             QueryStatistics qs = getQueryStatistics(hql);
264             qs.cacheHitCount++;
265         }
266         SecondLevelCacheStatistics slcs = getSecondLevelCacheStatistics(regionName);
267         slcs.hitCount++;
268     }
269
270     public synchronized void queryCacheMiss(String JavaDoc hql, String JavaDoc regionName) {
271         queryCacheMissCount++;
272         if (hql!=null) {
273             QueryStatistics qs = getQueryStatistics(hql);
274             qs.cacheMissCount++;
275         }
276         SecondLevelCacheStatistics slcs = getSecondLevelCacheStatistics(regionName);
277         slcs.missCount++;
278     }
279
280     public synchronized void queryCachePut(String JavaDoc hql, String JavaDoc regionName) {
281         queryCachePutCount++;
282         if (hql!=null) {
283             QueryStatistics qs = getQueryStatistics(hql);
284             qs.cachePutCount++;
285         }
286         SecondLevelCacheStatistics slcs = getSecondLevelCacheStatistics(regionName);
287         slcs.putCount++;
288     }
289
290     /**
291      * Query statistics from query string (HQL or SQL)
292      *
293      * @param queryString query string
294      * @return QueryStatistics
295      */

296     public synchronized QueryStatistics getQueryStatistics(String JavaDoc queryString) {
297         QueryStatistics qs = (QueryStatistics) queryStatistics.get(queryString);
298         if (qs==null) {
299             qs = new QueryStatistics(queryString);
300             queryStatistics.put(queryString, qs);
301         }
302         return qs;
303     }
304
305     /**
306      * @return entity deletion count
307      */

308     public long getEntityDeleteCount() {
309         return entityDeleteCount;
310     }
311     
312     /**
313      * @return entity insertion count
314      */

315     public long getEntityInsertCount() {
316         return entityInsertCount;
317     }
318     
319     /**
320      * @return entity load (from DB)
321      */

322     public long getEntityLoadCount() {
323         return entityLoadCount;
324     }
325     
326     /**
327      * @return entity fetch (from DB)
328      */

329     public long getEntityFetchCount() {
330         return entityFetchCount;
331     }
332
333     /**
334      * @return entity update
335      */

336     public long getEntityUpdateCount() {
337         return entityUpdateCount;
338     }
339
340     public long getQueryExecutionCount() {
341         return queryExecutionCount;
342     }
343     
344     public long getQueryCacheHitCount() {
345         return queryCacheHitCount;
346     }
347     
348     public long getQueryCacheMissCount() {
349         return queryCacheMissCount;
350     }
351     
352     public long getQueryCachePutCount() {
353         return queryCachePutCount;
354     }
355     
356     /**
357      * @return flush
358      */

359     public long getFlushCount() {
360         return flushCount;
361     }
362     
363     /**
364      * @return session connect
365      */

366     public long getConnectCount() {
367         return connectCount;
368     }
369
370     /**
371      * @return second level cache hit
372      */

373     public long getSecondLevelCacheHitCount() {
374         return secondLevelCacheHitCount;
375     }
376
377     /**
378      * @return second level cache miss
379      */

380     public long getSecondLevelCacheMissCount() {
381         return secondLevelCacheMissCount;
382     }
383     
384     /**
385      * @return second level cache put
386      */

387     public long getSecondLevelCachePutCount() {
388         return secondLevelCachePutCount;
389     }
390
391     /**
392      * @return session closing
393      */

394     public long getSessionCloseCount() {
395         return sessionCloseCount;
396     }
397     
398     /**
399      * @return session opening
400      */

401     public long getSessionOpenCount() {
402         return sessionOpenCount;
403     }
404
405     /**
406      * @return collection loading (from DB)
407      */

408     public long getCollectionLoadCount() {
409         return collectionLoadCount;
410     }
411
412     /**
413      * @return collection fetching (from DB)
414      */

415     public long getCollectionFetchCount() {
416         return collectionFetchCount;
417     }
418     
419     /**
420      * @return collection update
421      */

422     public long getCollectionUpdateCount() {
423         return collectionUpdateCount;
424     }
425
426     /**
427      * @return collection removal
428      * FIXME: even if isInverse="true"?
429      */

430     public long getCollectionRemoveCount() {
431         return collectionRemoveCount;
432     }
433     /**
434      * @return collection recreation
435      */

436     public long getCollectionRecreateCount() {
437         return collectionRecreateCount;
438     }
439
440     /**
441      * @return start time in ms (JVM standards {@link System#currentTimeMillis()})
442      */

443     public long getStartTime() {
444         return startTime;
445     }
446     
447     /**
448      * log in info level the main statistics
449      */

450     public void logSummary() {
451         log.info("Logging statistics....");
452         log.info("start time: " + startTime);
453         log.info("sessions opened: " + sessionOpenCount);
454         log.info("sessions closed: " + sessionCloseCount);
455         log.info("transactions: " + transactionCount);
456         log.info("successful transactions: " + commitedTransactionCount);
457         log.info("flushes: " + flushCount);
458         log.info("connections obtained: " + connectCount);
459         log.info("statements prepared: " + prepareStatementCount);
460         log.info("statements closed: " + closeStatementCount);
461         log.info("second level cache puts: " + secondLevelCachePutCount);
462         log.info("second level cache hits: " + secondLevelCacheHitCount);
463         log.info("second level cache misses: " + secondLevelCacheMissCount);
464         log.info("entities loaded: " + entityLoadCount);
465         log.info("entities updated: " + entityUpdateCount);
466         log.info("entities inserted: " + entityInsertCount);
467         log.info("entities deleted: " + entityDeleteCount);
468         log.info("entities fetched (minimize this): " + entityFetchCount);
469         log.info("collections loaded: " + collectionLoadCount);
470         log.info("collections updated: " + collectionUpdateCount);
471         log.info("collections removed: " + collectionRemoveCount);
472         log.info("collections recreated: " + collectionRecreateCount);
473         log.info("collections fetched (minimize this): " + collectionFetchCount);
474         log.info("queries executed to database: " + queryExecutionCount);
475         log.info("query cache puts: " + queryCachePutCount);
476         log.info("query cache hits: " + queryCacheHitCount);
477         log.info("query cache misses: " + queryCacheMissCount);
478         log.info("max query time: " + queryExecutionMaxTime + "ms");
479     }
480     
481     /**
482      * Are statistics logged
483      */

484     public boolean isStatisticsEnabled() {
485         return isStatisticsEnabled;
486     }
487     
488     /**
489      * Enable statistics logs (this is a dynamic parameter)
490      */

491     public void setStatisticsEnabled(boolean b) {
492         isStatisticsEnabled = b;
493     }
494
495     /**
496      * @return Returns the max query execution time,
497      * for all queries
498      */

499     public long getQueryExecutionMaxTime() {
500         return queryExecutionMaxTime;
501     }
502     
503     /**
504      * Get all executed query strings
505      */

506     public String JavaDoc[] getQueries() {
507         return ArrayHelper.toStringArray( queryStatistics.keySet() );
508     }
509     
510     /**
511      * Get the names of all entities
512      */

513     public String JavaDoc[] getEntityNames() {
514         if (sessionFactory==null) {
515             return ArrayHelper.toStringArray( entityStatistics.keySet() );
516         }
517         else {
518             return ArrayHelper.toStringArray( sessionFactory.getAllClassMetadata().keySet() );
519         }
520     }
521
522     /**
523      * Get the names of all collection roles
524      */

525     public String JavaDoc[] getCollectionRoleNames() {
526         if (sessionFactory==null) {
527             return ArrayHelper.toStringArray( collectionStatistics.keySet() );
528         }
529         else {
530             return ArrayHelper.toStringArray( sessionFactory.getAllCollectionMetadata().keySet() );
531         }
532     }
533     
534     /**
535      * Get all second-level cache region names
536      */

537     public String JavaDoc[] getSecondLevelCacheRegionNames() {
538         if (sessionFactory==null) {
539             return ArrayHelper.toStringArray( secondLevelCacheStatistics.keySet() );
540         }
541         else {
542             return ArrayHelper.toStringArray( sessionFactory.getAllSecondLevelCacheRegions().keySet() );
543         }
544     }
545
546     public void endTransaction(boolean success) {
547         transactionCount++;
548         if (success) commitedTransactionCount++;
549     }
550     
551     public long getSuccessfulTransactionCount() {
552         return commitedTransactionCount;
553     }
554     
555     public long getTransactionCount() {
556         return transactionCount;
557     }
558
559     public void closeStatement() {
560         closeStatementCount++;
561     }
562
563     public void prepareStatement() {
564         prepareStatementCount++;
565     }
566
567     public long getCloseStatementCount() {
568         return closeStatementCount;
569     }
570
571     public long getPrepareStatementCount() {
572         return prepareStatementCount;
573     }
574
575     public String JavaDoc toString() {
576         return new StringBuffer JavaDoc()
577             .append("Statistics[")
578             .append("start time=").append(startTime)
579             .append(",sessions opened=").append(sessionOpenCount)
580             .append(",sessions closed=").append(sessionCloseCount)
581             .append(",transactions=").append(transactionCount)
582             .append(",successful transactions=").append(commitedTransactionCount)
583             .append(",flushes=").append(flushCount)
584             .append(",connections obtained=").append(connectCount)
585             .append(",statements prepared=").append(prepareStatementCount)
586             .append(",statements closed=").append(closeStatementCount)
587             .append(",second level cache puts=").append(secondLevelCachePutCount)
588             .append(",second level cache hits=").append(secondLevelCacheHitCount)
589             .append(",second level cache misses=").append(secondLevelCacheMissCount)
590             .append(",entities loaded=").append(entityLoadCount)
591             .append(",entities updated=").append(entityUpdateCount)
592             .append(",entities inserted=").append(entityInsertCount)
593             .append(",entities deleted=").append(entityDeleteCount)
594             .append(",entities fetched=").append(entityFetchCount)
595             .append(",collections loaded=").append(collectionLoadCount)
596             .append(",collections updated=").append(collectionUpdateCount)
597             .append(",collections removed=").append(collectionRemoveCount)
598             .append(",collections recreated=").append(collectionRecreateCount)
599             .append(",collections fetched=").append(collectionFetchCount)
600             .append(",queries executed to database=").append(queryExecutionCount)
601             .append(",query cache puts=").append(queryCachePutCount)
602             .append(",query cache hits=").append(queryCacheHitCount)
603             .append(",query cache misses=").append(queryCacheMissCount)
604             .append(",max query time=").append(queryExecutionMaxTime)
605             .append(']')
606             .toString();
607     }
608     
609 }
Popular Tags