1 18 19 package org.apache.jmeter.visualizers; 20 21 22 import java.awt.BorderLayout ; 23 import java.awt.Dimension ; 24 import java.awt.event.MouseAdapter ; 25 import java.awt.event.MouseEvent ; 26 import java.util.Arrays ; 27 28 import javax.swing.BoxLayout ; 29 import javax.swing.JPanel ; 30 import javax.swing.JScrollPane ; 31 import javax.swing.JTable ; 32 import javax.swing.border.Border ; 33 import javax.swing.border.EmptyBorder ; 34 import javax.swing.event.TableModelEvent ; 35 import javax.swing.table.AbstractTableModel ; 36 import javax.swing.table.TableModel ; 37 38 import org.apache.jmeter.samplers.Clearable; 39 import org.apache.jmeter.samplers.SampleResult; 40 import org.apache.jmeter.testelement.TestElement; 41 import org.apache.jmeter.util.JMeterUtils; 42 import org.apache.jmeter.visualizers.gui.AbstractVisualizer; 43 44 45 53 public class StatVisualizer extends AbstractVisualizer 54 implements AccumListener, Clearable 55 { 56 protected JTable myJTable; 57 58 protected JScrollPane myScrollPane; 59 transient private StatVisualizerModel model; 60 transient private StatTableModel myStatTableModel; 61 62 public StatVisualizer() 63 { 64 super(); 65 model = new StatVisualizerModel(); 66 model.addAccumListener(this); 67 init(); 68 } 69 70 public String getLabelResource() 71 { 72 return "aggregate_report"; 73 } 74 75 public void add(SampleResult res) 76 { 77 model.addNewSample(res); 78 } 79 80 83 public void clear() 84 { 85 myStatTableModel.clear(); 86 model.clear(); 87 } 88 89 public synchronized void updateGui(RunningSample s) 90 { 91 myStatTableModel.rowChanged(s.getIndex()); 92 } 93 94 public TestElement createTestElement() 97 { 98 TestElement t = super.createTestElement(); 99 100 return t; 102 } 103 104 107 private void init() 108 { 109 this.setLayout(new BorderLayout ()); 110 111 JPanel mainPanel = new JPanel (); 113 Border margin = new EmptyBorder (10, 10, 5, 10); 114 115 mainPanel.setBorder(margin); 116 mainPanel.setLayout(new BoxLayout (mainPanel, BoxLayout.Y_AXIS)); 117 118 mainPanel.add(makeTitlePanel()); 119 myStatTableModel = new StatTableModel(model); 120 myJTable = new JTable (myStatTableModel); 123 myJTable.setPreferredScrollableViewportSize(new Dimension (500, 70)); 124 myScrollPane = new JScrollPane (myJTable); 125 this.add(mainPanel, BorderLayout.NORTH); 126 this.add(myScrollPane, BorderLayout.CENTER); 127 } 128 129 135 class StatTableModel extends AbstractTableModel 136 { 137 private final String [] columnNames = 138 { "URL", "Count", "Average", "Min", "Max", "Error%", "Rate" }; 139 private final Class [] columnClasses = 140 { 141 String .class, 142 Long .class, 143 Long .class, 144 Long .class, 145 Long .class, 146 String .class, 147 String .class }; 148 private final String TOTAL_LABEL = 149 JMeterUtils.getResString("aggregate_report_total_label"); 150 151 private transient StatVisualizerModel model; 152 private int currentRowCount = 0; 153 154 public StatTableModel(StatVisualizerModel model) 155 { 156 super(); 157 this.model = model; 158 } 159 160 public void rowChanged(int index) 161 { 162 TableModelEvent event; 163 164 synchronized (this) 167 { 168 if (index >= currentRowCount - 1) 169 { 170 event = 171 new TableModelEvent ( 172 this, 173 currentRowCount - 1, 174 index, 175 TableModelEvent.ALL_COLUMNS, 176 TableModelEvent.INSERT); 177 currentRowCount = index + 2; 178 } 179 else 180 { 181 event = new TableModelEvent (this, index); 182 } 183 } 184 fireTableChanged(event); 186 fireTableChanged(new TableModelEvent (this, currentRowCount)); 189 } 190 191 public int getColumnCount() 192 { 193 return columnNames.length; 194 } 195 196 public int getRowCount() 197 { 198 currentRowCount = model.getRunningSampleCount() + 1; 199 return currentRowCount; 200 } 201 202 public String getColumnName(int col) 203 { 204 return columnNames[col]; 205 } 206 207 public Object getValueAt(int row, int col) 208 { 209 RunningSample s; 210 211 if (row == model.getRunningSampleCount()) 212 { 213 if (col == 0) 214 { 215 return TOTAL_LABEL; 216 } 217 s = model.getRunningSampleTotal(); 218 } 219 else 220 { 221 s = model.getRunningSample(row); 222 } 223 224 switch (col) 225 { 226 case 0: 227 return s.getLabel(); 228 229 case 1: 230 return new Long (s.getNumSamples()); 231 232 case 2: 233 return new Long (s.getAverage()); 234 235 case 3: 236 return new Long (s.getMin()); 237 238 case 4: 239 return new Long (s.getMax()); 240 241 case 5: 242 return s.getErrorPercentageString(); 243 244 case 6: 245 return s.getRateString(); 246 247 default: 248 return "__ERROR__"; 249 } 250 } 251 252 public Class getColumnClass(int c) 253 { 254 return columnClasses[c]; 255 } 256 257 public void clear() 258 { 259 fireTableDataChanged(); 260 } 261 } 262 } 263 264 265 272 class SortFilterModel extends AbstractTableModel 273 { 274 private TableModel model; 275 private int sortColumn; 276 private Row[] rows; 277 278 public SortFilterModel(TableModel m) 279 { 280 model = m; 281 rows = new Row[model.getRowCount()]; 282 for (int i = 0; i < rows.length; i++) 283 { 284 rows[i] = new Row(); 285 rows[i].index = i; 286 } 287 } 288 289 public SortFilterModel() 290 { 291 } 292 293 public void setValueAt(Object aValue, int r, int c) 294 { 295 model.setValueAt(aValue, rows[r].index, c); 296 } 297 298 public Object getValueAt(int r, int c) 299 { 300 return model.getValueAt(rows[r].index, c); 301 } 302 303 public boolean isCellEditable(int r, int c) 304 { 305 return model.isCellEditable(rows[r].index, c); 306 } 307 308 public int getRowCount() 309 { 310 return model.getRowCount(); 311 } 312 313 public int getColumnCount() 314 { 315 return model.getColumnCount(); 316 } 317 318 public String getColumnName(int c) 319 { 320 return model.getColumnName(c); 321 } 322 323 public Class getColumnClass(int c) 324 { 325 return model.getColumnClass(c); 326 } 327 328 public void sort(int c) 329 { 330 sortColumn = c; 331 Arrays.sort(rows); 332 fireTableDataChanged(); 333 } 334 335 public void addMouseListener(final JTable table) 336 { 337 table.getTableHeader().addMouseListener(new MouseAdapter () 338 { 339 public void mouseClicked(MouseEvent event) 340 { 341 if (event.getClickCount() < 2) 342 { 343 return; 344 } 345 int tableColumn = table.columnAtPoint(event.getPoint()); 346 int modelColumn = table.convertColumnIndexToModel(tableColumn); 347 348 sort(modelColumn); 349 } 350 }); 351 } 352 353 private class Row implements Comparable 354 { 355 public int index; 356 357 public int compareTo(Object other) 358 { 359 Row otherRow = (Row) other; 360 Object a = model.getValueAt(index, sortColumn); 361 Object b = model.getValueAt(otherRow.index, sortColumn); 362 363 if (a instanceof Comparable ) 364 { 365 return ((Comparable ) a).compareTo(b); 366 } 367 else 368 { 369 return index - otherRow.index; 370 } 371 } 372 } 373 } | Popular Tags |