1 18 19 package org.apache.jmeter.reporters; 20 21 import java.io.Serializable ; 22 import java.text.DecimalFormat ; 23 import java.util.Hashtable ; 24 25 import org.apache.jmeter.engine.event.LoopIterationEvent; 26 import org.apache.jmeter.samplers.Clearable; 27 import org.apache.jmeter.samplers.SampleEvent; 28 import org.apache.jmeter.samplers.SampleListener; 29 import org.apache.jmeter.samplers.SampleResult; 30 import org.apache.jmeter.testelement.AbstractTestElement; 31 import org.apache.jmeter.testelement.TestListener; 32 import org.apache.jmeter.util.JMeterUtils; 33 import org.apache.jmeter.visualizers.RunningSample; 34 import org.apache.jorphan.logging.LoggingManager; 35 import org.apache.jorphan.util.JOrphanUtils; 36 import org.apache.log.Logger; 37 38 49 public class Summariser 50 extends AbstractTestElement 51 implements Serializable , 52 SampleListener, 53 TestListener, 54 Clearable 55 { 56 private static final Logger log = LoggingManager.getLoggerForClass(); 57 58 59 private static final long INTERVAL = 60 JMeterUtils.getPropDefault("summariser.interval",3*60); 62 63 private static final boolean TOLOG = 64 JMeterUtils.getPropDefault("summariser.log",true); 66 67 private static final boolean TOOUT = 68 JMeterUtils.getPropDefault("summariser.out",true); 70 76 private static Hashtable accumulators = new Hashtable (); 77 78 85 public Summariser(){ 86 super(); 87 } 90 91 96 public Summariser(String name){ 97 this(); 98 setName(name); 99 } 100 101 106 public void clear() 107 { 108 110 myName = this.getName(); 111 112 synchronized(accumulators){ 115 Totals tots = (Totals) accumulators.get(myName); 116 if (tots != null){ tots.clear(); 118 } else { 119 tots = new Totals(); 121 accumulators.put(myName,tots); 122 } 123 } 124 125 super.clear(); 126 } 127 128 133 private static class Totals{ 134 135 136 private long last = 0; 138 private RunningSample delta = new RunningSample("DELTA",0); 139 private RunningSample total = new RunningSample("TOTAL",0); 140 141 private void clear(){ 142 delta.clear(); 143 total.clear(); 144 last = 0; 145 } 146 147 150 private synchronized void moveDelta(){ 151 total.addSample(delta); 152 delta.clear(); 153 } 154 } 155 156 161 transient private Totals myTotals = null; 162 transient private String myName; 163 164 165 169 private static final int INTERVAL_WINDOW = 5; 171 177 public void sampleOccurred(SampleEvent e) { 178 SampleResult s = e.getResult(); 179 180 183 if (myName == null) myName = getName(); 184 185 if (myTotals == null) myTotals = (Totals) accumulators.get(myName); 186 187 if (s != null) 188 { 189 myTotals.delta.addSample(s); 190 } 191 192 long now = System.currentTimeMillis()/1000; 194 RunningSample myDelta=null; 195 RunningSample myTotal=null; 196 boolean reportNow = false; 197 198 202 synchronized(myTotals){ 203 if ((now > myTotals.last + INTERVAL_WINDOW) && (now % INTERVAL <= INTERVAL_WINDOW)) 204 { 205 reportNow=true; 206 myDelta = new RunningSample(myTotals.delta); myTotals.moveDelta(); 208 myTotal = new RunningSample(myTotals.total); myTotals.last = now; 210 } 211 } 212 if (reportNow){ 213 String str; 214 str = format(myDelta,"+"); 215 if (TOLOG) log.info(str); 216 if (TOOUT) System.out.println(str); 217 218 if (myTotal.getNumSamples() != myDelta.getNumSamples()) { str = format(myTotal,"="); 220 if (TOLOG) log.info(str); 221 if (TOOUT) System.out.println(str); 222 } 223 } 224 } 225 226 private static StringBuffer longToSb(StringBuffer sb,long l, int len){ 227 sb.setLength(0); 228 sb.append(l); 229 return JOrphanUtils.rightAlign(sb,len); 230 } 231 232 private static DecimalFormat dfDouble = new DecimalFormat ("#0.0"); 233 private static StringBuffer doubleToSb(StringBuffer sb,double d, int len, int frac){ 234 sb.setLength(0); 235 dfDouble.setMinimumFractionDigits(frac); 236 dfDouble.setMaximumFractionDigits(frac); 237 sb.append(dfDouble.format(d)); 238 return JOrphanUtils.rightAlign(sb,len); 239 } 240 245 private String format(RunningSample s, String type) 246 { 247 StringBuffer tmp = new StringBuffer (20); StringBuffer sb = new StringBuffer (100); sb.append(myName); 250 sb.append(" "); 251 sb.append(type); 252 sb.append(" "); 253 sb.append(longToSb(tmp,s.getNumSamples(),5)); 254 sb.append(" in "); 255 long elapsed = s.getElapsed(); 256 sb.append(doubleToSb(tmp,(double)elapsed/1000.0,5,1)); 257 sb.append("s = "); 258 if (elapsed > 0) 259 { 260 sb.append(doubleToSb(tmp,s.getRate(),6,1)); 261 } 262 else 263 { 264 sb.append("******"); } 266 sb.append("/s Avg: "); 267 sb.append(longToSb(tmp,s.getAverage(),5)); 268 sb.append(" Min: "); 269 sb.append(longToSb(tmp,s.getMin(),5)); 270 sb.append(" Max: "); 271 sb.append(longToSb(tmp,s.getMax(),5)); 272 sb.append(" Err: "); 273 sb.append(longToSb(tmp,s.getErrorCount(),5)); 274 sb.append(" ("); 275 sb.append(s.getErrorPercentageString()); 276 sb.append(")"); 277 return sb.toString(); 278 } 279 280 281 284 public void sampleStarted(SampleEvent e) 285 { 286 } 288 289 292 public void sampleStopped(SampleEvent e) { 293 } 295 296 299 public void testStarted() 300 { 301 } 303 306 public void testEnded() 307 { 308 testEnded("local"); 309 310 } 311 314 public void testStarted(String host) 315 { 316 } 318 325 public void testEnded(String host) 326 { 327 synchronized(accumulators){ 329 Totals t = (Totals) accumulators.get(myName); 330 if (t.last != -1){ 331 String str; 332 if (t.total.getNumSamples() != 0){ str = format(t.delta,"+"); 334 if (TOLOG) log.info(str); 335 if (TOOUT) System.out.println(str); 336 } 337 t.moveDelta(); 338 str = format(t.total,"="); 339 if (TOLOG) log.info(str); 340 if (TOOUT) System.out.println(str); 341 t.last = -1; 342 } 343 } 344 } 345 348 public void testIterationStart(LoopIterationEvent event) 349 { 350 } 352 353 } 354 | Popular Tags |