1 18 19 package org.apache.jmeter.visualizers; 20 21 import java.text.DecimalFormat ; 22 23 import org.apache.jmeter.samplers.SampleResult; 24 25 26 34 public class RunningSample 35 { 36 37 private static DecimalFormat rateFormatter = new DecimalFormat ("#.0"); 38 private static DecimalFormat errorFormatter = new DecimalFormat ("#0.00%"); 39 40 private long counter; 41 private volatile long runningSum; 42 private volatile long max, min; 43 private volatile long errorCount; 44 private volatile long firstTime; 45 private volatile long lastTime; 46 private String label; 47 private int index; 48 49 private RunningSample(){ } 51 52 55 public RunningSample(String label, int index) 56 { 57 this.label = label; 58 this.index = index; 59 init(); 60 } 61 62 67 public RunningSample(RunningSample src){ 68 this.counter = src.counter; 69 this.errorCount = src.errorCount; 70 this.firstTime = src.firstTime; 71 this.index = src.index; 72 this.label = src.label; 73 this.lastTime = src.lastTime; 74 this.max = src.max; 75 this.min = src.min; 76 this.runningSum = src.runningSum; 77 } 78 79 private void init(){ 80 counter = 0L; 81 runningSum = 0L; 82 max = Long.MIN_VALUE; 83 min = Long.MAX_VALUE; 84 errorCount = 0L; 85 firstTime = Long.MAX_VALUE; 86 lastTime = 0L; 87 } 88 89 93 public synchronized void clear(){ 94 init(); 95 } 96 97 101 public long getElapsed(){ 102 if (lastTime == 0) return 0; return lastTime - firstTime; 104 } 105 111 public double getRate() 112 { 113 if (counter == 0) return 0.0; 115 long howLongRunning = lastTime - firstTime; 116 117 if (howLongRunning == 0) 118 { 119 return Double.MAX_VALUE; 120 } 121 122 return (double) counter / howLongRunning * 1000.0; 123 } 124 125 131 public double getRatePerMin() 132 { 133 if (counter == 0) return 0.0; 135 long howLongRunning = lastTime - firstTime; 136 137 if (howLongRunning == 0) 138 { 139 return Double.MAX_VALUE; 140 } 141 return (double) counter / howLongRunning * 60000.0; 142 } 143 144 159 public String getRateString() 160 { 161 double rate = getRate(); 162 163 if (rate == Double.MAX_VALUE) 164 { 165 return "N/A"; 166 } 167 168 String unit = "sec"; 169 170 if (rate < 1.0) 171 { 172 rate *= 60.0; 173 unit = "min"; 174 } 175 if (rate < 1.0) 176 { 177 rate *= 60.0; 178 unit = "hour"; 179 } 180 181 String rval = rateFormatter.format(rate) + "/" + unit; 182 183 return (rval); 184 } 185 186 public String getLabel() 187 { 188 return label; 189 } 190 191 public int getIndex() 192 { 193 return index; 194 } 195 196 200 public synchronized void addSample(SampleResult res) 201 { 202 long aTimeInMillis = res.getTime(); 203 boolean aSuccessFlag = res.isSuccessful(); 204 205 counter++; 206 long startTime = res.getTimeStamp() - aTimeInMillis; 207 if (firstTime > startTime) 208 { 209 firstTime = startTime; 211 } 212 if(lastTime < res.getTimeStamp()) 213 { 214 lastTime = res.getTimeStamp(); 215 } 216 runningSum += aTimeInMillis; 217 218 if (aTimeInMillis > max) 219 { 220 max = aTimeInMillis; 221 } 222 223 if (aTimeInMillis < min) 224 { 225 min = aTimeInMillis; 226 } 227 228 if (!aSuccessFlag) 229 { 230 errorCount++; 231 } 232 } 233 234 235 239 public synchronized void addSample(RunningSample rs) 240 { 241 this.counter += rs.counter; 242 this.errorCount += rs.errorCount; 243 this.runningSum += rs.runningSum; 244 if (this.firstTime > rs.firstTime) this.firstTime = rs.firstTime; 245 if (this.lastTime < rs.lastTime) this.lastTime = rs.lastTime; 246 if (this.max < rs.max) this.max = rs.max; 247 if (this.min > rs.min) this.min = rs.min; 248 } 249 250 251 255 public long getMin() 256 { 257 long rval = 0; 258 259 if (min != Long.MAX_VALUE) 260 { 261 rval = min; 262 } 263 return (rval); 264 } 265 266 270 public long getMax() 271 { 272 long rval = 0; 273 274 if (max != Long.MIN_VALUE) 275 { 276 rval = max; 277 } 278 return (rval); 279 } 280 281 285 public long getAverage() 286 { 287 if (counter == 0) 288 { 289 return (0); 290 } 291 return (runningSum / counter); 292 } 293 294 300 public long getNumSamples() 301 { 302 return (counter); 303 } 304 305 314 public double getErrorPercentage() 315 { 316 double rval = 0.0; 317 318 if (counter == 0) 319 { 320 return (rval); 321 } 322 rval = (double) errorCount / (double) counter; 323 return (rval); 324 } 325 326 333 public String getErrorPercentageString() 334 { 335 double myErrorPercentage = this.getErrorPercentage(); 336 337 return (errorFormatter.format(myErrorPercentage)); 338 } 339 340 343 public String toString() 344 { 345 StringBuffer mySB = new StringBuffer (); 346 347 mySB.append("Samples: " + this.getNumSamples() + " "); 348 mySB.append("Avg: " + this.getAverage() + " "); 349 mySB.append("Min: " + this.getMin() + " "); 350 mySB.append("Max: " + this.getMax() + " "); 351 mySB.append("Error Rate: " + this.getErrorPercentageString() + " "); 352 mySB.append("Sample Rate: " + this.getRateString()); 353 return (mySB.toString()); 354 } 355 356 359 public long getErrorCount() { 360 return errorCount; 361 } 362 363 } | Popular Tags |