001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "WSIF" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 2001, 2002, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057: package performance;
058:
059: import java.text.DecimalFormat;
060: import java.util.ArrayList;
061: import java.util.Collections;
062: import java.util.HashMap;
063: import java.util.Iterator;
064:
065: /**
066: * Collect monitor point timing statistics.
067: *
068: * @author <a href="mailto:antelder@apache.org">Ant Elder</a>
069: */
070: public class Monitor {
071:
072: static HashMap timers = new HashMap();
073:
074: public static void start(String s) {
075: Timer t = (Timer) timers.get(s);
076: if (t == null) {
077: t = new Timer();
078: timers.put(s, t);
079: }
080: t.start();
081: }
082:
083: public static void stop(String s) {
084: Timer t = (Timer) timers.get(s);
085: t.stop();
086: }
087:
088: public static void pause(String s) {
089: Timer t = (Timer) timers.get(s);
090: if (t != null) {
091: t.pause();
092: }
093: }
094:
095: public static void resume(String s) {
096: Timer t = (Timer) timers.get(s);
097: if (t != null) {
098: t.resume();
099: }
100: }
101:
102: public static void clear() {
103: timers = new HashMap();
104: }
105:
106: public static HashMap getAvgResults() {
107: Timer t;
108: String testName;
109: HashMap results = new HashMap();
110: for (Iterator i = timers.keySet().iterator(); i.hasNext();) {
111: testName = (String) i.next();
112: t = (Timer) timers.get(testName);
113: float f1 = t.getAvgTime();
114: Float f = new Float(t.getAvgTime());
115: results.put(testName, new Float(t.getAvgTime()));
116: }
117: return results;
118: }
119:
120: public static void printResults() {
121: Timer t;
122: String testName;
123: float value;
124: ArrayList l = new ArrayList();
125: for (Iterator i = timers.keySet().iterator(); i.hasNext();) {
126: l.add(i.next());
127: }
128:
129: Collections.sort(l);
130: DecimalFormat formatter = new DecimalFormat("#0.00000");
131:
132: System.out.println("\n=== Monitor Results ===\n");
133: for (Iterator i = l.iterator(); i.hasNext();) {
134: testName = (String) i.next();
135: t = (Timer) timers.get(testName);
136: //value = ((float)Math.round( t.getAvgTime() * 10000 ) / 10000 );
137: System.out.println(pad(testName, 40) + " value="
138: + lpad(formatter.format(t.getAvgTime()), 8)
139: + " msecs" + ", samples= "
140: + lpad("" + t.getIterations(), 8));
141: }
142: System.out.println();
143: }
144:
145: public static String pad(String s, int pad) {
146: if (s == null)
147: s = "";
148: StringBuffer sb = new StringBuffer(pad);
149: for (int i = 0; i < (pad - s.length()); i++) {
150: sb = sb.append(" ");
151: }
152: return s + sb.toString();
153: }
154:
155: public static String lpad(String s, int pad) {
156: if (s == null)
157: s = "";
158: StringBuffer sb = new StringBuffer(pad);
159: for (int i = 0; i < (pad - s.length()); i++) {
160: sb = sb.append(" ");
161: }
162: return sb.toString() + s;
163: }
164:
165: }
166:
167: class Timer {
168:
169: long start;
170: long interval;
171: long totalTime;
172: int startCount;
173: long minimumInterval, errorInterval;
174: static final long CLOCK_ACCURACY = 10; // 10 milliseconds
175:
176: public Timer() {
177: interval = 0;
178: totalTime = 0;
179: startCount = 0;
180: minimumInterval = Long.MAX_VALUE;
181: errorInterval = Long.MAX_VALUE;
182: }
183:
184: public void start() {
185: interval = 0;
186: startCount += 1;
187: totalTime += interval;
188: start = System.currentTimeMillis();
189: }
190:
191: public void pause() {
192: interval += System.currentTimeMillis() - start;
193: start = 0;
194: }
195:
196: public void resume() {
197: start = System.currentTimeMillis();
198: }
199:
200: public void stop() {
201: long this Interval = System.currentTimeMillis() - start;
202: if (this Interval > errorInterval) {
203: startCount -= 1; // ignore this sample
204: } else {
205: interval += this Interval;
206: totalTime += this Interval;
207: if (startCount > 1 && minimumInterval > 0
208: && interval < minimumInterval) {
209: minimumInterval = interval;
210: errorInterval = minimumInterval + CLOCK_ACCURACY;
211: startCount = 0;
212: totalTime = 0;
213: }
214: }
215: }
216:
217: public long getInterval() {
218: return interval;
219: }
220:
221: public float getAvgTime() {
222: return (float) totalTime / startCount;
223: }
224:
225: public int getIterations() {
226: return startCount;
227: }
228:
229: }
|