1 16 17 package org.springframework.util; 18 19 import java.text.NumberFormat ; 20 import java.util.LinkedList ; 21 import java.util.List ; 22 23 40 public class StopWatch { 41 42 47 private final String id; 48 49 private boolean keepTaskList = true; 50 51 52 private final List taskList = new LinkedList (); 53 54 55 private long startTimeMillis; 56 57 58 private boolean running; 59 60 61 private String currentTaskName; 62 63 private TaskInfo lastTaskInfo; 64 65 private int taskCount; 66 67 68 private long totalTimeMillis; 69 70 71 74 public StopWatch() { 75 this.id = ""; 76 } 77 78 85 public StopWatch(String id) { 86 this.id = id; 87 } 88 89 94 public void setKeepTaskList(boolean keepTaskList) { 95 this.keepTaskList = keepTaskList; 96 } 97 98 99 104 public void start() throws IllegalStateException { 105 start(""); 106 } 107 108 114 public void start(String taskName) throws IllegalStateException { 115 if (this.running) { 116 throw new IllegalStateException ("Can't start StopWatch: it's already running"); 117 } 118 this.startTimeMillis = System.currentTimeMillis(); 119 this.running = true; 120 this.currentTaskName = taskName; 121 } 122 123 129 public void stop() throws IllegalStateException { 130 if (!this.running) { 131 throw new IllegalStateException ("Can't stop StopWatch: it's not running"); 132 } 133 long lastTime = System.currentTimeMillis() - this.startTimeMillis; 134 this.totalTimeMillis += lastTime; 135 this.lastTaskInfo = new TaskInfo(this.currentTaskName, lastTime); 136 if (this.keepTaskList) { 137 this.taskList.add(lastTaskInfo); 138 } 139 ++this.taskCount; 140 this.running = false; 141 this.currentTaskName = null; 142 } 143 144 147 public boolean isRunning() { 148 return this.running; 149 } 150 151 152 155 public long getLastTaskTimeMillis() throws IllegalStateException { 156 if (this.lastTaskInfo == null) { 157 throw new IllegalStateException ("No tests run: can't get last interval"); 158 } 159 return this.lastTaskInfo.getTimeMillis(); 160 } 161 162 165 public long getTotalTimeMillis() { 166 return totalTimeMillis; 167 } 168 169 172 public double getTotalTimeSeconds() { 173 return totalTimeMillis / 1000.0; 174 } 175 176 179 public int getTaskCount() { 180 return taskCount; 181 } 182 183 186 public TaskInfo[] getTaskInfo() { 187 if (!this.keepTaskList) { 188 throw new UnsupportedOperationException ("Task info is not being kept!"); 189 } 190 return (TaskInfo[]) this.taskList.toArray(new TaskInfo[this.taskList.size()]); 191 } 192 193 194 197 public String shortSummary() { 198 return "StopWatch '" + this.id + "': running time (millis) = " + getTotalTimeMillis(); 199 } 200 201 205 public String prettyPrint() { 206 StringBuffer sb = new StringBuffer (shortSummary()); 207 sb.append('\n'); 208 if (!this.keepTaskList) { 209 sb.append("No task info kept"); 210 } 211 else { 212 TaskInfo[] tasks = getTaskInfo(); 213 sb.append("-----------------------------------------\n"); 214 sb.append("ms % Task name\n"); 215 sb.append("-----------------------------------------\n"); 216 NumberFormat nf = NumberFormat.getNumberInstance(); 217 nf.setMinimumIntegerDigits(5); 218 nf.setGroupingUsed(false); 219 NumberFormat pf = NumberFormat.getPercentInstance(); 220 pf.setMinimumIntegerDigits(3); 221 pf.setGroupingUsed(false); 222 for (int i = 0; i < tasks.length; i++) { 223 sb.append(nf.format(tasks[i].getTimeMillis()) + " "); 224 sb.append(pf.format(tasks[i].getTimeSeconds() / getTotalTimeSeconds()) + " "); 225 sb.append(tasks[i].getTaskName() + "\n"); 226 } 227 } 228 return sb.toString(); 229 } 230 231 235 public String toString() { 236 StringBuffer sb = new StringBuffer (shortSummary()); 237 if (this.keepTaskList) { 238 TaskInfo[] tasks = getTaskInfo(); 239 for (int i = 0; i < tasks.length; i++) { 240 sb.append("; [" + tasks[i].getTaskName() + "] took " + tasks[i].getTimeMillis()); 241 long percent = Math.round((100.0 * tasks[i].getTimeSeconds()) / getTotalTimeSeconds()); 242 sb.append(" = " + percent + "%"); 243 } 244 } 245 else { 246 sb.append("; no task info kept"); 247 } 248 return sb.toString(); 249 } 250 251 252 255 public static class TaskInfo { 256 257 private final String taskName; 258 259 private final long timeMillis; 260 261 private TaskInfo(String taskName, long timeMillis) { 262 this.taskName = taskName; 263 this.timeMillis = timeMillis; 264 } 265 266 269 public String getTaskName() { 270 return taskName; 271 } 272 273 276 public long getTimeMillis() { 277 return timeMillis; 278 } 279 280 283 public double getTimeSeconds() { 284 return timeMillis / 1000.0; 285 } 286 } 287 288 } 289 | Popular Tags |