1 18 19 package org.apache.jmeter.visualizers; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Color ; 23 import java.awt.Dimension ; 24 import java.awt.Graphics ; 25 import java.awt.GridLayout ; 26 import java.awt.Image ; 27 28 import javax.swing.BorderFactory ; 29 import javax.swing.JComponent ; 30 import javax.swing.JLabel ; 31 import javax.swing.JPanel ; 32 import javax.swing.border.Border ; 33 import javax.swing.border.EmptyBorder ; 34 35 import org.apache.jmeter.samplers.Clearable; 36 import org.apache.jmeter.samplers.SampleResult; 37 import org.apache.jmeter.util.JMeterUtils; 38 import org.apache.jmeter.visualizers.gui.AbstractVisualizer; 39 import org.apache.jorphan.gui.layout.VerticalLayout; 40 41 48 public class SplineVisualizer 49 extends AbstractVisualizer 50 implements ImageVisualizer, GraphListener, Clearable 51 { 52 53 protected final Color BACKGROUND_COLOR = getBackground(); 54 protected final Color MINIMUM_COLOR = new Color (0F, 0.5F, 0F); 55 protected final Color MAXIMUM_COLOR = new Color (0.9F, 0F, 0F); 56 protected final Color AVERAGE_COLOR = new Color (0F, 0F, 0.75F); 57 protected final Color INCOMING_COLOR = Color.black; 58 protected final int NUMBERS_TO_DISPLAY = 4; 59 60 protected final boolean FILL_UP_WITH_ZEROS = false; 61 62 transient private SplineGraph graph = null; 63 64 private JLabel minimumLabel = null; 65 private JLabel maximumLabel = null; 66 private JLabel averageLabel = null; 67 private JLabel incomingLabel = null; 68 69 private JLabel minimumNumberLabel = null; 70 private JLabel maximumNumberLabel = null; 71 private JLabel averageNumberLabel = null; 72 private JLabel incomingNumberLabel = null; 73 transient private SplineModel model; 74 75 public SplineVisualizer() 76 { 77 super(); 78 model = new SplineModel(); 79 graph = new SplineGraph(); 80 this.model.setListener(this); 81 setGUI(); 82 } 83 84 public void add(SampleResult res) 85 { 86 model.add(res); 87 } 88 89 public String getLabelResource() 90 { 91 return "spline_visualizer_title"; 92 } 93 94 public void updateGui(Sample s) 95 { 96 updateGui(); 97 } 98 99 public void clear() 100 { 101 model.clear(); 102 } 103 104 public void setGUI() 105 { 106 Color backColor = BACKGROUND_COLOR; 107 108 this.setBackground(backColor); 109 110 this.setLayout(new BorderLayout ()); 111 112 JPanel mainPanel = new JPanel (); 114 Border margin = new EmptyBorder (10, 10, 5, 10); 115 116 mainPanel.setBorder(margin); 117 mainPanel.setLayout(new VerticalLayout(5, VerticalLayout.LEFT)); 118 119 mainPanel.add(makeTitlePanel()); 121 122 maximumLabel = 123 new JLabel (JMeterUtils.getResString("spline_visualizer_maximum")); 124 maximumLabel.setForeground(MAXIMUM_COLOR); 125 maximumLabel.setBackground(backColor); 126 127 averageLabel = 128 new JLabel (JMeterUtils.getResString("spline_visualizer_average")); 129 averageLabel.setForeground(AVERAGE_COLOR); 130 averageLabel.setBackground(backColor); 131 132 incomingLabel = 133 new JLabel (JMeterUtils.getResString("spline_visualizer_incoming")); 134 incomingLabel.setForeground(INCOMING_COLOR); 135 incomingLabel.setBackground(backColor); 136 137 minimumLabel = 138 new JLabel (JMeterUtils.getResString("spline_visualizer_minimum")); 139 minimumLabel.setForeground(MINIMUM_COLOR); 140 minimumLabel.setBackground(backColor); 141 142 maximumNumberLabel = new JLabel ("0 ms"); 143 maximumNumberLabel.setHorizontalAlignment(JLabel.RIGHT); 144 maximumNumberLabel.setForeground(MAXIMUM_COLOR); 145 maximumNumberLabel.setBackground(backColor); 146 147 averageNumberLabel = new JLabel ("0 ms"); 148 averageNumberLabel.setHorizontalAlignment(JLabel.RIGHT); 149 averageNumberLabel.setForeground(AVERAGE_COLOR); 150 averageNumberLabel.setBackground(backColor); 151 152 incomingNumberLabel = new JLabel ("0 ms"); 153 incomingNumberLabel.setHorizontalAlignment(JLabel.RIGHT); 154 incomingNumberLabel.setForeground(INCOMING_COLOR); 155 incomingNumberLabel.setBackground(backColor); 156 157 minimumNumberLabel = new JLabel ("0 ms"); 158 minimumNumberLabel.setHorizontalAlignment(JLabel.RIGHT); 159 minimumNumberLabel.setForeground(MINIMUM_COLOR); 160 minimumNumberLabel.setBackground(backColor); 161 162 JPanel labelPanel = new JPanel (); 164 165 labelPanel.setLayout(new GridLayout (0, 1)); 166 labelPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20)); 167 labelPanel.setBackground(backColor); 168 labelPanel.add(maximumLabel); 169 labelPanel.add(averageLabel); 170 if (model.SHOW_INCOMING_SAMPLES) 171 { 172 labelPanel.add(incomingLabel); 173 } 174 labelPanel.add(minimumLabel); 175 JPanel numberPanel = new JPanel (); 177 178 numberPanel.setLayout(new GridLayout (0, 1)); 179 numberPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20)); 180 numberPanel.setBackground(backColor); 181 numberPanel.add(maximumNumberLabel); 182 numberPanel.add(averageNumberLabel); 183 if (model.SHOW_INCOMING_SAMPLES) 184 { 185 numberPanel.add(incomingNumberLabel); 186 } 187 numberPanel.add(minimumNumberLabel); 188 JPanel infoPanel = new JPanel (); 190 191 infoPanel.setLayout(new BorderLayout ()); 192 infoPanel.add(labelPanel, BorderLayout.CENTER); 193 infoPanel.add(numberPanel, BorderLayout.EAST); 194 195 this.add(mainPanel, BorderLayout.NORTH); 196 this.add(infoPanel, BorderLayout.WEST); 197 this.add(graph, BorderLayout.CENTER); 198 } 201 202 public void updateGui() 203 { 204 repaint(); 205 synchronized (this) 206 { 207 setMinimum(model.getMinimum()); 208 setMaximum(model.getMaximum()); 209 setAverage(model.getAverage()); 210 setIncoming(model.getCurrent()); 211 } 212 } 213 214 public String toString() 215 { 216 return "Show the samples analysis as a Spline curve"; 217 } 218 219 public String formatMeasureToDisplay(long measure) 220 { 221 String numberString = String.valueOf(measure); 222 223 if (FILL_UP_WITH_ZEROS) 224 { 225 for (int i = numberString.length(); i < NUMBERS_TO_DISPLAY; i++) 226 { 227 numberString = "0" + numberString; 228 } 229 } 230 return numberString; 231 } 232 233 public void setMinimum(long n) 234 { 235 String text = this.formatMeasureToDisplay(n) + " ms"; 236 237 this.minimumNumberLabel.setText(text); 238 } 239 240 public void setMaximum(long n) 241 { 242 String text = this.formatMeasureToDisplay(n) + " ms"; 243 244 this.maximumNumberLabel.setText(text); 245 } 246 247 public void setAverage(long n) 248 { 249 String text = this.formatMeasureToDisplay(n) + " ms"; 250 251 this.averageNumberLabel.setText(text); 252 } 253 254 public void setIncoming(long n) 255 { 256 String text = this.formatMeasureToDisplay(n) + " ms"; 257 258 this.incomingNumberLabel.setText(text); 259 } 260 261 public JPanel getControlPanel() 262 { 263 return this; 264 } 265 266 public Image getImage() 267 { 268 Image result = graph.createImage(graph.getWidth(), graph.getHeight()); 269 270 graph.paintComponent(result.getGraphics()); 271 272 return result; 273 } 274 275 280 public class SplineGraph extends JComponent 281 { 282 public boolean reinterpolated = false; 283 protected final Color WAITING_COLOR = Color.darkGray; 284 protected int lastWidth = -1; 285 protected int lastHeight = -1; 286 protected int[] plot = null; 287 288 public SplineGraph() 289 { 290 } 291 292 295 public void clear() 296 { 297 lastWidth = -1; 298 lastHeight = -1; 299 plot = null; 300 this.repaint(); 301 } 302 303 public void paintComponent(Graphics g) 304 { 305 super.paintComponent(g); 306 307 Dimension dimension = this.getSize(); 308 int width = dimension.width; 309 int height = dimension.height; 310 311 if (model.getDataCurve() == null) 312 { 313 g.setColor(this.getBackground()); 314 g.fillRect(0, 0, width, height); 315 g.setColor(WAITING_COLOR); 316 g.drawString( 317 JMeterUtils.getResString( 318 "spline_visualizer_waitingmessage"), 319 (width - 120) / 2, 320 height - (height - 12) / 2); 321 return; 322 } 323 324 326 if (width == lastWidth && height == lastHeight) 327 { 328 } 331 else 332 { 333 lastWidth = width; 336 lastHeight = height; 337 } 338 339 this.plot = model.getDataCurve().getPlots(width, height); 341 int n = plot.length; 342 int curY = plot[0]; 343 344 for (int i = 1; i < n; i++) 345 { 346 g.setColor(Color.black); 347 g.drawLine(i - 1, height - curY - 1, i, height - plot[i] - 1); 348 curY = plot[i]; 349 } 350 } 351 } 352 } 353 | Popular Tags |