1 17 package org.apache.jmeter.visualizers; 18 19 import java.io.Serializable ; 20 import java.net.URL ; 21 import java.util.ArrayList ; 22 import java.util.Collections ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.LinkedList ; 27 28 import org.apache.jmeter.samplers.Clearable; 29 import org.apache.jmeter.samplers.SampleResult; 30 import org.apache.jmeter.monitor.model.ObjectFactory; 31 import org.apache.jmeter.monitor.model.Status; 32 import org.apache.jmeter.monitor.util.Stats; 33 import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult; 34 35 public class MonitorAccumModel implements Clearable, Serializable 36 { 37 38 private HashMap MAP; 39 private List listeners; 41 44 private int DEFAULT_BUFFER = 800; 45 46 49 public MonitorAccumModel() 50 { 51 MAP = new HashMap (); 52 listeners = new LinkedList (); 53 } 54 55 public int getBufferSize(){ 56 return DEFAULT_BUFFER; 57 } 58 59 public void setBufferSize(int buffer){ 60 DEFAULT_BUFFER = buffer; 61 } 62 63 69 public void addSample(MonitorModel model){ 70 if (MAP.containsKey(model.getURL())){ 72 List newlist = updateArray(model,(List )MAP.get(model.getURL())); 73 MAP.put(model.getURL(),newlist); 74 } else { 75 List samples = Collections.synchronizedList(new LinkedList ()); 76 samples.add(model); 77 MAP.put(model.getURL(),samples); 78 } 79 } 80 81 86 private List updateArray(MonitorModel model, List list){ 87 if (list.size() < DEFAULT_BUFFER){ 88 list.add(model); 89 } else { 90 list.add(model); 91 list.remove(0); 92 } 93 return list; 94 } 95 96 101 public List getAllSamples(String url){ 102 if (!MAP.containsKey(url)){ 103 return Collections.synchronizedList(new LinkedList ()); 104 } else { 105 return (List )MAP.get(url); 106 } 107 } 108 109 114 public MonitorModel getSample(String url){ 115 if (MAP.containsKey(url)){ 116 ArrayList list = (ArrayList )MAP.get(url); 117 return (MonitorModel)list.get(0); 118 } else { 119 return null; 120 } 121 } 122 123 132 public void addSample(SampleResult sample){ 133 URL surl = null; 134 if (sample instanceof HTTPSampleResult){ 135 surl = ((HTTPSampleResult)sample).getURL(); 136 } 137 if (sample.getResponseCode().equals("200") && 139 ((HTTPSampleResult)sample).isMonitor()){ 140 ObjectFactory of = ObjectFactory.getInstance(); 141 Status st = of.parseBytes(sample.getResponseData()); 142 if (st != null){ 143 MonitorStats stat = 144 new MonitorStats(Stats.calculateStatus(st), 145 Stats.calculateLoad(st), 146 0, 147 Stats.calculateMemoryLoad(st), 148 Stats.calculateThreadLoad(st), 149 surl.getHost(), 150 String.valueOf(surl.getPort()), 151 surl.getProtocol(), 152 System.currentTimeMillis()); 153 MonitorModel mo = new MonitorModel(stat); 154 this.addSample(mo); 155 notifyListeners(mo); 156 } else { 157 noResponse(surl); 158 } 159 } else if (((HTTPSampleResult)sample).isMonitor() ){ 160 noResponse(surl); 161 } 162 } 163 164 170 public void noResponse(URL url){ 171 notifyListeners(createNewMonitorModel(url)); 172 } 173 174 181 public MonitorModel createNewMonitorModel(URL url){ 182 MonitorStats stat = new MonitorStats(Stats.DEAD, 183 0, 184 0, 185 0, 186 0, 187 url.getHost(), 188 String.valueOf(url.getPort()), 189 url.getProtocol(), 190 System.currentTimeMillis()); 191 MonitorModel mo = new MonitorModel(stat); 192 return mo; 193 } 194 195 201 public void clear(){ 202 Iterator itr = this.MAP.keySet().iterator(); 203 while (itr.hasNext()){ 204 List lt = (List )this.MAP.get(itr.next()); 205 lt.clear(); 206 } 207 this.MAP.clear(); 208 } 209 210 214 public void notifyListeners(MonitorModel model) 215 { 216 for (int idx=0; idx < listeners.size(); idx++){ 217 MonitorListener ml = (MonitorListener)listeners.get(idx); 218 ml.addSample(model); 219 } 220 } 221 222 227 public void addListener(MonitorListener listener){ 228 listeners.add(listener); 229 } 230 } 231 | Popular Tags |