1 24 package org.objectweb.jalisto.se.test.workbench; 25 26 import java.io.PrintStream ; 27 import java.util.*; 28 29 public class JalistoTimer { 30 public JalistoTimer(String name) { 31 this.name = name; 32 this.sum = 0; 33 this.count = 0; 34 out = System.out; 35 mask = new BitSet(NEWLINE + 1); 36 mask.set(0, NEWLINE + 1, true); 37 verbose = true; 38 } 39 40 public void start() { 41 start = System.currentTimeMillis(); 42 } 43 44 public void stop(String name, boolean show) { 45 long end = System.currentTimeMillis(); 46 long result = (end - start); 47 if (show) { 48 out.println(name + " elapsed time : " + result + " ms"); 49 } 50 sum += result; 51 count++; 52 } 53 54 public float getAverage() { 55 return sum / count; 56 } 57 58 private String getSummary() { 59 StringBuffer sb = new StringBuffer (); 60 if (mask.get(NAME)) { 61 if (verbose) { 62 sb.append("TIMER:"); 63 } 64 sb.append(name).append(";"); 65 } 66 if (mask.get(TIME)) { 67 if (verbose) { 68 sb.append("TIME:"); 69 } 70 sb.append(System.currentTimeMillis()).append(";"); 71 } 72 if (mask.get(SUM)) { 73 if (verbose) { 74 sb.append("SUM:"); 75 } 76 sb.append(sum).append(";"); 77 } 78 if (mask.get(COUNT)) { 79 if (verbose) { 80 sb.append("COUNT:"); 81 } 82 sb.append(count).append(";"); 83 } 84 if (mask.get(AVG) && (count != 0)) { 85 if (verbose) { 86 sb.append("AVG:"); 87 } 88 sb.append(sum / count).append(";"); 89 } 90 if (mask.get(NEWLINE)) { 91 sb.append("\n"); 92 } 93 return sb.toString(); 94 } 95 96 private void clean() { 97 this.sum = 0; 98 this.count = 0; 99 } 100 101 102 105 106 public static void createTimer(String name) { 107 JalistoTimer timer = (JalistoTimer) timers.get(name); 108 if (timer != null) { 109 throw new UnsupportedOperationException ("Create over an existing timer"); 110 } 111 timer = new JalistoTimer(name); 112 timers.put(name, timer); 113 timerNames.add(name); 114 } 115 116 public static void removeTimer(String name) { 117 timers.remove(name); 118 timerNames.remove(name); 119 } 120 121 public static void timerStart(String name) { 122 JalistoTimer timer = (JalistoTimer) timers.get(name); 123 if (timer == null) { 124 timer = new JalistoTimer(name); 125 timers.put(name, timer); 126 timerNames.add(name); 127 } 128 timer.start(); 129 } 130 131 public static void timerStop(String name) { 132 timerStop(name, true); 133 } 134 135 public static void timerStop(String name, boolean show) { 136 JalistoTimer timer = (JalistoTimer) timers.get(name); 137 if (timer == null) { 138 throw new UnsupportedOperationException ("Unable to stop an unexisting timer"); 139 } 140 timer.stop(name, show); 141 } 142 143 public static void setOut(String name, PrintStream out) { 144 JalistoTimer timer = (JalistoTimer) timers.get(name); 145 if (timer == null) { 146 throw new UnsupportedOperationException ("Unable to set Out of an unexisting timer"); 147 } 148 timer.out = out; 149 } 150 151 public static void setFormat(String name, int format) { 152 JalistoTimer timer = (JalistoTimer) timers.get(name); 153 if (timer == null) { 154 throw new UnsupportedOperationException ("Unable to set format of an unexisting timer"); 155 } 156 if (timer.initialValues) { 157 timer.mask.clear(); 158 timer.verbose = false; 159 timer.initialValues = false; 160 } 161 timer.mask.set(format); 162 } 163 164 public static void unsetFormat(String name, int format) { 165 JalistoTimer timer = (JalistoTimer) timers.get(name); 166 if (timer == null) { 167 throw new UnsupportedOperationException ("Unable to unset format of an unexisting timer"); 168 } 169 timer.mask.clear(format); 170 } 171 172 public static void setVerbose(String name, boolean verbose) { 173 JalistoTimer timer = (JalistoTimer) timers.get(name); 174 if (timer == null) { 175 throw new UnsupportedOperationException ("Unable to set verbose value of an unexisting timer"); 176 } 177 if (timer.initialValues) { 178 timer.mask.clear(); 179 timer.initialValues = false; 180 } 181 timer.verbose = verbose; 182 } 183 184 public static void summary() { 185 for (int i = 0; i < timerNames.size(); i++) { 186 JalistoTimer timer = (JalistoTimer) timers.get(timerNames.get(i)); 187 timer.out.print(timer.getSummary()); 188 } 189 } 190 191 public static void summary(String name) { 192 JalistoTimer timer = (JalistoTimer) timers.get(name); 193 if (timer == null) { 194 throw new UnsupportedOperationException ("Unable to print summary for an unexisting timer"); 195 } 196 timer.out.print(timer.getSummary()); 197 } 198 199 public static void clean(String name) { 200 JalistoTimer timer = (JalistoTimer) timers.get(name); 201 if (timer == null) { 202 throw new UnsupportedOperationException ("Unable to cleanAll an unexisting timer"); 203 } 204 timer.clean(); 205 } 206 207 public static void deleteAllTimers() { 208 timers.clear(); 209 timerNames.clear(); 210 } 211 212 public static float getAverage(String name) { 213 JalistoTimer timer = (JalistoTimer) timers.get(name); 214 if (timer == null) { 215 throw new UnsupportedOperationException ("Unable to get average on an unexisting timer"); 216 } 217 return timer.getAverage(); 218 } 219 220 221 private String name; 222 private long start; 223 private long sum; 224 private long count; 225 private PrintStream out; 226 private BitSet mask; 227 private boolean verbose; 228 private boolean initialValues = true; 229 230 231 private static Map timers = new HashMap(); 232 private static ArrayList timerNames = new ArrayList(); 233 234 public static final int TIME = 0; 235 public static final int NAME = 1; 236 public static final int SUM = 2; 237 public static final int COUNT = 3; 238 public static final int AVG = 4; 239 public static final int NEWLINE = 5; 240 } 241 | Popular Tags |