1 22 package org.jboss.web.tomcat.statistics; 23 24 import java.io.Serializable ; 25 import java.util.Collections ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 30 35 public class InvocationStatistics implements Serializable 36 { 37 38 private static final long serialVersionUID = 9153807780893455734L; 39 40 41 private Map ctxStats; 42 43 public volatile int concurrentCalls = 0; 44 45 public volatile int maxConcurrentCalls = 0; 46 47 public long lastResetTime = System.currentTimeMillis(); 48 49 public static class TimeStatistic 50 { 51 public long count; 52 public long minTime = Long.MAX_VALUE; 53 public long maxTime; 54 public long totalTime; 55 56 public void reset() 57 { 58 count = 0; 59 minTime = Long.MAX_VALUE; 60 maxTime = 0; 61 totalTime = 0; 62 } 63 } 64 65 public InvocationStatistics() 66 { 67 ctxStats = Collections.synchronizedMap(new HashMap ()); 68 } 69 70 76 public void updateStats(String ctx, long elapsed) 77 { 78 TimeStatistic stat = (TimeStatistic) ctxStats.get(ctx); 79 if( stat == null ) 80 { 81 stat = new TimeStatistic(); 82 ctxStats.put(ctx, stat); 83 } 84 stat.count ++; 85 stat.totalTime += elapsed; 86 if( stat.minTime > elapsed ) 87 stat.minTime = elapsed; 88 if( stat.maxTime < elapsed ) 89 stat.maxTime = elapsed; 90 } 91 92 public void callIn() 93 { 94 concurrentCalls ++; 95 if (concurrentCalls > maxConcurrentCalls) 96 maxConcurrentCalls = concurrentCalls; 97 } 98 99 public void callOut() 100 { 101 concurrentCalls --; 102 } 103 104 107 public void resetStats() 108 { 109 synchronized( ctxStats ) 110 { 111 Iterator iter = ctxStats.values().iterator(); 112 while( iter.hasNext() ) 113 { 114 TimeStatistic stat = (TimeStatistic) iter.next(); 115 stat.reset(); 116 } 117 } 118 maxConcurrentCalls = 0; 119 lastResetTime = System.currentTimeMillis(); 120 } 121 122 126 public Map getStats() 127 { 128 return ctxStats; 129 } 130 131 public String toString() 132 { 133 StringBuffer tmp = new StringBuffer ("(concurrentCalls: "); 134 tmp.append(concurrentCalls); 135 tmp.append(", maxConcurrentCalls: "); 136 tmp.append(maxConcurrentCalls); 137 138 HashMap copy = new HashMap (ctxStats); 139 Iterator iter = copy.entrySet().iterator(); 140 while( iter.hasNext() ) 141 { 142 Map.Entry entry = (Map.Entry ) iter.next(); 143 TimeStatistic stat = (TimeStatistic) entry.getValue(); 144 tmp.append("[webCtx: "); 145 tmp.append(entry.getKey()); 146 tmp.append(", count="); 147 tmp.append(stat.count); 148 tmp.append(", minTime="); 149 tmp.append(stat.minTime); 150 tmp.append(", maxTime="); 151 tmp.append(stat.maxTime); 152 tmp.append(", totalTime="); 153 tmp.append(stat.totalTime); 154 tmp.append("];"); 155 } 156 tmp.append(")"); 157 return tmp.toString(); 158 } 159 160 168 public String toXML() 169 { 170 StringBuffer tmp = new StringBuffer ("<InvocationStatistics concurrentCalls='"); 171 tmp.append(concurrentCalls); 172 tmp.append("' maxConcurrentCalls='"); 173 tmp.append(maxConcurrentCalls); 174 tmp.append("' >\n"); 175 176 HashMap copy = new HashMap (ctxStats); 177 Iterator iter = copy.entrySet().iterator(); 178 while( iter.hasNext() ) 179 { 180 Map.Entry entry = (Map.Entry ) iter.next(); 181 TimeStatistic stat = (TimeStatistic) entry.getValue(); 182 tmp.append("<webCtx name='"); 183 tmp.append(entry.getKey()); 184 tmp.append("' count='"); 185 tmp.append(stat.count); 186 tmp.append("' minTime='"); 187 tmp.append(stat.minTime); 188 tmp.append("' maxTime='"); 189 tmp.append(stat.maxTime); 190 tmp.append("' totalTime='"); 191 tmp.append(stat.totalTime); 192 tmp.append("' />\n"); 193 } 194 tmp.append("</InvocationStatistics>"); 195 return tmp.toString(); 196 } 197 } 198 | Popular Tags |