1 17 package org.alfresco.util.perf; 18 19 import java.text.DecimalFormat ; 20 import java.util.Set ; 21 import java.util.SortedMap ; 22 import java.util.TreeMap ; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 60 public abstract class AbstractPerformanceMonitor 61 { 62 63 private static final Log methodSummaryLogger = LogFactory.getLog("performance.summary.method"); 64 65 private static final Log vmSummaryLogger = LogFactory.getLog("performance.summary.vm"); 66 67 private final String entityName; 68 69 private SortedMap <String , MethodStats> stats; 70 71 76 public static boolean isDebugEnabled() 77 { 78 return (vmSummaryLogger.isDebugEnabled() || methodSummaryLogger.isDebugEnabled()); 79 } 80 81 84 public AbstractPerformanceMonitor(String entityName) 85 { 86 this.entityName = entityName; 87 stats = new TreeMap <String , MethodStats>(); 88 89 if (vmSummaryLogger.isDebugEnabled()) 91 { 92 Thread hook = new ShutdownThread(); 93 Runtime.getRuntime().addShutdownHook(hook); 94 } 95 } 96 97 107 protected void recordStats(String methodName, double delayMs) 108 { 109 Log methodLogger = LogFactory.getLog("performance." + entityName + "." + methodName); 110 if (!methodLogger.isDebugEnabled()) 111 { 112 return; 114 } 115 116 DecimalFormat format = new DecimalFormat (); 117 format.setMinimumFractionDigits (3); 118 format.setMaximumFractionDigits (3); 119 120 if (methodSummaryLogger.isDebugEnabled()) 122 { 123 methodLogger.debug("Executed " + entityName + "#" + methodName + " in " + format.format(delayMs) + "ms"); 124 } 125 if (vmSummaryLogger.isDebugEnabled()) 126 { 127 synchronized(this) { 129 MethodStats methodStats = stats.get(methodName); 131 if (methodStats == null) 132 { 133 methodStats = new MethodStats(); 134 stats.put(methodName, methodStats); 135 } 136 methodStats.record(delayMs); 137 } 138 } 139 } 140 141 144 private class MethodStats 145 { 146 private int count; 147 private double totalTimeMs; 148 149 154 public void record(double delayMs) 155 { 156 count++; 157 totalTimeMs += delayMs; 158 } 159 160 public String toString() 161 { 162 DecimalFormat format = new DecimalFormat (); 163 format.setMinimumFractionDigits (3); 164 format.setMaximumFractionDigits (3); 165 double averageMs = totalTimeMs / (long) count; 166 return ("Executed " + count + " times, averaging " + format.format(averageMs) + "ms per call"); 167 } 168 } 169 170 173 private class ShutdownThread extends Thread 174 { 175 public void run() 176 { 177 String beanName = AbstractPerformanceMonitor.this.entityName; 178 179 synchronized(ShutdownThread.class) 181 { 182 vmSummaryLogger.debug("\n==================== " + beanName.toUpperCase() + " ==================="); 183 Set <String > methodNames = stats.keySet(); 184 for (String methodName : methodNames) 185 { 186 vmSummaryLogger.debug("\nMethod performance summary: \n" + 187 " Bean: " + AbstractPerformanceMonitor.this.entityName + "\n" + 188 " Method: " + methodName + "\n" + 189 " Statistics: " + stats.get(methodName)); 190 } 191 } 192 } 193 } 194 } 195 | Popular Tags |