KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > UtilTimer


1 /*
2  * $Id: UtilTimer.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.base.util;
25
26 import java.util.Map JavaDoc;
27 import java.util.HashMap JavaDoc;
28
29 /**
30  * Timer handling utility
31  * Utility class for simple reporting of the progress of a process.
32  * Steps are labelled, and the time between each label (or message)
33  * and the time since the start are reported in each call to timerString.
34  *
35  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
36  * @version $Rev: 5462 $
37  * @since 2.0
38  */

39 public class UtilTimer {
40     
41     public static final String JavaDoc module = UtilTimer.class.getName();
42     protected static Map JavaDoc staticTimers = new HashMap JavaDoc();
43
44     protected String JavaDoc timerName = null;
45     protected String JavaDoc lastMessage = null;
46
47     protected long realStartTime;
48     protected long startTime;
49     protected long lastMessageTime;
50     protected boolean running = false;
51     protected boolean log = false;
52     
53     public static UtilTimer makeTimer() {
54         return new UtilTimer();
55     }
56
57     /** Default constructor. Starts the timer. */
58     public UtilTimer() {
59         this("", true);
60     }
61
62     public UtilTimer(String JavaDoc timerName, boolean start) {
63         this(timerName, start, false);
64     }
65
66     public UtilTimer(String JavaDoc timerName, boolean start, boolean log) {
67         this.timerName = timerName;
68         this.setLog(log);
69         if (start) {
70             this.startTimer();
71         }
72     }
73
74     public void startTimer() {
75         this.lastMessageTime = realStartTime = startTime = System.currentTimeMillis();
76         this.lastMessage = "Begin";
77         this.running = true;
78     }
79
80     public String JavaDoc getName() {
81         return this.timerName;
82     }
83
84     public boolean isRunning() {
85         return this.running;
86     }
87
88     /** Creates a string with information including the passed message, the last passed message and the time since the last call, and the time since the beginning
89      * @param message A message to put into the timer String
90      * @return A String with the timing information, the timer String
91      */

92     public String JavaDoc timerString(String JavaDoc message) {
93         return timerString(message, this.getClass().getName());
94     }
95
96     /** Creates a string with information including the passed message, the last passed message and the time since the last call, and the time since the beginning
97      * @param message A message to put into the timer String
98      * @param module The debug/log module/thread to use, can be null for root module
99      * @return A String with the timing information, the timer String
100      */

101     public String JavaDoc timerString(String JavaDoc message, String JavaDoc module) {
102         // time this call to avoid it interfering with the main timer
103
long tsStart = System.currentTimeMillis();
104
105         String JavaDoc retString = "[[" + message + "- total:" + secondsSinceStart();
106         if (lastMessage != null) {
107             retString += ",since last(" + ((lastMessage.length() > 20) ? (lastMessage.substring(0, 17) + "...") : lastMessage) + "):" +
108                     secondsSinceLast() + "]]";
109         } else {
110             retString += "]]";
111         }
112
113         // append the timer name
114
if (UtilValidate.isNotEmpty(timerName)) {
115             retString = retString + " - '" + timerName + "'";
116         }
117
118         lastMessage = message;
119         if (log) Debug.log(Debug.TIMING, null, retString, module, "org.ofbiz.base.util.UtilTimer");
120
121         // have lastMessageTime come as late as possible to just time what happens between calls
122
lastMessageTime = System.currentTimeMillis();
123         // update startTime to disclude the time this call took
124
startTime += (lastMessageTime - tsStart);
125
126         return retString;
127     }
128
129     /** Returns the number of seconds since the timer started
130      * @return The number of seconds since the timer started
131      */

132     public double secondsSinceStart() {
133         return ((double) timeSinceStart()) / 1000.0;
134     }
135
136     /** Returns the number of seconds since the last time timerString was called
137      * @return The number of seconds since the last time timerString was called
138      */

139     public double secondsSinceLast() {
140         return ((double) timeSinceLast()) / 1000.0;
141     }
142
143     /** Returns the number of milliseconds since the timer started
144      * @return The number of milliseconds since the timer started
145      */

146     public long timeSinceStart() {
147         long currentTime = System.currentTimeMillis();
148
149         return currentTime - startTime;
150     }
151
152     /** Returns the number of milliseconds since the last time timerString was called
153      * @return The number of milliseconds since the last time timerString was called
154      */

155     public long timeSinceLast() {
156         long currentTime = System.currentTimeMillis();
157
158         return currentTime - lastMessageTime;
159     }
160
161     /** Sets the value of the log member, denoting whether log output is off or not
162      * @param log The new value of log
163      */

164     public void setLog(boolean log) {
165         this.log = log;
166     }
167
168     /** Gets the value of the log member, denoting whether log output is off or not
169      * @return The value of log
170      */

171     public boolean getLog() {
172         return log;
173     }
174
175     /** Creates a string with information including the passed message, the time since the last call,
176      * and the time since the beginning. This version allows an integer level to be specified to
177      * improve readability of the output.
178      * @param level Integer specifying how many levels to indent the timer string so the output can be more easily read through nested method calls.
179      * @param message A message to put into the timer String
180      * @return A String with the timing information, the timer String
181      */

182     public String JavaDoc timerString(int level, String JavaDoc message) {
183         // String retString = "[[" + message + ": seconds since start: " + secondsSinceStart() + ",since last(" + lastMessage + "):" + secondsSinceLast() + "]]";
184

185         StringBuffer JavaDoc retStringBuf = new StringBuffer JavaDoc();
186
187         for (int i = 0; i < level; i++) {
188             retStringBuf.append("| ");
189         }
190         retStringBuf.append("(");
191
192         String JavaDoc timeSinceStartStr = String.valueOf(timeSinceStart());
193
194         // int spacecount = 5 - timeSinceStartStr.length();
195
// for (int i=0; i < spacecount; i++) { retStringBuf.append(' '); }
196
retStringBuf.append(timeSinceStartStr + ",");
197
198         String JavaDoc timeSinceLastStr = String.valueOf(timeSinceLast());
199
200         // spacecount = 4 - timeSinceLastStr.length();
201
// for (int i=0; i < spacecount; i++) { retStringBuf.append(' '); }
202
retStringBuf.append(timeSinceLastStr);
203
204         retStringBuf.append(")");
205         int spacecount = 12 + (2 * level) - retStringBuf.length();
206
207         for (int i = 0; i < spacecount; i++) {
208             retStringBuf.append(' ');
209         }
210         retStringBuf.append(message);
211
212         // lastMessageTime = (new Date()).getTime();
213
lastMessageTime = System.currentTimeMillis();
214         // lastMessage = message;
215

216         String JavaDoc retString = retStringBuf.toString();
217
218         // if(!quiet) Debug.logInfo(retString, module);
219
if (log && Debug.timingOn()) Debug.logTiming(retString, module);
220         return retString;
221     }
222
223     // static logging timer - be sure to close the timer when finished!
224

225     public static UtilTimer getTimer(String JavaDoc timerName) {
226         return getTimer(timerName, true);
227     }
228
229     public static UtilTimer getTimer(String JavaDoc timerName, boolean log) {
230         UtilTimer timer = (UtilTimer) staticTimers.get(timerName);
231         if (timer == null) {
232             synchronized(UtilTimer.class) {
233                 timer = (UtilTimer) staticTimers.get(timerName);
234                 if (timer == null) {
235                     timer = new UtilTimer(timerName, false);
236                     timer.setLog(log);
237                     staticTimers.put(timerName, timer);
238                 }
239             }
240         }
241         return timer;
242     }
243
244     public static void timerLog(String JavaDoc timerName, String JavaDoc message, String JavaDoc module) {
245         UtilTimer timer = UtilTimer.getTimer(timerName);
246         if (!timer.isRunning()) {
247             timer.startTimer();
248         }
249
250         if (timer.getLog()) {
251             if (module == null) {
252                 module = timer.getClass().getName();
253             }
254             timer.timerString(message, module);
255         }
256     }
257
258     public static void closeTimer(String JavaDoc timerName) {
259         UtilTimer.closeTimer(timerName, null, null);
260     }
261
262     public static void closeTimer(String JavaDoc timerName, String JavaDoc message) {
263         UtilTimer.closeTimer(timerName, message, null);
264     }
265
266     public static void closeTimer(String JavaDoc timerName, String JavaDoc message, String JavaDoc module) {
267         if (message != null) {
268             UtilTimer.timerLog(timerName, message, module);
269         }
270         synchronized(UtilTimer.class) {
271             staticTimers.remove(timerName);
272         }
273     }
274 }
275
Popular Tags