1 18 19 package org.apache.jmeter.visualizers; 20 21 import java.io.Serializable ; 22 import java.util.Collections ; 23 import java.util.Iterator ; 24 import java.util.LinkedList ; 25 import java.util.List ; 26 27 import org.apache.jmeter.samplers.Clearable; 28 import org.apache.jmeter.samplers.SampleResult; 29 import org.apache.jorphan.math.StatCalculator; 30 31 36 public class GraphModel implements Clearable, Serializable 37 { 38 39 private String name; 40 private List samples; 41 private List listeners; 42 private boolean bigChange = false; 44 private Sample current = new Sample(0, 0, 0, 0, 0,false); 45 private long startTime = Long.MAX_VALUE; 46 private long endTime = Long.MIN_VALUE; 47 private int throughputMax = 20; 48 private long graphMax = 20; 49 private StatCalculator statCalc = new StatCalculator(); 50 51 54 public GraphModel() 55 { 56 listeners = new LinkedList (); 57 samples = Collections.synchronizedList(new LinkedList ()); 58 } 59 60 65 66 public void setName(String name) 67 { 68 this.name = name; 69 } 70 71 76 public long getCurrentData() 77 { 78 return current.data; 79 } 80 81 86 public long getCurrentAverage() 87 { 88 return current.average; 89 } 90 91 public long getCurrentMedian() 92 { 93 return current.median; 94 } 95 96 101 public long getCurrentDeviation() 102 { 103 return current.deviation; 104 } 105 106 public float getCurrentThroughput() 107 { 108 return current.throughput; 109 } 110 111 116 public int getSampleCount() 117 { 118 return samples.size(); 119 } 120 121 126 public List getSamples() 127 { 128 return samples; 129 } 130 131 136 public Class getGuiClass() 137 { 138 return GraphVisualizer.class; 139 } 140 141 146 public String getName() 147 { 148 return name; 149 } 150 151 156 public long getMaxSample() 157 { 158 return statCalc.getMax().longValue(); 159 } 160 161 public long getGraphMax() 162 { 163 return graphMax; 164 } 165 166 public int getThroughputMax() 167 { 168 return throughputMax; 169 } 170 171 177 public void addGraphListener(GraphListener listener) 178 { 179 listeners.add(listener); 180 } 181 182 187 public Sample addSample(SampleResult e) 188 { 189 Sample s = 190 addNewSample(e.getTime(), e.getTimeStamp(), e.isSuccessful()); 191 192 fireDataChanged(); 193 return s; 194 } 195 196 public void clear() 197 { 198 samples.clear(); 199 graphMax = 1; 201 bigChange = true; 202 current = new Sample(0, 0, 0, 0, 0,false); 203 statCalc.clear(); 204 startTime = Long.MAX_VALUE; 205 endTime = Long.MIN_VALUE; 206 this.fireDataChanged(); 207 } 208 209 protected void fireDataChanged() 210 { 211 Iterator iter = listeners.iterator(); 212 213 if (bigChange) 214 { 215 while (iter.hasNext()) 216 { 217 ((GraphListener) iter.next()).updateGui(); 218 } 219 bigChange = false; 220 } 221 else 222 { 223 quickUpdate(current); 224 } 225 } 226 227 protected void quickUpdate(Sample s) 228 { 229 Iterator iter = listeners.iterator(); 230 { 231 while (iter.hasNext()) 232 { 233 ((GraphListener) iter.next()).updateGui(s); 234 } 235 } 236 } 237 238 243 protected Sample addNewSample(long sample, long timeStamp, boolean success) 244 { 245 float average; 247 long deviation, median; 248 synchronized (statCalc) 249 { 250 statCalc.addValue(sample); 251 average = (float) statCalc.getMean(); 253 deviation = (long) statCalc.getStandardDeviation(); 254 median = statCalc.getMedian().longValue(); 255 long start = timeStamp - sample; 256 if (startTime > start) 257 { 258 startTime = start; 259 } 260 if(endTime < timeStamp) 261 { 262 endTime = timeStamp; 263 } 264 } 265 266 float throughput = 0; 267 268 if (endTime - startTime > 0) 269 { 270 throughput = 271 (float) (((float) (samples.size() + 1)) 272 / ((float) (timeStamp - startTime)) 273 * 60000); 274 } 275 if (throughput > throughputMax) 276 { 277 bigChange = true; 278 throughputMax = (int) (throughput * 1.5F); 279 } 280 if (average > graphMax) 281 { 282 bigChange = true; 283 graphMax = (long) average * 3; 284 } 285 if (deviation > graphMax) 286 { 287 bigChange = true; 288 graphMax = deviation * 3; 289 } 290 Sample s = 291 new Sample( 292 sample, 293 (long) average, 294 deviation, 295 throughput, 296 median, 297 !success); 298 299 current = s; 301 samples.add(s); 302 return s; 303 } 304 } 305 | Popular Tags |