1 18 19 package org.apache.jmeter.visualizers; 20 21 22 import java.awt.Color ; 23 import java.awt.Dimension ; 24 import java.awt.Graphics ; 25 import java.awt.GridBagConstraints ; 26 import java.awt.GridBagLayout ; 27 import java.awt.Insets ; 28 import java.awt.Point ; 29 import java.awt.Rectangle ; 30 import java.util.Iterator ; 31 32 import javax.swing.JComponent ; 33 import javax.swing.JLabel ; 34 import javax.swing.JPanel ; 35 import javax.swing.Scrollable ; 36 import javax.swing.SwingUtilities ; 37 38 import org.apache.jmeter.samplers.SampleResult; 39 import org.apache.jmeter.util.ColorHelper; 40 import org.apache.jorphan.logging.LoggingManager; 41 import org.apache.log.Logger; 42 43 44 51 public class GraphAccum extends JComponent implements Scrollable , 52 GraphAccumListener 53 { 54 protected GraphAccumModel model; 55 protected GraphAccumVisualizer visualizer; 56 57 58 protected boolean noLegendYet = true; 59 60 64 protected Point [] previousPts; 65 66 71 protected boolean previousPtsAlloc = false; 72 73 protected final static int width = 2000; 74 75 protected final static int PLOT_X_WIDTH = 10; 76 transient private static Logger log = LoggingManager.getLoggerForClass(); 77 78 79 82 public GraphAccum() 83 { 84 log.debug("Start : GraphAnnum1"); 85 log.debug("End : GraphAnnum1"); 86 } 87 88 93 public GraphAccum(GraphAccumModel model) 94 { 95 this(); 96 log.debug("Start : GraphAnnum2"); 97 setModel(model); 98 log.debug("End : GraphAnnum2"); 99 } 100 101 106 private void setModel(Object model) 107 { 108 log.debug("Start : setModel1"); 109 this.model = (GraphAccumModel) model; 110 this.model.addGraphAccumListener(this); 111 repaint(); 112 log.debug("End : setModel1"); 113 } 114 115 120 public void setVisualizer(Object visualizer) 121 { 122 if (log.isDebugEnabled()) 123 { 124 log.debug("setVisualizer1 : Setting visualizer - " + visualizer); 125 } 126 this.visualizer = (GraphAccumVisualizer) visualizer; 127 } 128 129 136 public void setNoLegendYet(boolean value) 137 { 138 noLegendYet = value; 139 } 140 141 146 public Dimension getPreferredScrollableViewportSize() 147 { 148 return this.getPreferredSize(); 149 } 150 151 155 public int getScrollableUnitIncrement( 156 Rectangle visibleRect, 157 int orientation, 158 int direction) 159 { 160 return 5; 161 } 162 163 167 public int getScrollableBlockIncrement( 168 Rectangle visibleRect, 169 int orientation, 170 int direction) 171 { 172 return (int) (visibleRect.width * .9); 173 } 174 175 180 public boolean getScrollableTracksViewportWidth() 181 { 182 return false; 183 } 184 185 190 public boolean getScrollableTracksViewportHeight() 191 { 192 return true; 193 } 194 195 202 public boolean getNoLegendYet() 203 { 204 return noLegendYet; 205 } 206 207 210 public void updateGui() 211 { 212 log.debug("Start : updateGui1"); 213 repaint(); 214 log.debug("End : updateGui1"); 215 } 216 217 222 public void updateGui(final SampleResult oneSample) 223 { 224 log.debug("Start : updateGui2"); 225 final int xPos = model.getSampleCount(); 226 227 SwingUtilities.invokeLater( 228 new Runnable () 229 { 230 public void run() 231 { 232 Graphics g = getGraphics(); 233 234 if (g != null) 235 { 236 drawSample(xPos * PLOT_X_WIDTH, oneSample, g); 237 } 238 } 239 } 240 ); 241 log.debug("End : updateGui2"); 242 } 243 244 public void paintComponent(Graphics g) 245 { 246 super.paintComponent(g); 247 log.debug("Start : paintComponent1"); 248 249 synchronized (model.getList()) 250 { 251 previousPtsAlloc = false; 254 Iterator e = model.getList().iterator(); 255 256 for (int i = 0; e.hasNext(); i++) 257 { 258 SampleResult s = (SampleResult) e.next(); 259 260 drawSample(i * PLOT_X_WIDTH, s, g); 261 } 262 } 263 log.debug("End : paintComponent1"); 264 } 265 266 269 public void clear() 270 { 271 setNoLegendYet(true); 272 ((JPanel ) visualizer.getWhiteCanvas()).removeAll(); 273 previousPts = null; 274 } 275 276 private void drawSample(int x, SampleResult oneSample, Graphics g) 277 { 278 log.debug("Start : drawSample1"); 279 280 int lastLevel = 0; 282 283 int compCount = 0; 285 286 SampleResult[] resultList = oneSample.getSubResults(); 287 int resultListCount = 0; 288 289 if (!previousPtsAlloc) 291 { 292 resultListCount += resultList.length; 293 previousPts = new Point [resultListCount + 2]; 294 } 295 296 Color currColor = Color.black; 297 JPanel lPanel = (JPanel ) visualizer.getWhiteCanvas(); 298 JPanel legendPanel = new JPanel (); 299 GridBagLayout gridBag = new GridBagLayout (); 300 GridBagConstraints gbc = new GridBagConstraints (); 301 302 legendPanel.setLayout(gridBag); 303 lPanel.add(legendPanel); 304 Dimension d = this.getSize(); 305 306 long totalTime = oneSample.getTime(); 308 309 if (log.isDebugEnabled()) 312 { 313 log.debug("drawSample1 : total time - " + totalTime); 314 } 315 int data = (int) (totalTime * d.height / model.getMax()); 316 317 g.setColor(currColor); 318 if (!previousPtsAlloc) 319 { 320 g.drawLine( 322 x % width, 323 d.height - data, 324 x % width, 325 d.height - data - 1); 326 } 327 else 328 { 329 g.drawLine((previousPts[0].x) % width, previousPts[0].y, x % width, 331 d.height - data); 332 } 333 334 previousPts[0] = new Point (x % width, d.height - data); 336 if (noLegendYet) 337 { 338 gbc.gridx = 0; 339 gbc.gridy = compCount++; 340 gbc.anchor = GridBagConstraints.WEST; 341 gbc.weightx = 1.0; 342 gbc.insets = new Insets (0, 10, 0, 0); 343 JLabel totalTimeLabel = 344 new JLabel ("Total time - " + oneSample.toString()); 345 346 totalTimeLabel.setForeground(currColor); 347 gridBag.setConstraints(totalTimeLabel, gbc); 348 legendPanel.add(totalTimeLabel); 349 } 350 351 if (log.isDebugEnabled()) 353 { 354 log.debug( 355 "drawSample1 : main page load time - " + oneSample.getTime()); 356 } 357 data = (int) (oneSample.getTime() * d.height / model.getMax()); 358 currColor = ColorHelper.changeColorCyclicIncrement(currColor, 40); 359 g.setColor(currColor); 360 if (!previousPtsAlloc) 361 { 362 g.drawLine( 364 x % width, 365 d.height - data, 366 x % width, 367 d.height - data - 1); 368 } 369 else 370 { 371 g.drawLine((previousPts[1].x) % width, previousPts[1].y, x % width, 373 d.height - data); 374 } 375 previousPts[1] = new Point (x % width, d.height - data); 377 if (noLegendYet) 378 { 379 gbc.gridx = 0; 380 gbc.gridy = compCount++; 381 gbc.anchor = GridBagConstraints.WEST; 382 gbc.weightx = 1.0; 383 gbc.insets = new Insets (0, 10, 0, 0); 384 JLabel mainTimeLabel = new JLabel (oneSample.toString()); 385 386 mainTimeLabel.setForeground(currColor); 387 gridBag.setConstraints(mainTimeLabel, gbc); 388 legendPanel.add(mainTimeLabel); 389 } 390 lastLevel += data; 391 int currPreviousPts = 2; 393 394 if (resultList != null) 395 { 396 for (int i = 0; i < resultList.length; i++) 397 { 398 SampleResult componentRes = (SampleResult) resultList[i]; 399 400 if (log.isDebugEnabled()) 401 { 402 log.debug( 403 "drawSample1 : componentRes - " 404 + componentRes.getSampleLabel() 405 + " loading time - " 406 + componentRes.getTime()); 407 } 408 data = 409 (int) (componentRes.getTime() * d.height / model.getMax()); 410 data += lastLevel; 411 currColor = 412 ColorHelper.changeColorCyclicIncrement(currColor, 100); 413 g.setColor(currColor); 414 if (!previousPtsAlloc) 415 { 416 g.drawLine( 418 x % width, 419 d.height - data, 420 x % width, 421 d.height - data - 1); 422 } 423 else 424 { 425 g.drawLine( 427 (previousPts[currPreviousPts].x) % width, 428 previousPts[currPreviousPts].y, 429 x % width, 430 d.height - data); 431 } 432 previousPts[currPreviousPts++] = 434 new Point (x % width, d.height - data); 435 if (noLegendYet) 436 { 437 gbc.gridx = 0; 438 gbc.gridy = compCount++; 439 gbc.anchor = GridBagConstraints.WEST; 440 gbc.weightx = 1.0; 441 gbc.insets = new Insets (0, 10, 0, 0); 442 JLabel compTimeLabel = 443 new JLabel (componentRes.getSampleLabel()); 444 445 compTimeLabel.setForeground(currColor); 446 gridBag.setConstraints(compTimeLabel, gbc); 447 legendPanel.add(compTimeLabel); 448 } 449 lastLevel = data; 450 } 451 } 452 453 if (noLegendYet) 454 { 455 noLegendYet = false; 456 lPanel.repaint(); 457 lPanel.revalidate(); 458 } 459 460 if (!previousPtsAlloc) 465 { 466 previousPtsAlloc = true; 467 } 468 log.debug("End : drawSample1"); 469 } 470 } 471 | Popular Tags |