KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > util > TimeMeasure


1 /*
2  * $Id: TimeMeasure.java,v 1.4 2004/12/01 07:54:30 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.util;
15
16 import java.text.MessageFormat JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 /*
21  * Ein Klasse, die Zeitmessungen aufnimmt und diese in Relation zueineander
22  * setzt. Zum Start der Zeitmessung Methode start kurzer Beschreibung was
23  * gemessen wird als Parameter aufrufen. Das Ende der Zeitmessung wird durch
24  * stop angezeigt. Es kann nur eine Zeitmessung gleichzeitig stattfinden. Die
25  * Ausgabe ist nicht sortiert, gibt aber die relativen Unterschiede in der
26  * Dauer der einzelnen Messungen an. Die Messung mit der laengsten Dauer ist
27  * der Referenzwert (1.0).
28  */

29
30 /**
31  * Some simple stop watch. It allows to measure multiple time periods
32  * and prints them. Usage: call start(comment) and stop() for
33  * each period of time.
34  *
35  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
36  * @version $Revision: 1.4 $
37  */

38 public class TimeMeasure {
39     protected final static double RESOLUTION = 100.0;
40
41     /**
42      * List of measurements.
43      */

44     protected final ArrayList JavaDoc measures;
45
46     /**
47      * Message formatter
48      */

49     protected final MessageFormat JavaDoc formatter;
50
51     /**
52      * the current time measurement.
53      */

54     protected Measure current;
55
56     /**
57      * Simple TimeMesaure with default format.
58      */

59     public TimeMeasure() {
60         this(new MessageFormat JavaDoc("{0}\t: {1}\t {2}x\n"));
61     }
62
63     /**
64      * A new TimeMeasure which reports in a specific format. The
65      * format is a standard MessageFormat with the following variables:
66      * <ul>
67      * <li><code>{0}</code> the measurement comment</li>
68      * <li><code>{1}</code> the time it took</li>
69      * <li><code>{2}</code> how many times this is faster than the
70      * slowest measurement</li>
71      * </ul>
72      */

73     public TimeMeasure(MessageFormat JavaDoc formatter) {
74         this.measures = new ArrayList JavaDoc();
75         this.formatter = formatter;
76     }
77
78     /**
79      * Reset of all Measurements.
80      */

81     public void reset() {
82         measures.clear();
83     }
84
85     /*
86      * Startet eine neue Messsung.
87      * @param comment Die Beschreibung der Messung.
88      */

89     public void start(String JavaDoc comment) {
90         current = new Measure(comment);
91     }
92
93     /*
94      * Startet eine neue Messsung.
95      * @param comment Die Beschreibung der Messung.
96     public Object generate(String comment) {
97         current = new Measure();
98         actual.comment = comment;
99         measures.add(actual);
100         return actual;
101     }
102      */

103
104     /*
105      * Addiert eine Messsung zu einer bestehenden.
106      * @param comment Die Beschreibung der Messung.
107     public void addToMeasure(Object measure) {
108         int index = measures.indexOf(measure);
109         if ( index<0 ) {
110             System.err.println("Measure does not exists " + measure);
111             actual = null;
112             return;
113         }
114
115         actual = (Measure)measures.get(index);
116         measures.remove(index);
117
118         actual.start = System.currentTimeMillis();
119     }
120      */

121
122     /**
123      * stop current time measurement and store it.
124      */

125     public void stop() {
126         if (current != null) {
127             current.stop();
128             measures.add(current);
129             current = null;
130         }
131     }
132
133     /**
134      * determines the time duration of the longest or shortest time interval.
135      *
136      * @param findShortest boolean 'true', if we are looking for the shortest
137      * time interval; 'false' if we are looking for the
138      * longest.
139      */

140     private long findReferenceValue(boolean findShortest) {
141         long result = findShortest ? Long.MAX_VALUE : -1;
142
143         Iterator JavaDoc it = measures.iterator();
144         while (it.hasNext()) {
145             Measure m = (Measure) it.next();
146             result = (findShortest
147                     ? Math.min(result, m.getDuration())
148                     : Math.max(result, m.getDuration()));
149         }
150         return result;
151     }
152
153     public String JavaDoc print() {
154         return print(false);
155     }
156
157     /**
158      * creates a formatted output (using the MessageFormat) of all
159      * results. The output is sorted in the in the sequence the time
160      * measurements took place.
161      * Writes the relative time to either the shortest or the longest
162      * time interval.
163      *
164      * @param shortestIsReference boolean true, if the shortest time interval
165      * is the reference value (1.0). False, if the
166      * longest is the reference.
167      */

168     public String JavaDoc print(boolean shortestIsReference) {
169         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
170         long reference = findReferenceValue(shortestIsReference);
171         Iterator JavaDoc it = measures.iterator();
172         while (it.hasNext()) {
173             Measure m = (Measure) it.next();
174             String JavaDoc factor = " -- ";
175             long duration = m.getDuration();
176             if (reference > 0) {
177                 long tmp = (long) ((duration * RESOLUTION) / reference);
178                 factor = String.valueOf(tmp / RESOLUTION);
179             }
180             Object JavaDoc[] args = {m.getComment(), (duration + "ms"), factor};
181             result.append(formatter.format(args));
182         }
183         return result.toString();
184     }
185
186     public String JavaDoc toString() {
187         return print();
188     }
189
190     /**
191      * A class to store one period of time.
192      */

193     private final static class Measure {
194         /**
195          * start time.
196          */

197         private final long start;
198
199         /**
200          * stop time.
201          */

202         private long stop;
203
204         /**
205          * Die Gesamtdauer der Messung
206          */

207         private long duration;
208
209         /**
210          * Description.
211          */

212         private String JavaDoc comment;
213
214         public Measure(String JavaDoc comment) {
215             start = System.currentTimeMillis();
216             this.comment = comment;
217         }
218
219         public void stop() {
220             stop = System.currentTimeMillis();
221             duration = stop - start;
222         }
223
224         public long getDuration() { return duration; }
225
226         public String JavaDoc getComment() { return comment; }
227     }
228 }
229
230
231
Popular Tags