1 22 package org.jboss.ejb3.statistics; 23 24 import java.lang.reflect.Method ; 25 import java.io.Serializable ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 30 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap; 31 32 37 public class InvocationStatistics implements Serializable 38 { 39 40 private Map methodStats; 41 42 public long concurrentCalls = 0; 43 public long maxConcurrentCalls = 0; 44 public long lastResetTime = System.currentTimeMillis(); 45 46 public class TimeStatistic implements Serializable 47 { 48 public volatile long count; 49 public volatile long minTime = Long.MAX_VALUE; 50 public volatile long maxTime; 51 public volatile long totalTime; 52 53 public void reset() 54 { 55 count = 0; 56 minTime = Long.MAX_VALUE; 57 maxTime = 0; 58 totalTime = 0; 59 } 60 } 61 62 public InvocationStatistics() 63 { 64 methodStats = new ConcurrentReaderHashMap(); 65 } 66 67 73 public void updateStats(Method m, long elapsed) 74 { 75 TimeStatistic stat = (TimeStatistic) methodStats.get(m.getName()); 76 if (stat == null) 77 { 78 stat = new TimeStatistic(); 79 methodStats.put(m.getName(), stat); 80 } 81 stat.count++; 82 stat.totalTime += elapsed; 83 if (stat.minTime > elapsed) 84 stat.minTime = elapsed; 85 if (stat.maxTime < elapsed) 86 stat.maxTime = elapsed; 87 } 88 89 public synchronized void callIn() 90 { 91 concurrentCalls++; 92 if (concurrentCalls > maxConcurrentCalls) 93 maxConcurrentCalls = concurrentCalls; 94 } 95 96 public synchronized void callOut() 97 { 98 concurrentCalls--; 99 } 100 101 104 public void resetStats() 105 { 106 synchronized (methodStats) 107 { 108 Iterator iter = methodStats.values().iterator(); 109 while (iter.hasNext()) 110 { 111 TimeStatistic stat = (TimeStatistic) iter.next(); 112 stat.reset(); 113 } 114 } 115 maxConcurrentCalls = 0; 116 lastResetTime = System.currentTimeMillis(); 117 } 118 119 123 public Map getStats() 124 { 125 return methodStats; 126 } 127 128 136 public String toString() 137 { 138 StringBuffer tmp = new StringBuffer ("<InvocationStatistics concurrentCalls='"); 139 tmp.append(concurrentCalls); 140 tmp.append("' >\n"); 141 142 HashMap copy = new HashMap (methodStats); 143 Iterator iter = copy.entrySet().iterator(); 144 while (iter.hasNext()) 145 { 146 Map.Entry entry = (Map.Entry ) iter.next(); 147 TimeStatistic stat = (TimeStatistic) entry.getValue(); 148 if (stat != null) 149 { 150 tmp.append("<method name='"); 151 tmp.append(entry.getKey()); 152 tmp.append("' count='"); 153 tmp.append(stat.count); 154 tmp.append("' minTime='"); 155 tmp.append(stat.minTime); 156 tmp.append("' maxTime='"); 157 tmp.append(stat.maxTime); 158 tmp.append("' totalTime='"); 159 tmp.append(stat.totalTime); 160 tmp.append("' />\n"); 161 } 162 } 163 tmp.append("</InvocationStatistics>"); 164 return tmp.toString(); 165 } 166 } 167 | Popular Tags |