001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.util;
018:
019: import java.util.Date;
020:
021: /**
022: * Implementation of ResponseTimeMonitor for use via delegation by
023: * objects that implement this interface.
024: *
025: * @author Rod Johnson
026: * @since November 21, 2000
027: */
028: public class ResponseTimeMonitorImpl implements ResponseTimeMonitor {
029:
030: /** The system time at which this object was initialized */
031: private final long initedMillis = System.currentTimeMillis();
032:
033: /** The number of operations recorded by this object */
034: private volatile int accessCount;
035:
036: /** The sum of the response times for all operations */
037: private volatile int totalResponseTimeMillis = 0;
038:
039: /** The best response time this object has recorded */
040: private volatile int bestResponseTimeMillis = Integer.MAX_VALUE;
041:
042: /** The worst response time this object has recorded */
043: private volatile int worstResponseTimeMillis = Integer.MIN_VALUE;
044:
045: /**
046: * Return the date when this object was loaded.
047: */
048: public Date getLoadDate() {
049: return new Date(this .initedMillis);
050: }
051:
052: /**
053: * Return the number of hits this object has handled.
054: */
055: public int getAccessCount() {
056: return accessCount;
057: }
058:
059: /**
060: * Return the number of milliseconds since this object was loaded.
061: */
062: public long getUptimeMillis() {
063: return System.currentTimeMillis() - this .initedMillis;
064: }
065:
066: /**
067: * Return the average response time achieved by this object.
068: */
069: public int getAverageResponseTimeMillis() {
070: // avoid division by 0
071: if (getAccessCount() == 0) {
072: return 0;
073: }
074: return this .totalResponseTimeMillis / getAccessCount();
075: }
076:
077: /**
078: * Return the best (lowest) response time achieved by this object.
079: */
080: public int getBestResponseTimeMillis() {
081: return bestResponseTimeMillis;
082: }
083:
084: /**
085: * Return the worst (slowest) response time achieved by this object.
086: */
087: public int getWorstResponseTimeMillis() {
088: return worstResponseTimeMillis;
089: }
090:
091: /**
092: * Utility method to record this response time, updating
093: * the best and worst response times if necessary.
094: * @param responseTimeMillis the response time of this request
095: */
096: public synchronized void recordResponseTime(long responseTimeMillis) {
097: ++this .accessCount;
098: int iResponseTime = (int) responseTimeMillis;
099: this .totalResponseTimeMillis += iResponseTime;
100: if (iResponseTime < this .bestResponseTimeMillis) {
101: this .bestResponseTimeMillis = iResponseTime;
102: }
103: if (iResponseTime > this .worstResponseTimeMillis) {
104: this .worstResponseTimeMillis = iResponseTime;
105: }
106: }
107:
108: /**
109: * Return a human-readable string showing the performance
110: * data recorded by this object.
111: */
112: public synchronized String toString() {
113: StringBuffer sb = new StringBuffer();
114: sb.append("hits=[").append(getAccessCount()).append("]; ");
115: sb.append("average=[").append(getAverageResponseTimeMillis())
116: .append("ms]; ");
117: sb.append("best=[").append(getBestResponseTimeMillis()).append(
118: "ms]; ");
119: sb.append("worst=[").append(getWorstResponseTimeMillis())
120: .append("ms]");
121: return sb.toString();
122: }
123:
124: }
|