001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.management.j2ee.statistics;
023:
024: import javax.management.j2ee.statistics.TimeStatistic;
025:
026: /**
027: * Time Statisitic Container for JBoss.
028: *
029: * @author <a href="mailto:marc.fleury@jboss.org">Marc Fleury</a>
030: * @author <a href="mailto:andreas@jboss.com">Andreas Schaefer</a>
031: * @version $Revision: 57197 $
032: */
033: public class TimeStatisticImpl extends StatisticImpl implements
034: TimeStatistic {
035: // -------------------------------------------------------------------------
036: // Constants
037: // -------------------------------------------------------------------------
038:
039: /** @since 4.0.2 */
040: private static final long serialVersionUID = -3508391696541148001L;
041:
042: // -------------------------------------------------------------------------
043: // Members
044: // -------------------------------------------------------------------------
045:
046: protected long count;
047: protected long minTime;
048: protected long maxTime;
049: protected long totalTime;
050: protected double requestRate;
051:
052: private long start;
053:
054: // -------------------------------------------------------------------------
055: // Constructors
056: // -------------------------------------------------------------------------
057:
058: /**
059: * Create a TimeStatistic
060: *
061: * @param name the name of the state
062: * @param units the units of the stat
063: * @param description a description of the stat
064: */
065: public TimeStatisticImpl(String name, String units,
066: String description) {
067: super (name, units, description);
068: start = System.currentTimeMillis();
069: }
070:
071: // -------------------------------------------------------------------------
072: // CountStatistic Implementation
073: // -------------------------------------------------------------------------
074:
075: /**
076: * @return The number of times a time measurements was added
077: */
078: public long getCount() {
079: return count;
080: }
081:
082: /**
083: * @return The minimum time added since start of the measurements
084: */
085: public long getMinTime() {
086: return minTime;
087: }
088:
089: /**
090: * @return The maximum time added since start of the measurements
091: */
092: public long getMaxTime() {
093: return maxTime;
094: }
095:
096: /**
097: * @return The sum of all the time added to the measurements since
098: * it started
099: */
100: public long getTotalTime() {
101: return totalTime;
102: }
103:
104: /**
105: * @return The request rate which is the number of counts divided by
106: * the time elapsed since the time measurements started
107: */
108: public double getRequestRate() {
109: return requestRate;
110: }
111:
112: /**
113: * @return Debug Information about this instance
114: */
115: public String toString() {
116: return "[ " + "Count: " + getCount() + ", Min. Time: "
117: + getMinTime() + ", Max. Time: " + getMaxTime()
118: + ", Total Time: " + getTotalTime()
119: + ", Request Rate: " + getRequestRate() + ", "
120: + super .toString() + " ]";
121: }
122:
123: // -------------------------------------------------------------------------
124: // Methods
125: // -------------------------------------------------------------------------
126:
127: /**
128: * Adds a Statistic Information about the elapsed time an action
129: * observed by this instance took.
130: *
131: * @param pTime Time elapsed to added to a statistics
132: */
133: public void add(long pTime) {
134: count++;
135: if (pTime == 0) {
136: minTime = 1;
137: }
138: if (minTime == 0) {
139: minTime = pTime;
140: }
141: minTime = pTime < minTime ? pTime : minTime;
142: maxTime = pTime > maxTime ? pTime : maxTime;
143: totalTime += pTime;
144: requestRate = (System.currentTimeMillis() - start) / count;
145: }
146:
147: /**
148: * Resets the statistics to the initial values
149: */
150: public void reset() {
151: count = 0;
152: minTime = 0;
153: maxTime = 0;
154: totalTime = 0;
155: requestRate = 0;
156: super .reset();
157: }
158:
159: /**
160: * Set all TimeStatistic values.
161: *
162: * @param count the invocation count
163: * @param minTime the min time for an invocation
164: * @param maxTime the max time for an invocation
165: * @param totalTime the total time for all invocations
166: */
167: public void set(long count, long minTime, long maxTime,
168: long totalTime) {
169: this .count = count;
170: this .minTime = minTime;
171: this .maxTime = maxTime;
172: this .totalTime = totalTime;
173: if (count == 0)
174: this.requestRate = Double.NaN;
175: else
176: this.requestRate = totalTime / count;
177: }
178: }
|