KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > util > ResponseTimeMonitorImpl


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.util;
18
19 import java.util.Date JavaDoc;
20
21 /**
22  * Implementation of ResponseTimeMonitor for use via delegation by
23  * objects that implement this interface.
24  *
25  * @author Rod Johnson
26  * @since November 21, 2000
27  */

28 public class ResponseTimeMonitorImpl implements ResponseTimeMonitor {
29
30     /** The system time at which this object was initialized */
31     private final long initedMillis = System.currentTimeMillis();
32
33     /** The number of operations recorded by this object */
34     private volatile int accessCount;
35
36     /** The sum of the response times for all operations */
37     private volatile int totalResponseTimeMillis = 0;
38
39     /** The best response time this object has recorded */
40     private volatile int bestResponseTimeMillis = Integer.MAX_VALUE;
41
42     /** The worst response time this object has recorded */
43     private volatile int worstResponseTimeMillis = Integer.MIN_VALUE;
44
45
46     /**
47      * Return the date when this object was loaded.
48      */

49     public Date JavaDoc getLoadDate() {
50         return new Date JavaDoc(this.initedMillis);
51     }
52
53     /**
54      * Return the number of hits this object has handled.
55      */

56     public int getAccessCount() {
57         return accessCount;
58     }
59
60     /**
61      * Return the number of milliseconds since this object was loaded.
62      */

63     public long getUptimeMillis() {
64         return System.currentTimeMillis() - this.initedMillis;
65     }
66
67     /**
68      * Return the average response time achieved by this object.
69      */

70     public int getAverageResponseTimeMillis() {
71         // avoid division by 0
72
if (getAccessCount() == 0) {
73             return 0;
74         }
75         return this.totalResponseTimeMillis / getAccessCount();
76     }
77
78     /**
79      * Return the best (lowest) response time achieved by this object.
80      */

81     public int getBestResponseTimeMillis() {
82         return bestResponseTimeMillis;
83     }
84
85     /**
86      * Return the worst (slowest) response time achieved by this object.
87      */

88     public int getWorstResponseTimeMillis() {
89         return worstResponseTimeMillis;
90     }
91
92
93     /**
94      * Utility method to record this response time, updating
95      * the best and worst response times if necessary.
96      * @param responseTimeMillis the response time of this request
97      */

98     public synchronized void recordResponseTime(long responseTimeMillis) {
99         ++this.accessCount;
100         int iResponseTime = (int) responseTimeMillis;
101         this.totalResponseTimeMillis += iResponseTime;
102         if (iResponseTime < this.bestResponseTimeMillis) {
103             this.bestResponseTimeMillis = iResponseTime;
104         }
105         if (iResponseTime > this.worstResponseTimeMillis) {
106             this.worstResponseTimeMillis = iResponseTime;
107         }
108     }
109
110     /**
111      * Return a human-readable string showing the performance
112      * data recorded by this object.
113      */

114     public synchronized String JavaDoc toString() {
115         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
116         sb.append("hits=[").append(getAccessCount()).append("]; ");
117         sb.append("average=[").append(getAverageResponseTimeMillis()).append("ms]; ");
118         sb.append("best=[").append(getBestResponseTimeMillis()).append("ms]; ");
119         sb.append("worst=[").append(getWorstResponseTimeMillis()).append("ms]");
120         return sb.toString();
121     }
122
123 }
124
Popular Tags