1 17 18 package org.sape.carbon.services.perflog; 19 20 import java.util.Collections ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import org.sape.carbon.core.component.ComponentConfiguration; 26 import org.sape.carbon.core.component.lifecycle.Configurable; 27 28 40 public class BoundedBufferPerformanceLogger 41 extends DefaultPerformanceLogger 42 implements ReportingPerformanceLogger, Configurable { 43 44 45 48 protected Map timingMap = Collections.synchronizedMap(new HashMap ()); 49 50 53 protected int trackedTimings; 54 55 62 protected void trackTiming(Object trackedObject, long time) { 63 super.trackTiming(trackedObject, time); 64 registerTiming(trackedObject, time); 65 } 66 67 75 protected void registerTiming(Object trackedObject, long time) { 76 77 TimingInfo timingInfo = 81 (TimingInfo) this.timingMap.get(trackedObject); 82 83 if (timingInfo == null) { 84 timingInfo = new TimingInfo(trackedObject, this.trackedTimings); 85 86 this.timingMap.put(trackedObject, timingInfo); 87 } 88 timingInfo.addTiming(time); 89 } 90 91 96 private static final String PAD = " "; 97 98 105 private String pad(String str, int len) { 106 if (str.length() > len) { 107 return str.substring(0, len - 1) + " "; 108 } else { 109 return str + PAD.substring(0, len - str.length()); 110 } 111 } 112 113 119 public String getReport() { 120 StringBuffer buf = new StringBuffer (1000); 121 buf.append("Extended performance logger report ["); 122 buf.append(this.timingMap.size()); 123 buf.append("] items.\n"); 124 125 buf.append("Min Max Avg StdDev Tracked Object\n"); 126 Iterator iter = this.timingMap.values().iterator(); 127 while (iter.hasNext()) { 128 TimingInfo info = (TimingInfo) iter.next(); 129 130 buf.append(pad(String.valueOf(info.getMin()), 10)); 131 buf.append(pad(String.valueOf(info.getMax()), 10)); 132 buf.append(pad(String.valueOf(info.getAverage()), 10)); 133 buf.append(pad(String.valueOf(info.getStandardDeviation()), 10)); 134 buf.append(String.valueOf(info.getTrackedObject())); 135 buf.append("\n"); 136 } 137 return buf.toString(); 138 } 139 140 148 public void configure(ComponentConfiguration configuration) { 149 150 this.trackedTimings = 151 ((BoundedBufferPerformanceLoggerConfiguration) configuration). 152 getTrackedTimingsCount(); 153 } 154 155 166 public static class TimingInfo { 167 168 private Object trackedObject; 169 170 171 private int capacity; 172 173 174 private boolean full = false; 175 176 177 private long[] timingArray; 178 179 184 private int index; 185 186 187 private long min = Long.MAX_VALUE; 188 189 190 private long max; 191 192 199 public TimingInfo(Object trackedObject, int capacity) { 200 this.trackedObject = trackedObject; 201 this.timingArray = new long[capacity]; 202 this.capacity = capacity; 203 } 204 205 210 public Object getTrackedObject() { 211 return this.trackedObject; 212 } 213 214 219 public synchronized void addTiming(long time) { 220 this.timingArray[index++] = time; 221 if (this.index >= this.capacity) { 222 this.index = 0; 223 this.full = true; 224 } 225 226 if (this.max < time) { 227 this.max = time; 228 } 229 230 if (this.min > time) { 231 this.min = time; 232 } 233 } 234 235 240 public long getMin() { 241 return this.min; 242 } 243 244 249 public long getMax() { 250 return this.max; 251 } 252 253 258 public synchronized int size() { 259 if (full) { 260 return this.capacity; 261 } else { 262 return this.index; 263 } 264 } 265 266 271 public synchronized double getStandardDeviation() { 272 final int n = size(); 273 if (n < 2) { 274 return Double.NaN; 275 } 276 277 double avg = this.timingArray[0]; 278 double sum = 0; 279 for (int i = 1; i < n - 1; i++) { 280 double newavg = avg + (this.timingArray[i] - avg) / (i + 1); 281 sum += (this.timingArray[i] - avg) 282 * (this.timingArray[i] - newavg); 283 284 avg = newavg; 285 } 286 return Math.sqrt (sum / (n - 1)); 287 } 288 289 294 public synchronized double getAverage() { 295 final int n = size(); 296 double sum = 0; 297 for (int i = 0; i < n; i++) { 298 sum += this.timingArray[i]; 299 } 300 return (sum / (double) n); 301 } 302 } 303 } | Popular Tags |