1 18 19 package org.apache.jmeter.visualizers; 21 22 import java.util.Collections ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.Vector ; 29 30 import org.apache.jmeter.samplers.Clearable; 31 import org.apache.jmeter.samplers.SampleResult; 32 33 34 44 public class StatVisualizerModel implements Clearable 45 { 46 private String name; 47 private List listeners; 48 private Vector runningSamples; 49 private Map labelMap; 50 private RunningSample total; 51 52 55 public StatVisualizerModel() 56 { 57 listeners = new LinkedList (); 58 runningSamples = new Vector (0, 10); 59 labelMap = Collections.synchronizedMap(new HashMap (10)); 60 total = new RunningSample("__TOTAL__", -1); 61 } 62 63 68 public void setName(String name) 69 { 70 this.name = name; 71 } 72 73 78 public Class getGuiClass() 79 { 80 return StatVisualizer.class; 81 } 82 83 88 public String getName() 89 { 90 return name; 91 } 92 93 97 public void addGraphListener(GraphListener listener) 98 { 99 listeners.add(listener); 100 } 101 102 public void addAccumListener(AccumListener listener) 103 { 104 listeners.add(listener); 105 } 106 107 public int getRunningSampleCount() 108 { 109 return runningSamples.size(); 110 } 111 112 public RunningSample getRunningSample(int index) 113 { 114 return (RunningSample) runningSamples.get(index); 115 } 116 117 public RunningSample getRunningSample(String label) 118 { 119 return (RunningSample) labelMap.get(label); 120 } 121 122 public RunningSample getRunningSampleTotal() 123 { 124 return total; 125 } 126 127 public void addNewSample(SampleResult res) 128 { 129 String aLabel = res.getSampleLabel(); 130 RunningSample s; 131 132 synchronized (labelMap) 133 { 134 s = (RunningSample) labelMap.get(aLabel); 135 if (s == null) 136 { 137 s = new RunningSample(aLabel, runningSamples.size()); 138 runningSamples.add(s); 139 labelMap.put(aLabel, s); 140 } 141 } 142 s.addSample(res); 143 total.addSample(res); 144 this.fireDataChanged(s); 145 } 146 147 150 public void clear() 151 { 152 runningSamples.clear(); 154 labelMap.clear(); 155 total = new RunningSample("__TOTAL__", -1); 156 this.fireDataChanged(); 157 } 158 159 163 protected void fireDataChanged() 164 { 165 Iterator iter = listeners.iterator(); 166 167 while (iter.hasNext()) 168 { 169 Object myObj = iter.next(); 170 171 if (!(myObj instanceof GraphListener)) 172 { 173 continue; 174 } 175 ((GraphListener) myObj).updateGui(); 176 } 177 } 178 179 protected void fireDataChanged(RunningSample s) 180 { 181 Iterator iter = listeners.iterator(); 182 183 while (iter.hasNext()) 184 { 185 Object myObj = iter.next(); 186 187 if (!(myObj instanceof AccumListener)) 188 { 189 continue; 190 } 191 ((AccumListener) myObj).updateGui(s); 192 } 193 } 194 195 public static class Test extends junit.framework.TestCase 196 { 197 public Test(String name) 198 { 199 super(name); 200 } 201 202 private SampleResult sample(String label, long start, 203 long elapsed, boolean ok) 204 { 205 SampleResult res = SampleResult.createTestSample(start, start+elapsed); 206 207 res.setSampleLabel(label); 208 res.setSuccessful(ok); 209 assertEquals(elapsed,res.getTime()); 210 return res; 211 } 212 213 public void testStatisticsCalculation() 214 { 215 StatVisualizerModel m = new StatVisualizerModel(); 216 long t0 = System.currentTimeMillis(); 217 218 219 223 226 m.addNewSample(sample("1", t0 + 000, 100, true)); 227 m.addNewSample(sample("2", t0 + 150, 200, true)); 228 m.addNewSample(sample("1", t0 + 300, 300, true)); 229 230 assertEquals(2, m.getRunningSampleCount()); 231 assertEquals(2, m.labelMap.size()); 232 233 { 234 RunningSample s = m.getRunningSample("1"); 235 236 assertEquals("1", s.getLabel()); 237 assertEquals(2, s.getNumSamples()); 238 assertEquals(100, s.getMin()); 239 assertEquals(300, s.getMax()); 240 assertEquals(200, s.getAverage()); 241 assertEquals(600, s.getElapsed()); 242 } 243 244 { 245 RunningSample s = m.getRunningSample("2"); 246 247 assertEquals("2", s.getLabel()); 248 assertEquals(1, s.getNumSamples()); 249 assertEquals(200, s.getMin()); 250 assertEquals(200, s.getMax()); 251 assertEquals(200, s.getAverage()); 252 assertEquals(200, s.getElapsed()); 253 } 254 255 { 256 RunningSample s = m.getRunningSampleTotal(); 257 258 assertEquals(3, s.getNumSamples()); 259 assertEquals(100, s.getMin()); 260 assertEquals(300, s.getMax()); 261 assertEquals(200, s.getAverage()); 262 assertEquals(600, s.getElapsed()); 263 assertEquals(5.0, s.getRate(), 1e-6); 264 } 265 } 266 } 267 } 268 | Popular Tags |