1 16 package com.ibatis.common.util; 17 18 import com.ibatis.common.logging.Log; 19 import com.ibatis.common.logging.LogFactory; 20 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 28 public class Stopwatch { 29 30 private static final Log log = LogFactory.getLog(Stopwatch.class); 31 32 private Map taskMap = new HashMap (); 33 34 private String currentTaskName = null; 35 private long currentTaskTime = 0; 36 37 41 public Iterator getTaskNames() { 42 return taskMap.keySet().iterator(); 43 } 44 45 50 public long getTaskCount(String taskName) { 51 return ((TaskStat) taskMap.get(taskName)).getCount(); 52 } 53 54 59 public long getTotalTaskTime(String taskName) { 60 return ((TaskStat) taskMap.get(taskName)).getTotal(); 61 } 62 63 68 public long getMaxTaskTime(String taskName) { 69 return ((TaskStat) taskMap.get(taskName)).getMax(); 70 } 71 72 77 public long getMinTaskTime(String taskName) { 78 return ((TaskStat) taskMap.get(taskName)).getMin(); 79 } 80 81 86 public long getAvgTaskTime(String taskName) { 87 return ((TaskStat) taskMap.get(taskName)).getAverage(); 88 } 89 90 94 public void start(String taskName) { 95 if (log.isDebugEnabled()) { 96 log.debug("Starting: " + taskName); 97 } 98 this.currentTaskName = taskName; 99 currentTaskTime = System.currentTimeMillis(); 100 } 101 102 105 public void stop() { 106 if (log.isDebugEnabled()) { 107 log.debug("Stopping: " + currentTaskName); 108 } 109 currentTaskTime = System.currentTimeMillis() - currentTaskTime; 110 appendTaskTime(currentTaskName, currentTaskTime); 111 } 112 113 private synchronized void appendTaskTime(String taskName, long taskTime) { 114 TaskStat stat = (TaskStat) taskMap.get(taskName); 115 if (stat == null) { 116 stat = new TaskStat(); 117 taskMap.put(taskName, stat); 118 } 119 stat.appendTaskTime(taskTime); 120 } 121 122 126 public void mergeStopwatch(Stopwatch watch) { 127 Iterator names = watch.getTaskNames(); 128 while (names.hasNext()) { 129 String name = (String ) names.next(); 130 long taskTime = watch.getTotalTaskTime(name); 131 appendTaskTime(name, taskTime); 132 } 133 } 134 135 138 public synchronized void reset() { 139 taskMap.clear(); 140 } 141 142 public String toString() { 143 StringBuffer buffer = new StringBuffer (); 144 buffer.append("Task,Count,Total,Max,Min,Avg\n"); 145 Iterator names = getTaskNames(); 146 while (names.hasNext()) { 147 String name = (String ) names.next(); 148 long taskCount = getTaskCount(name); 149 long taskTime = getTotalTaskTime(name); 150 long taskMin = getMinTaskTime(name); 151 long taskMax = getMaxTaskTime(name); 152 long taskAvg = getAvgTaskTime(name); 153 buffer.append(name + "," + taskCount + "," + taskTime + "," + taskMax + "," + taskMin + "," + taskAvg + "\n"); 154 } 155 return buffer.toString(); 156 } 157 158 private class TaskStat { 159 private static final long UNSET = -999999; 160 161 private long count = 0; 162 private long total = 0; 163 private long min = UNSET; 164 private long max = UNSET; 165 166 170 public void appendTaskTime(long taskTime) { 171 count++; 172 total += taskTime; 173 if (max == UNSET || taskTime > max) { 174 max = taskTime; 175 } 176 if (min == UNSET || taskTime < min) { 177 min = taskTime; 178 } 179 } 180 181 185 public long getTotal() { 186 return total; 187 } 188 189 193 public long getMax() { 194 return max; 195 } 196 197 201 public long getMin() { 202 return min; 203 } 204 205 209 public long getCount() { 210 return count; 211 } 212 213 217 public long getAverage() { 218 if (count > 0) { 219 return Math.round((double) total / (double) count); 220 } else { 221 return 0; 222 } 223 } 224 225 } 226 227 } 228 229 230 231 | Popular Tags |