playgrnd
@ HEAD
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
/* * TimeLogger.java Feb 6, 2008 * * Copyright 2007 Kobie Marketing, Inc. All rights reserved. */ package com.kobie.alchemy.util; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.util.Map; import java.util.TreeMap; import org.apache.log4j.Logger; /** * This class provides time logging functions for application benchmarking. * * @author Vlad Orzhekhovskiy */ public class TimeLogger { private final Logger log = Logger.getLogger(getClass()); private static TimeLogger global = new TimeLogger(); private Map<String, Timer> timers = new TreeMap<String, Timer>(); private boolean nanoMode = false; private int precision = 0; /* * Global shared time recorder. */ public static TimeLogger getGlobal() { return global; } public static void setNanoModeGlobal(boolean nanoMode) { global.setNanoMode(nanoMode); } public static void setPrecisionGlobal(int precision) { global.setPrecision(precision); } public static void startGlobal(String timerId) { global.start(timerId); } public static void stopGlobal(String timerId) { global.stop(timerId); } public static void resetGlobal(String timerId) { global.reset(timerId); } public static void resetAllGlobal() { global.resetAll(); } public static void printTimingsGlobal() { global.printTimings(); } public static void printTimingsGlobal(PrintStream out) { global.printTimings(out); } /* * Local timer recorders. */ public void setNanoMode(boolean nanoMode) { this.nanoMode = nanoMode; } public void setPrecision(int precision) { this.precision = precision; } public void start(String timerId) { Timer timer = timers.get(timerId); if (timer == null) { timer = new Timer(timerId); timers.put(timerId, timer); } try { timer.start(); } catch (IllegalStateException e) { log.warn(e.getMessage()); } } public void stop(String timerId) { Timer timer = timers.get(timerId); if (timer == null) return; try { timer.stop(); } catch (IllegalStateException e) { log.warn(e.getMessage()); } } public void reset(String timerId) { Timer timer = timers.get(timerId); if (timer == null) return; timer.reset(); } public void resetAll() { timers.clear(); } public String getTimings() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos); printTimings(ps); ps.close(); return baos.toString(); } public void printTimings() { printTimings(System.out); } public void printTimings(PrintStream out) { // print header out.printf("%-45s %15s %10s %12s %12s\n", "Timer", "Total,ms", "Loops", "Avg,ms", "StDev,ms"); for (int i = 0; i < 98; i++) out.print("-"); out.println(); for (Map.Entry<String, Timer> entry : timers.entrySet()) { Timer timer = entry.getValue(); Stats stats = timer.getStats(); // print timer id with optional new line if (timer.getId().length() > 45) out.printf("%s\n%-45s", timer.getId(), ""); else out.printf("%-45s", timer.getId()); // print according to precision if (precision > 0) { final double NS2MS = 1000000; // nanoseconds to milliseconds ratio String format = " %,15." + precision + "f %,10d %,12." + precision + "f %,12." + precision + "f\n"; out.printf(format, stats.getTotal() / NS2MS, stats.getCount(), stats .getAverage() / NS2MS, stats.getStDev() / NS2MS); } else { final int NS2MS = 1000000; // nanoseconds to milliseconds ratio String format = " %,15d %,10d %,12d %,12d\n"; out.printf(format, stats.getTotal() / NS2MS, stats.getCount(), stats .getAverage() / NS2MS, stats.getStDev() / NS2MS); } } } protected long getCurrentNanoTime() { return nanoMode ? System.nanoTime() : System.currentTimeMillis() * 1000000L; } /** * Timer implementation. Ths class is NOT thread safe. * * @author Vlad Orzhekhovskiy */ protected class Timer { private final String id; private Stats stats; private boolean started; private long lastStartTime; public Timer(String id) { this.id = id; reset(); } public String getId() { return id; } public boolean isStarted() { return started; } public Stats getStats() { return stats; } public void reset() { stats = new Stats(); started = false; lastStartTime = 0L; } public void start() throws IllegalStateException { if (started) throw new IllegalStateException("Timer [" + id + "] has already been started"); started = true; lastStartTime = getCurrentNanoTime(); } public void stop() throws IllegalStateException { long timeElapsed = getCurrentNanoTime() - lastStartTime; if (!started) throw new IllegalStateException("Timer [" + id + "] has not been started"); stats.addValue(timeElapsed); started = false; } } } /** * General purpose statistics processor. * * @author Vlad Orzhekhovskiy */ class Stats { private long total = 0L; private long totalSq = 0L; private long count = 0L; public void addValue(long value) { total += value; totalSq += value * value; count++; } public long getTotal() { return total; } public long getCount() { return count; } public long getAverage() { if (count == 0) return 0; else return (long) (((double) total / count) + 0.5); } public long getStDev() { if (count == 0) return 0; double dispersion = totalSq / count - getAverage() * getAverage(); return (long) (Math.sqrt(dispersion) + 0.5); } } |
Commits for playgrnd/TimeLogger.java
Revision | Author | Commited | Message |
---|---|---|---|
17 | snark | Tue 24 Jul, 2012 01:39:20 +0000 | initial checkin |