001: /*
002: * $Id: UtilTimer.java,v 1.3 2003/08/20 23:46:03 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.base.util;
025:
026: /**
027: * Timer handling utility
028: * Utility class for simple reporting of the progress of a process.
029: * Steps are labelled, and the time between each label (or message)
030: * and the time since the start are reported in each call to timerString.
031: *
032: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
033: * @version $Revision: 1.3 $
034: * @since 2.0
035: */
036: public class UtilTimer {
037:
038: public static final String module = UtilTimer.class.getName();
039:
040: long realStartTime;
041: long startTime;
042: long lastMessageTime;
043: String lastMessage = null;
044: boolean log = false;
045:
046: /** Default constructor. Starts the timer.
047: */
048: public UtilTimer() {
049: lastMessageTime = realStartTime = startTime = System
050: .currentTimeMillis();
051: lastMessage = "Begin";
052: }
053:
054: /** 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
055: * @param message A message to put into the timer String
056: * @return A String with the timing information, the timer String
057: */
058: public String timerString(String message) {
059: return timerString(message, this .getClass().getName());
060: }
061:
062: /** 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
063: * @param message A message to put into the timer String
064: * @param module The debug/log module/thread to use, can be null for root module
065: * @return A String with the timing information, the timer String
066: */
067: public String timerString(String message, String module) {
068: // time this call to avoid it interfering with the main timer
069: long tsStart = System.currentTimeMillis();
070:
071: String retString = "[["
072: + message
073: + "- total:"
074: + secondsSinceStart()
075: + ",since last("
076: + ((lastMessage.length() > 20) ? (lastMessage
077: .substring(0, 17) + "...") : lastMessage)
078: + "):" + secondsSinceLast() + "]]";
079:
080: lastMessage = message;
081: if (log)
082: Debug.log(Debug.TIMING, null, retString, module,
083: "org.ofbiz.base.util.UtilTimer");
084:
085: // have lastMessageTime come as late as possible to just time what happens between calls
086: lastMessageTime = System.currentTimeMillis();
087: // update startTime to disclude the time this call took
088: startTime += (lastMessageTime - tsStart);
089:
090: return retString;
091: }
092:
093: /** Returns the number of seconds since the timer started
094: * @return The number of seconds since the timer started
095: */
096: public double secondsSinceStart() {
097: return ((double) timeSinceStart()) / 1000.0;
098: }
099:
100: /** Returns the number of seconds since the last time timerString was called
101: * @return The number of seconds since the last time timerString was called
102: */
103: public double secondsSinceLast() {
104: return ((double) timeSinceLast()) / 1000.0;
105: }
106:
107: /** Returns the number of milliseconds since the timer started
108: * @return The number of milliseconds since the timer started
109: */
110: public long timeSinceStart() {
111: long currentTime = System.currentTimeMillis();
112:
113: return currentTime - startTime;
114: }
115:
116: /** Returns the number of milliseconds since the last time timerString was called
117: * @return The number of milliseconds since the last time timerString was called
118: */
119: public long timeSinceLast() {
120: long currentTime = System.currentTimeMillis();
121:
122: return currentTime - lastMessageTime;
123: }
124:
125: /** Sets the value of the log member, denoting whether log output is off or not
126: * @param log The new value of log
127: */
128: public void setLog(boolean log) {
129: this .log = log;
130: }
131:
132: /** Gets the value of the log member, denoting whether log output is off or not
133: * @return The value of log
134: */
135: public boolean getLog() {
136: return log;
137: }
138:
139: /** Creates a string with information including the passed message, the time since the last call,
140: * and the time since the beginning. This version allows an integer level to be specified to
141: * improve readability of the output.
142: * @param level Integer specifying how many levels to indent the timer string so the output can be more easily read through nested method calls.
143: * @param message A message to put into the timer String
144: * @return A String with the timing information, the timer String
145: */
146: public String timerString(int level, String message) {
147: // String retString = "[[" + message + ": seconds since start: " + secondsSinceStart() + ",since last(" + lastMessage + "):" + secondsSinceLast() + "]]";
148:
149: StringBuffer retStringBuf = new StringBuffer();
150:
151: for (int i = 0; i < level; i++) {
152: retStringBuf.append("| ");
153: }
154: retStringBuf.append("(");
155:
156: String timeSinceStartStr = String.valueOf(timeSinceStart());
157:
158: // int spacecount = 5 - timeSinceStartStr.length();
159: // for (int i=0; i < spacecount; i++) { retStringBuf.append(' '); }
160: retStringBuf.append(timeSinceStartStr + ",");
161:
162: String timeSinceLastStr = String.valueOf(timeSinceLast());
163:
164: // spacecount = 4 - timeSinceLastStr.length();
165: // for (int i=0; i < spacecount; i++) { retStringBuf.append(' '); }
166: retStringBuf.append(timeSinceLastStr);
167:
168: retStringBuf.append(")");
169: int spacecount = 12 + (2 * level) - retStringBuf.length();
170:
171: for (int i = 0; i < spacecount; i++) {
172: retStringBuf.append(' ');
173: }
174: retStringBuf.append(message);
175:
176: // lastMessageTime = (new Date()).getTime();
177: lastMessageTime = System.currentTimeMillis();
178: // lastMessage = message;
179:
180: String retString = retStringBuf.toString();
181:
182: // if(!quiet) Debug.logInfo(retString, module);
183: if (Debug.infoOn())
184: Debug.logInfo(retString, module);
185: return retString;
186: }
187:
188: }
|