1 package org.igfay.util; 2 3 import java.text.DateFormat ; 4 import java.util.ArrayList ; 5 import java.util.Date ; 6 import java.util.HashMap ; 7 import java.util.List ; 8 9 import org.apache.log4j.Level; 10 import org.apache.log4j.Logger; 11 import org.igfay.jfig.JFigUtility; 12 13 20 public class Metric { 21 private static Logger log = Logger.getLogger(Metric.class); 22 23 public final static String DEFAULT = "DEFAULT"; 24 public final static String BENCHMARK = "BENCHMARK"; 25 public final static String DB = "DB"; 26 public final static String EVENT = "EVENT"; 27 public final static String LOGIN = "LOGIN"; 28 public final static String QUERY = "QUERY"; 29 public final static String DOWNLOAD = "DOWNLOAD"; 30 public final static String UPLOAD = "UPLOAD"; 31 public final static String ACCEPTANCE_MANAGER = "ACCEPTANCE_MANAGER"; 32 public final static String NOTIFY_ACTION = "NOTIFY_ACTION"; 33 public final static String RULES_ENGINE = "RULES_ENGINE"; 34 35 private static java.util.HashMap allGroups; 36 37 private long startTime; 38 private long endTime; 39 private long pauseStartTime; 40 private long pauseElapsedTime; 41 private java.lang.String groupName; 42 private java.lang.String metricName; 43 private boolean isDebugEnabled; 44 45 46 49 public Metric() { 50 this(false); 51 } 52 53 54 59 public Metric(String groupName) { 60 this(groupName, false); 61 } 62 63 64 70 public Metric(String groupName, String metricName) { 71 this(groupName, metricName, false); 72 } 73 74 75 82 public Metric(String groupName, String metricName, boolean isStart) { 83 this(groupName, metricName, isStart, false); 84 } 85 86 87 public Metric(String groupName, String metricName, boolean isStart, boolean isDebugEnabled) { 88 this.groupName = groupName; 89 this.metricName = metricName; 90 this.setIsDebugEnabled(isDebugEnabled); 91 if (isStart) { 95 start(); 96 } 97 } 98 99 100 106 public Metric(String groupName, boolean isStart) { 107 this(groupName, "unnamed", isStart); 108 } 109 110 111 116 public Metric(boolean isStart) { 117 this(DEFAULT, isStart); 118 } 119 120 121 128 public static void addToListForGroup(String groupName, Metric metric) { 129 getListForGroup(groupName).add(metric); 130 } 131 132 133 139 public static void main(String [] args) { 140 testSimple(); 141 } 142 143 144 150 public static void printGroup(String groupName) { 151 List list = getListForGroup(groupName); 152 for (int i = 0; i < list.size(); i++) { 153 Metric metric = (Metric) list.get(i); 154 log.info(metric.toStringLong()); 155 } 156 } 157 158 159 163 public static void test() { 164 Metric metric = new Metric(Metric.LOGIN, "BRUCE1"); 165 Metric bruce2 = new Metric(Metric.LOGIN, "BRUCE2"); 166 Metric bruce3 = new Metric(Metric.LOGIN, "BRUCE3"); 167 metric.start(); 168 bruce2.start(); 169 bruce3.start(); 170 try { 171 Thread.sleep(300); 172 } 173 catch (InterruptedException e) { 174 } 175 metric.end(); 176 bruce2.pause(); 177 try { 178 Thread.sleep(300); 179 } 180 catch (InterruptedException e) { 181 } 182 bruce2.resume(); 183 bruce2.end(); 184 bruce3.end(); 185 Metric bruce4 = new Metric(Metric.LOGIN, "BRUCE4", true); 186 printGroup(Metric.LOGIN); 187 bruce4.end(); 188 log.info(bruce4.toStringLong()); 189 190 bruce4.clear(); 191 bruce4.start(); 192 for (int i = 0; i < 1000000; i++) { 193 } 194 bruce4.end(); 195 log.info(bruce4.toStringLong()); 196 197 bruce4.print(); 199 testIsDebugEnabled(bruce4); 200 201 } 202 203 204 public static void testSimple() { 205 Metric metric = new Metric(Metric.LOGIN, "BRUCE1"); 206 metric.start(); 207 try { 208 Thread.sleep(300); 209 } 210 catch (InterruptedException e) { 211 } 212 metric.endAndPrint(); 213 } 214 215 public static void testIsDebugEnabled(Metric bruce4) { 216 log.info(""); 217 bruce4.setIsDebugEnabled(true); 218 log.setLevel(Level.INFO); 219 log.info("should not print"); 220 bruce4.print(); 221 bruce4.setIsDebugEnabled(false); 222 log.info("should print"); 223 bruce4.print(); 224 } 225 226 227 234 public static List getListForGroup(String groupName) { 235 List list = (List ) getAllGroups().get(groupName); 236 if (list == null) { 237 list = new ArrayList (); 238 getAllGroups().put(groupName, list); 239 } 240 return list; 241 } 242 243 244 250 public static boolean isMetricLoggingEnabled() { 251 258 return true; 259 } 260 261 262 268 private static HashMap getAllGroups() { 269 if (allGroups == null) { 270 allGroups = new HashMap (); 271 } 272 273 return allGroups; 274 } 275 276 277 283 private static void setAllGroups(HashMap newAllGroups) { 284 allGroups = newAllGroups; 285 } 286 287 288 292 public void clear() { 293 setStartTime(0); 294 setEndTime(0); 295 setPauseElapsedTime(0); 296 setPauseStartTime(0); 297 298 } 299 300 301 305 public void end() { 306 setEndTime(System.currentTimeMillis()); 307 308 } 309 310 311 public void stop() { 312 end(); 313 314 } 315 316 317 public void endAndPrint() { 318 end(); 319 print(); 320 321 } 322 323 324 328 public void pause() { 329 setPauseStartTime(System.currentTimeMillis()); 330 } 331 332 333 337 public void print() { 338 if (isMetricLoggingEnabled() ) { 339 if (isDebugEnabled() && !log.isDebugEnabled()) 340 return; 341 log.info(toStringLong()); 342 } 343 344 } 345 346 347 351 public void resume() { 352 if (getPauseStartTime() > 0) { 353 setPauseElapsedTime(getPauseElapsedTime() + System.currentTimeMillis() - getPauseStartTime()); 354 setPauseStartTime(0); 355 } 356 } 357 358 359 363 public void start() { 364 clear(); 365 setStartTime(System.currentTimeMillis()); 366 367 } 368 369 370 376 public String toString() { 377 return toStringBuffer().toString(); 378 } 379 380 381 387 public StringBuffer toStringBuffer() { 388 StringBuffer buffer = new StringBuffer (); 389 String blank = " "; 390 String elapsedTimeString = "---"; 391 elapsedTimeString = Long.toString(getElapsedTime()); 393 buffer.append(getGroupName()).append(blank).append(getMetricName()).append(blank).append(elapsedTimeString); 395 return buffer; 396 } 397 398 399 405 public String toStringLong() { 406 StringBuffer buffer = toStringBuffer(); 407 408 DateFormat dateFormat = JFigUtility.getDateFormat(); 409 String startDate = dateFormat.format(new Date (getStartTime())); 410 String endDate = dateFormat.format(new Date (getEndTime())); 411 String blank = " "; 412 413 buffer.append(blank).append(startDate).append(blank).append(endDate).append(blank). 414 append(getPauseElapsedTime()).append(blank); 415 return buffer.toString(); 416 } 417 418 419 425 public String getGroupName() { 426 return groupName; 427 } 428 429 430 436 public String getMetricName() { 437 return metricName; 438 } 439 440 441 447 public long getElapsedTime() { 448 return getEndTime() - getStartTime() - getPauseElapsedTime(); 449 } 450 451 452 458 protected long getEndTime() { 459 return endTime; 460 } 461 462 463 469 protected long getPauseElapsedTime() { 470 return pauseElapsedTime; 471 } 472 473 474 480 protected long getPauseStartTime() { 481 return pauseStartTime; 482 } 483 484 485 491 protected long getStartTime() { 492 return startTime; 493 } 494 495 496 502 public void setEndTime(long newEndTime) { 503 endTime = newEndTime; 504 } 505 506 507 513 public void setGroupName(String newGroup) { 514 groupName = newGroup; 515 } 516 517 518 524 public void setMetricName(String newName) { 525 metricName = newName; 526 } 527 528 529 public void setMetricNameFailed() { 530 metricName += "-failed"; 531 } 532 538 public void setStartTime(long newStartTime) { 539 startTime = newStartTime; 540 } 541 542 543 549 protected void setPauseElapsedTime(long newPauseElapsedTime) { 550 pauseElapsedTime = newPauseElapsedTime; 551 } 552 553 554 560 protected void setPauseStartTime(long newPauseStartTime) { 561 pauseStartTime = newPauseStartTime; 562 } 563 567 protected boolean isDebugEnabled() { 568 return isDebugEnabled; 569 } 570 571 575 public void setIsDebugEnabled(boolean isDebugEnabled) { 576 this.isDebugEnabled = isDebugEnabled; 577 } 578 579 } 580 | Popular Tags |