001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.util;
014:
015: import java.text.MessageFormat;
016: import java.util.ArrayList;
017: import java.util.Iterator;
018:
019: /*
020: * Ein Klasse, die Zeitmessungen aufnimmt und diese in Relation zueineander
021: * setzt. Zum Start der Zeitmessung Methode start kurzer Beschreibung was
022: * gemessen wird als Parameter aufrufen. Das Ende der Zeitmessung wird durch
023: * stop angezeigt. Es kann nur eine Zeitmessung gleichzeitig stattfinden. Die
024: * Ausgabe ist nicht sortiert, gibt aber die relativen Unterschiede in der
025: * Dauer der einzelnen Messungen an. Die Messung mit der laengsten Dauer ist
026: * der Referenzwert (1.0).
027: */
028:
029: /**
030: * Some simple stop watch. It allows to measure multiple time periods
031: * and prints them. Usage: call start(comment) and stop() for
032: * each period of time.
033: *
034: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
035: */
036: public class TimeMeasure {
037: protected final static double RESOLUTION = 100.0;
038:
039: /**
040: * List of measurements.
041: */
042: protected final ArrayList measures;
043:
044: /**
045: * Message formatter
046: */
047: protected final MessageFormat formatter;
048:
049: /**
050: * the current time measurement.
051: */
052: protected Measure current;
053:
054: /**
055: * Simple TimeMesaure with default format.
056: */
057: public TimeMeasure() {
058: this (new MessageFormat("{0}\t: {1}\t {2}x\n"));
059: }
060:
061: /**
062: * A new TimeMeasure which reports in a specific format. The
063: * format is a standard MessageFormat with the following variables:
064: * <ul>
065: * <li><code>{0}</code> the measurement comment</li>
066: * <li><code>{1}</code> the time it took</li>
067: * <li><code>{2}</code> how many times this is faster than the
068: * slowest measurement</li>
069: * </ul>
070: */
071: public TimeMeasure(MessageFormat formatter) {
072: this .measures = new ArrayList();
073: this .formatter = formatter;
074: }
075:
076: /**
077: * Reset of all Measurements.
078: */
079: public void reset() {
080: measures.clear();
081: }
082:
083: /*
084: * Startet eine neue Messsung.
085: * @param comment Die Beschreibung der Messung.
086: */
087: public void start(String comment) {
088: current = new Measure(comment);
089: }
090:
091: /*
092: * Startet eine neue Messsung.
093: * @param comment Die Beschreibung der Messung.
094: public Object generate(String comment) {
095: current = new Measure();
096: actual.comment = comment;
097: measures.add(actual);
098: return actual;
099: }
100: */
101:
102: /*
103: * Addiert eine Messsung zu einer bestehenden.
104: * @param comment Die Beschreibung der Messung.
105: public void addToMeasure(Object measure) {
106: int index = measures.indexOf(measure);
107: if ( index<0 ) {
108: System.err.println("Measure does not exists " + measure);
109: actual = null;
110: return;
111: }
112:
113: actual = (Measure)measures.get(index);
114: measures.remove(index);
115:
116: actual.start = System.currentTimeMillis();
117: }
118: */
119:
120: /**
121: * stop current time measurement and store it.
122: */
123: public void stop() {
124: if (current != null) {
125: current.stop();
126: measures.add(current);
127: current = null;
128: }
129: }
130:
131: /**
132: * determines the time duration of the longest or shortest time interval.
133: *
134: * @param findShortest boolean 'true', if we are looking for the shortest
135: * time interval; 'false' if we are looking for the
136: * longest.
137: */
138: private long findReferenceValue(boolean findShortest) {
139: long result = findShortest ? Long.MAX_VALUE : -1;
140:
141: Iterator it = measures.iterator();
142: while (it.hasNext()) {
143: Measure m = (Measure) it.next();
144: result = (findShortest ? Math.min(result, m.getDuration())
145: : Math.max(result, m.getDuration()));
146: }
147: return result;
148: }
149:
150: public String print() {
151: return print(false);
152: }
153:
154: /**
155: * creates a formatted output (using the MessageFormat) of all
156: * results. The output is sorted in the in the sequence the time
157: * measurements took place.
158: * Writes the relative time to either the shortest or the longest
159: * time interval.
160: *
161: * @param shortestIsReference boolean true, if the shortest time interval
162: * is the reference value (1.0). False, if the
163: * longest is the reference.
164: */
165: public String print(boolean shortestIsReference) {
166: SStringBuilder result = new SStringBuilder();
167: long reference = findReferenceValue(shortestIsReference);
168: Iterator it = measures.iterator();
169: while (it.hasNext()) {
170: Measure m = (Measure) it.next();
171: String factor = " -- ";
172: long duration = m.getDuration();
173: if (reference > 0) {
174: long tmp = (long) ((duration * RESOLUTION) / reference);
175: factor = String.valueOf(tmp / RESOLUTION);
176: }
177: Object[] args = { m.getComment(), (duration + "ms"), factor };
178: result.append(formatter.format(args));
179: }
180: return result.toString();
181: }
182:
183: public String toString() {
184: return print();
185: }
186:
187: /**
188: * A class to store one period of time.
189: */
190: private final static class Measure {
191: /**
192: * start time.
193: */
194: private final long start;
195:
196: /**
197: * stop time.
198: */
199: private long stop;
200:
201: /**
202: * Die Gesamtdauer der Messung
203: */
204: private long duration;
205:
206: /**
207: * Description.
208: */
209: private String comment;
210:
211: public Measure(String comment) {
212: start = System.currentTimeMillis();
213: this .comment = comment;
214: }
215:
216: public void stop() {
217: stop = System.currentTimeMillis();
218: duration = stop - start;
219: }
220:
221: public long getDuration() {
222: return duration;
223: }
224:
225: public String getComment() {
226: return comment;
227: }
228: }
229: }
|