1 18 19 package org.apache.jmeter.visualizers; 20 21 22 import java.awt.BorderLayout ; 23 import java.awt.Color ; 24 import java.awt.Component ; 25 import java.awt.Dimension ; 26 import java.awt.FlowLayout ; 27 import java.awt.Image ; 28 import java.awt.event.ItemEvent ; 29 import java.awt.event.ItemListener ; 30 31 import javax.swing.BorderFactory ; 32 import javax.swing.Box ; 33 import javax.swing.JCheckBox ; 34 import javax.swing.JLabel ; 35 import javax.swing.JPanel ; 36 import javax.swing.JScrollPane ; 37 import javax.swing.JTextField ; 38 import javax.swing.border.Border ; 39 import javax.swing.border.EmptyBorder ; 40 41 import org.apache.jmeter.gui.util.JMeterColor; 42 import org.apache.jmeter.samplers.Clearable; 43 import org.apache.jmeter.samplers.SampleResult; 44 import org.apache.jmeter.util.JMeterUtils; 45 import org.apache.jmeter.visualizers.gui.AbstractVisualizer; 46 47 48 56 public class GraphVisualizer extends AbstractVisualizer 57 implements ImageVisualizer, ItemListener , GraphListener, Clearable 58 { 59 GraphModel model; 60 private JTextField maxYField = null; 61 private JTextField minYField = null; 62 private JTextField noSamplesField = null; 63 String minute = JMeterUtils.getResString("minute"); 64 65 private Graph graph; 66 private JCheckBox data; 67 private JCheckBox average; 68 private JCheckBox deviation; 69 private JCheckBox throughput; 70 private JCheckBox median; 71 private JTextField dataField; 72 private JTextField averageField; 73 private JTextField deviationField; 74 private JTextField throughputField; 75 private JTextField medianField; 76 77 80 public GraphVisualizer() 81 { 82 model = new GraphModel(); 83 model.addGraphListener(this); 84 graph = new Graph(model); 85 init(); 86 } 87 88 93 public Image getImage() 94 { 95 Image result = graph.createImage(graph.getWidth(), graph.getHeight()); 96 97 graph.paintComponent(result.getGraphics()); 98 99 return result; 100 } 101 102 public synchronized void updateGui() 103 { 104 graph.updateGui(); 105 noSamplesField.setText(Long.toString(model.getSampleCount())); 106 dataField.setText(Long.toString(model.getCurrentData())); 107 averageField.setText(Long.toString(model.getCurrentAverage())); 108 deviationField.setText(Long.toString(model.getCurrentDeviation())); 109 throughputField.setText( 110 Float.toString(model.getCurrentThroughput()) + "/" + minute); 111 medianField.setText(Long.toString(model.getCurrentMedian())); 112 updateYAxis(); 113 } 114 115 public synchronized void updateGui(Sample s) 116 { 117 graph.updateGui(s); 119 noSamplesField.setText(Long.toString(model.getSampleCount())); 120 dataField.setText(Long.toString(s.data)); 121 averageField.setText(Long.toString(s.average)); 122 deviationField.setText(Long.toString(s.deviation)); 123 throughputField.setText(Float.toString(s.throughput) + "/" + minute); 124 medianField.setText(Long.toString(s.median)); 125 updateYAxis(); 126 } 127 128 public void add(SampleResult res) 129 { 130 model.addSample(res); 131 } 132 133 public String getLabelResource() 134 { 135 return "graph_results_title"; 136 } 137 138 public void itemStateChanged(ItemEvent e) 139 { 140 if (e.getItem() == data) 141 { 142 this.graph.enableData(e.getStateChange() == ItemEvent.SELECTED); 143 } 144 else if (e.getItem() == average) 145 { 146 this.graph.enableAverage(e.getStateChange() == ItemEvent.SELECTED); 147 } 148 else if (e.getItem() == deviation) 149 { 150 this.graph.enableDeviation( 151 e.getStateChange() == ItemEvent.SELECTED); 152 } 153 else if (e.getItem() == throughput) 154 { 155 this.graph.enableThroughput( 156 e.getStateChange() == ItemEvent.SELECTED); 157 } 158 else if(e.getItem() == median) 159 { 160 this.graph.enableMedian(e.getStateChange() == ItemEvent.SELECTED); 161 } 162 this.graph.repaint(); 163 } 164 165 public synchronized void clear() 166 { 167 model.clear(); 169 dataField.setText("0000"); 170 averageField.setText("0000"); 171 deviationField.setText("0000"); 172 throughputField.setText("0/" + minute); 173 medianField.setText("0000"); 174 updateYAxis(); 175 repaint(); 176 } 177 178 public String toString() 179 { 180 return "Show the samples analysis as dot plots"; 181 } 182 183 186 private void updateYAxis() 187 { 188 maxYField.setText(Long.toString(model.getGraphMax())); 189 minYField.setText("0"); 190 } 191 192 195 private void init() 196 { 197 this.setLayout(new BorderLayout ()); 198 199 Border margin = new EmptyBorder (10, 10, 5, 10); 201 202 this.setBorder(margin); 203 204 205 JPanel graphPanel = new JPanel (new BorderLayout ()); 207 graphPanel.add(createYAxis(), BorderLayout.WEST); 208 graphPanel.add(createChoosePanel(), BorderLayout.NORTH); 209 graphPanel.add(createGraphPanel(), BorderLayout.CENTER); 210 graphPanel.add(createGraphInfoPanel(), BorderLayout.SOUTH); 211 212 this.add(makeTitlePanel(), BorderLayout.NORTH); 214 this.add(graphPanel, BorderLayout.CENTER); 215 } 216 217 219 224 private JPanel createYAxis() 225 { 226 JPanel graphYAxisPanel = new JPanel (); 227 228 graphYAxisPanel.setLayout(new BorderLayout ()); 229 230 maxYField = createYAxisField(5); 231 minYField = createYAxisField(3); 232 233 graphYAxisPanel.add(createYAxisPanel("graph_results_ms", maxYField), 234 BorderLayout.NORTH); 235 graphYAxisPanel.add(createYAxisPanel("graph_results_ms", minYField), 236 BorderLayout.SOUTH); 237 238 return graphYAxisPanel; 239 } 240 241 257 private JTextField createYAxisField(int length) 258 { 259 JTextField field = new JTextField (length); 260 field.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0)); 261 field.setEditable(false); 262 field.setForeground(Color.black); 263 field.setBackground(getBackground()); 264 field.setHorizontalAlignment(JTextField.RIGHT); 265 return field; 266 } 267 268 279 private JPanel createYAxisPanel(String labelResourceName, JTextField field) 280 { 281 JPanel panel = new JPanel (new FlowLayout ()); 282 JLabel label = new JLabel (JMeterUtils.getResString(labelResourceName)); 283 284 panel.add(field); 285 panel.add(label); 286 return panel; 287 } 288 289 290 298 private JPanel createChoosePanel() 299 { 300 JPanel chooseGraphsPanel = new JPanel (); 301 302 chooseGraphsPanel.setLayout(new FlowLayout ()); 303 JLabel selectGraphsLabel = new JLabel ( 304 JMeterUtils.getResString("graph_choose_graphs")); 305 data = createChooseCheckBox("graph_results_data", Color.black); 306 average = createChooseCheckBox("graph_results_average", Color.blue); 307 deviation = createChooseCheckBox("graph_results_deviation", Color.red); 308 throughput = createChooseCheckBox("graph_results_throughput", 309 JMeterColor.dark_green); 310 median = 311 createChooseCheckBox("graph_results_median", JMeterColor.purple); 312 313 chooseGraphsPanel.add(selectGraphsLabel); 314 chooseGraphsPanel.add(data); 315 chooseGraphsPanel.add(average); 316 chooseGraphsPanel.add(median); 317 chooseGraphsPanel.add(deviation); 318 chooseGraphsPanel.add(throughput); 319 return chooseGraphsPanel; 320 } 321 322 338 private JCheckBox createChooseCheckBox( 339 String labelResourceName, 340 Color color) 341 { 342 JCheckBox checkBox = new JCheckBox ( 343 JMeterUtils.getResString(labelResourceName)); 344 checkBox.setSelected(true); 345 checkBox.addItemListener(this); 346 checkBox.setForeground(color); 347 return checkBox; 348 } 349 350 351 357 private Component createGraphPanel() 358 { 359 JScrollPane graphScrollPanel = 360 makeScrollPane( 361 graph, 362 JScrollPane.VERTICAL_SCROLLBAR_NEVER, 363 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 364 graphScrollPanel.setViewportBorder( 365 BorderFactory.createEmptyBorder(2, 2, 2, 2)); 366 graphScrollPanel.setPreferredSize(graphScrollPanel.getMinimumSize()); 367 368 return graphScrollPanel; 369 } 370 371 372 378 private Box createGraphInfoPanel() 379 { 380 Box graphInfoPanel = Box.createHorizontalBox(); 381 382 noSamplesField = createInfoField(Color.black, 6); 383 dataField = createInfoField(Color.black, 5); 384 averageField = createInfoField(Color.blue, 5); 385 deviationField = createInfoField(Color.red, 5); 386 throughputField = createInfoField(JMeterColor.dark_green, 15); 387 medianField = createInfoField(JMeterColor.purple,5); 388 389 graphInfoPanel.add( 390 createInfoColumn( 391 createInfoLabel("graph_results_no_samples", noSamplesField), 392 noSamplesField, 393 createInfoLabel("graph_results_deviation", deviationField), 394 deviationField)); 395 graphInfoPanel.add(Box.createHorizontalGlue()); 396 397 graphInfoPanel.add( 398 createInfoColumn( 399 createInfoLabel("graph_results_latest_sample", dataField), 400 dataField, 401 createInfoLabel("graph_results_throughput", throughputField), 402 throughputField)); 403 graphInfoPanel.add(Box.createHorizontalGlue()); 404 405 graphInfoPanel.add( 406 createInfoColumn( 407 createInfoLabel("graph_results_average", averageField), 408 averageField, 409 createInfoLabel("graph_results_median", medianField), 410 medianField)); 411 graphInfoPanel.add(Box.createHorizontalGlue()); 412 return graphInfoPanel; 413 } 414 415 428 private JTextField createInfoField(Color color, int length) 429 { 430 JTextField field = new JTextField (length); 431 field.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); 432 field.setEditable(false); 433 field.setForeground(color); 434 field.setBackground(getBackground()); 435 436 field.setMaximumSize(new Dimension ( 439 field.getMaximumSize().width, 440 field.getPreferredSize().height)); 441 return field; 442 } 443 444 445 455 private JLabel createInfoLabel(String labelResourceName, JTextField field) 456 { 457 JLabel label = new JLabel ( 458 JMeterUtils.getResString(labelResourceName)); 459 label.setForeground(field.getForeground()); 460 label.setLabelFor(field); 461 return label; 462 } 463 464 487 private Box createInfoColumn( 488 JLabel label1, 489 JTextField field1, 490 JLabel label2, 491 JTextField field2) 492 { 493 Box row = Box.createHorizontalBox(); 497 Box col = Box.createVerticalBox(); 498 col.add(label1 != null ? label1 : Box.createVerticalGlue()); 499 col.add(label2 != null ? label2 : Box.createVerticalGlue()); 500 row.add(col); 501 502 row.add(Box.createHorizontalStrut(5)); 503 504 col = Box.createVerticalBox(); 505 col.add(field1 != null ? field1 : Box.createVerticalGlue()); 506 col.add(field2 != null ? field2 : Box.createVerticalGlue()); 507 row.add(col); 508 509 row.add(Box.createHorizontalStrut(5)); 510 511 return row; 512 } 513 } 514 | Popular Tags |