1 package org.incava.qualog; 2 3 import java.util.*; 4 5 6 public class QlTimer 7 { 8 private List periods = null; 9 10 public QlTimer() 11 { 12 periods = new ArrayList(); 13 } 14 15 public boolean start() 16 { 17 return start(null); 18 } 19 20 public boolean end() 21 { 22 return end(null); 23 } 24 25 public boolean start(String msg) 26 { 27 StackTraceElement ste = getFrame(); 28 29 String className = ste.getClassName(); 30 String methodName = ste.getMethodName(); 31 int lineNumber = ste.getLineNumber(); 32 String fileName = ste.getFileName(); 33 34 QlTimedPeriod qtp = new QlTimedPeriod(fileName, className, methodName, lineNumber, msg); 35 periods.add(qtp); 36 37 return true; 38 } 39 40 public boolean end(String msg) 41 { 42 long endTime = System.currentTimeMillis(); 43 44 StackTraceElement ste = getFrame(); 45 46 48 String className = ste.getClassName(); 49 String methodName = ste.getMethodName(); 50 int lineNumber = ste.getLineNumber(); 51 String fileName = ste.getFileName(); 52 int bestMatchIdx = -1; 53 int bestMatchness = -1; 54 55 Iterator it = periods.iterator(); 56 for (int idx = 0; it.hasNext(); ++idx) { 57 Object obj = it.next(); 58 QlTimedPeriod qtp = (QlTimedPeriod)obj; 59 int matchness = 0; 60 61 matchness += qtp.getMessage() != null && msg.equals(qtp.getMessage()) ? 1 : 0; 62 matchness += className.equals(qtp.getClassName()) ? 1 : 0; 63 matchness += fileName.equals(qtp.getFileName()) ? 1 : 0; 64 matchness += methodName.equals(qtp.getMethodName()) ? 1 : 0; 65 66 if (matchness >= bestMatchness) { 68 bestMatchness = matchness; 69 bestMatchIdx = idx; 70 } 71 } 72 73 76 if (bestMatchIdx >= 0) { 77 QlTimedPeriod qtp = (QlTimedPeriod)periods.remove(bestMatchIdx); 78 long elapsed = endTime - qtp.getStartTime(); 79 StringBuffer buf = new StringBuffer (); 80 81 buf.append(format(elapsed)); 82 buf.append("; "); 83 84 if (msg != null) { 85 buf.append(msg); 86 buf.append("; "); 87 } 88 89 buf.append("from: ["); 90 buf.append(fileName); 91 buf.append(":"); 92 buf.append(Integer.toString(lineNumber)); 93 buf.append("]"); 94 buf.append(" "); 95 96 buf.append("{"); 97 buf.append(className); 98 buf.append("#"); 99 buf.append(methodName); 100 buf.append("}"); 101 102 Qualog.log(buf.toString()); 103 } 104 else { 105 System.err.println("ERROR no matching start!"); 106 } 107 108 return true; 109 } 110 111 protected StackTraceElement getFrame() 112 { 113 StackTraceElement [] stack = (new Exception ("")).getStackTrace(); 114 int stIdx = Qualog.findStackStart(stack); 115 StackTraceElement ste = stack[stIdx]; 116 117 return ste; 118 } 119 120 public String format(long duration) 121 { 122 StringBuffer buf = new StringBuffer (); 123 if (duration < 10000) { 124 buf.append(Long.toString(duration)); 125 buf.append(" ms"); 126 } 127 else if (duration < 100000) { 128 double nSecs = duration / 1000.0; 129 buf.append(Double.toString(nSecs)); 130 buf.append(" sec"); 131 } 132 else if (duration < 1000000) { 133 double nMin = Math.floor(duration / (60 * 1000.0)); 134 double nSec = (duration - 60.0 * nMin) / 1000.0; 135 buf.append(Double.toString(nMin)); 136 buf.append(":"); 137 buf.append(Double.toString(nSec)); 138 } 139 else { 140 buf.append(Long.toString(duration)); 142 } 143 return buf.toString(); 144 } 145 146 } 147 | Popular Tags |