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