KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: QueryStatistics.java,v 1.10 2005/06/09 06:33:06 oneovthafew Exp $
2
package org.hibernate.stat;
3
4
5 /**
6  * Query statistics (HQL and SQL)
7  *
8  * Note that for a cached query, the cache miss is equals to the db count
9  *
10  * @author Gavin King
11  */

12 public class QueryStatistics extends CategorizedStatistics {
13     
14     QueryStatistics(String JavaDoc query) {
15         super(query);
16     }
17     
18     long cacheHitCount;
19     long cacheMissCount;
20     long cachePutCount;
21     private long executionCount;
22     private long executionRowCount;
23     private long executionAvgTime;
24     private long executionMaxTime;
25     private long executionMinTime;
26
27     /**
28      * queries executed to the DB
29      */

30     public long getExecutionCount() {
31         return executionCount;
32     }
33     
34     /**
35      * Queries retrieved successfully from the cache
36      */

37     public long getCacheHitCount() {
38         return cacheHitCount;
39     }
40     
41     public long getCachePutCount() {
42         return cachePutCount;
43     }
44     
45     public long getCacheMissCount() {
46         return cacheMissCount;
47     }
48     
49     /**
50      * Number of lines returned by all the executions of this query (from DB)
51      * For now, {@link org.hibernate.Query#iterate()}
52      * and {@link org.hibernate.Query#scroll()()} do not fill this statistic
53      * @return
54      */

55     public long getExecutionRowCount() {
56         return executionRowCount;
57     }
58
59     /**
60      * average time in ms taken by the excution of this query onto the DB
61      */

62     public long getExecutionAvgTime() {
63         return executionAvgTime;
64     }
65
66     /**
67      * max time in ms taken by the excution of this query onto the DB
68      */

69     public long getExecutionMaxTime() {
70         return executionMaxTime;
71     }
72     
73     /**
74      * min time in ms taken by the excution of this query onto the DB
75      */

76     public long getExecutionMinTime() {
77         return executionMinTime;
78     }
79     
80     /**
81      * add statistics report of a DB query
82      *
83      * @param rows rows count returned
84      * @param time time taken
85      */

86     void executed(long rows, long time) {
87         if (time < executionMinTime) executionMinTime = time;
88         if (time > executionMaxTime) executionMaxTime = time;
89         executionAvgTime = ( executionAvgTime * executionCount + time ) / ( executionCount + 1 );
90         executionCount++;
91         executionRowCount += rows;
92     }
93
94     public String JavaDoc toString() {
95         return new StringBuffer JavaDoc()
96             .append("QueryStatistics")
97             .append("[cacheHitCount=").append(this.cacheHitCount)
98             .append(",cacheMissCount=").append(this.cacheMissCount)
99             .append(",cachePutCount=").append(this.cachePutCount)
100             .append(",executionCount=").append(this.executionCount)
101             .append(",executionRowCount=").append(this.executionRowCount)
102             .append(",executionAvgTime=").append(this.executionAvgTime)
103             .append(",executionMaxTime=").append(this.executionMaxTime)
104             .append(",executionMinTime=").append(this.executionMinTime)
105             .append(']')
106             .toString();
107     }
108
109 }
110
Popular Tags