KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > performance > Monitor


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57 package performance;
58
59 import java.text.DecimalFormat JavaDoc;
60 import java.util.ArrayList JavaDoc;
61 import java.util.Collections JavaDoc;
62 import java.util.HashMap JavaDoc;
63 import java.util.Iterator JavaDoc;
64
65 /**
66  * Collect monitor point timing statistics.
67  *
68  * @author <a HREF="mailto:antelder@apache.org">Ant Elder</a>
69  */

70 public class Monitor {
71
72    static HashMap JavaDoc timers = new HashMap JavaDoc();
73    
74    public static void start(String JavaDoc s) {
75       Timer t = (Timer)timers.get( s );
76       if ( t == null ) {
77          t = new Timer();
78          timers.put( s, t );
79       }
80       t.start();
81    }
82
83    public static void stop(String JavaDoc s) {
84       Timer t = (Timer)timers.get( s );
85       t.stop();
86    }
87
88    public static void pause(String JavaDoc s) {
89       Timer t = (Timer)timers.get( s );
90       if ( t != null ) {
91          t.pause();
92       }
93    }
94
95    public static void resume(String JavaDoc s) {
96       Timer t = (Timer)timers.get( s );
97       if ( t != null ) {
98          t.resume();
99       }
100    }
101    
102    public static void clear() {
103       timers = new HashMap JavaDoc();
104    }
105
106    public static HashMap JavaDoc getAvgResults() {
107       Timer t;
108       String JavaDoc testName;
109       HashMap JavaDoc results = new HashMap JavaDoc();
110       for (Iterator JavaDoc i = timers.keySet().iterator(); i.hasNext(); ) {
111          testName = (String JavaDoc)i.next();
112          t = (Timer)timers.get( testName );
113          float f1 = t.getAvgTime();
114          Float JavaDoc f = new Float JavaDoc( t.getAvgTime() );
115          results.put( testName, new Float JavaDoc( t.getAvgTime() ) );
116       }
117       return results;
118    }
119
120    public static void printResults() {
121       Timer t;
122       String JavaDoc testName;
123       float value;
124       ArrayList JavaDoc l = new ArrayList JavaDoc();
125       for (Iterator JavaDoc i = timers.keySet().iterator(); i.hasNext(); ) {
126          l.add( i.next() );
127       }
128
129       Collections.sort( l );
130       DecimalFormat JavaDoc formatter = new DecimalFormat JavaDoc("#0.00000");
131
132       System.out.println( "\n=== Monitor Results ===\n" );
133       for (Iterator JavaDoc i = l.iterator(); i.hasNext(); ) {
134          testName = (String JavaDoc)i.next();
135          t = (Timer)timers.get( testName );
136          //value = ((float)Math.round( t.getAvgTime() * 10000 ) / 10000 );
137
System.out.println( pad(testName,40) +
138             " value=" + lpad(formatter.format( t.getAvgTime() ),8) + " msecs" +
139             ", samples= " + lpad( ""+t.getIterations(), 8 ) );
140       }
141       System.out.println();
142     }
143         
144     public static String JavaDoc pad(String JavaDoc s, int pad) {
145        if ( s==null ) s = "";
146        StringBuffer JavaDoc sb = new StringBuffer JavaDoc( pad );
147        for (int i = 0; i < (pad-s.length()); i++) {
148          sb = sb.append( " " );
149        }
150        return s + sb.toString();
151     }
152
153     public static String JavaDoc lpad(String JavaDoc s, int pad) {
154        if ( s==null ) s = "";
155        StringBuffer JavaDoc sb = new StringBuffer JavaDoc( pad );
156        for (int i = 0; i < (pad-s.length()); i++) {
157          sb = sb.append( " " );
158        }
159        return sb.toString() + s;
160     }
161
162 }
163
164 class Timer {
165
166    long start;
167    long interval;
168    long totalTime;
169    int startCount;
170    long minimumInterval, errorInterval;
171    static final long CLOCK_ACCURACY = 10; // 10 milliseconds
172

173    public Timer() {
174        interval = 0;
175        totalTime = 0;
176        startCount = 0;
177        minimumInterval = Long.MAX_VALUE;
178        errorInterval = Long.MAX_VALUE;
179    }
180
181    public void start() {
182        interval = 0;
183        startCount +=1;
184        totalTime += interval;
185        start = System.currentTimeMillis();
186    }
187
188    public void pause() {
189        interval += System.currentTimeMillis() - start;
190        start = 0;
191    }
192
193    public void resume() {
194        start = System.currentTimeMillis();
195    }
196
197    public void stop() {
198        long thisInterval = System.currentTimeMillis() - start;
199        if ( thisInterval > errorInterval ) {
200           startCount-=1; // ignore this sample
201
} else {
202           interval += thisInterval;
203           totalTime += thisInterval;
204           if ( startCount > 1 && minimumInterval > 0 && interval < minimumInterval ) {
205              minimumInterval = interval;
206              errorInterval = minimumInterval + CLOCK_ACCURACY;
207              startCount = 0;
208              totalTime = 0;
209           }
210        }
211    }
212
213    public long getInterval() {
214        return interval;
215    }
216     
217    public float getAvgTime() {
218        return (float)totalTime / startCount;
219    }
220     
221    public int getIterations() {
222        return startCount;
223    }
224     
225 }
Popular Tags