1 24 package org.ofbiz.base.util; 25 26 import java.util.Map ; 27 import java.util.HashMap ; 28 29 39 public class UtilTimer { 40 41 public static final String module = UtilTimer.class.getName(); 42 protected static Map staticTimers = new HashMap (); 43 44 protected String timerName = null; 45 protected String 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 58 public UtilTimer() { 59 this("", true); 60 } 61 62 public UtilTimer(String timerName, boolean start) { 63 this(timerName, start, false); 64 } 65 66 public UtilTimer(String 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 getName() { 81 return this.timerName; 82 } 83 84 public boolean isRunning() { 85 return this.running; 86 } 87 88 92 public String timerString(String message) { 93 return timerString(message, this.getClass().getName()); 94 } 95 96 101 public String timerString(String message, String module) { 102 long tsStart = System.currentTimeMillis(); 104 105 String 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 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 lastMessageTime = System.currentTimeMillis(); 123 startTime += (lastMessageTime - tsStart); 125 126 return retString; 127 } 128 129 132 public double secondsSinceStart() { 133 return ((double) timeSinceStart()) / 1000.0; 134 } 135 136 139 public double secondsSinceLast() { 140 return ((double) timeSinceLast()) / 1000.0; 141 } 142 143 146 public long timeSinceStart() { 147 long currentTime = System.currentTimeMillis(); 148 149 return currentTime - startTime; 150 } 151 152 155 public long timeSinceLast() { 156 long currentTime = System.currentTimeMillis(); 157 158 return currentTime - lastMessageTime; 159 } 160 161 164 public void setLog(boolean log) { 165 this.log = log; 166 } 167 168 171 public boolean getLog() { 172 return log; 173 } 174 175 182 public String timerString(int level, String message) { 183 185 StringBuffer retStringBuf = new StringBuffer (); 186 187 for (int i = 0; i < level; i++) { 188 retStringBuf.append("| "); 189 } 190 retStringBuf.append("("); 191 192 String timeSinceStartStr = String.valueOf(timeSinceStart()); 193 194 retStringBuf.append(timeSinceStartStr + ","); 197 198 String timeSinceLastStr = String.valueOf(timeSinceLast()); 199 200 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 = System.currentTimeMillis(); 214 216 String retString = retStringBuf.toString(); 217 218 if (log && Debug.timingOn()) Debug.logTiming(retString, module); 220 return retString; 221 } 222 223 225 public static UtilTimer getTimer(String timerName) { 226 return getTimer(timerName, true); 227 } 228 229 public static UtilTimer getTimer(String 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 timerName, String message, String 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 timerName) { 259 UtilTimer.closeTimer(timerName, null, null); 260 } 261 262 public static void closeTimer(String timerName, String message) { 263 UtilTimer.closeTimer(timerName, message, null); 264 } 265 266 public static void closeTimer(String timerName, String message, String 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 |