1 18 19 package org.apache.jmeter.visualizers; 20 21 import java.awt.Color ; 22 import java.awt.Dimension ; 23 import java.awt.Graphics ; 24 import java.awt.Rectangle ; 25 import java.util.Iterator ; 26 27 import javax.swing.JComponent ; 28 import javax.swing.Scrollable ; 29 import javax.swing.SwingUtilities ; 30 31 import org.apache.jmeter.gui.util.JMeterColor; 32 import org.apache.jmeter.samplers.Clearable; 33 import org.apache.jorphan.logging.LoggingManager; 34 import org.apache.log.Logger; 35 36 43 public class Graph 44 extends JComponent 45 implements Scrollable , GraphListener, Clearable 46 { 47 private static Logger log = LoggingManager.getLoggerForClass(); 48 private boolean wantData = true; 49 private boolean wantAverage = true; 50 private boolean wantDeviation = true; 51 private boolean wantThroughput = true; 52 private boolean wantMedian = true; 53 54 private GraphModel model; 55 private static int width = 2000; 56 57 60 public Graph() 61 { 62 this.setPreferredSize(new Dimension (width, 100)); 63 } 64 65 68 public Graph(GraphModel model) 69 { 70 this(); 71 setModel(model); 72 } 73 74 77 private void setModel(Object model) 78 { 79 this.model = (GraphModel) model; 80 this.model.addGraphListener(this); 81 repaint(); 82 } 83 84 89 public Dimension getPreferredScrollableViewportSize() 90 { 91 return this.getPreferredSize(); 92 } 94 95 99 public int getScrollableUnitIncrement( 100 Rectangle visibleRect, 101 int orientation, 102 int direction) 103 { 104 return 5; 105 } 106 107 111 public int getScrollableBlockIncrement( 112 Rectangle visibleRect, 113 int orientation, 114 int direction) 115 { 116 return (int) (visibleRect.width * .9); 117 } 118 119 124 public boolean getScrollableTracksViewportWidth() 125 { 126 return false; 127 } 128 129 134 public boolean getScrollableTracksViewportHeight() 135 { 136 return true; 137 } 138 139 142 public void clear() 143 {} 144 145 public void enableData(boolean value) 146 { 147 this.wantData = value; 148 } 149 150 public void enableAverage(boolean value) 151 { 152 this.wantAverage = value; 153 } 154 155 public void enableMedian(boolean value) 156 { 157 this.wantMedian = value; 158 } 159 160 public void enableDeviation(boolean value) 161 { 162 this.wantDeviation = value; 163 } 164 165 public void enableThroughput(boolean value) 166 { 167 this.wantThroughput = value; 168 } 169 170 public void updateGui() 171 { 172 repaint(); 173 } 174 175 public void updateGui(final Sample oneSample) 176 { 177 final int xPos = model.getSampleCount(); 178 179 SwingUtilities.invokeLater(new Runnable () 180 { 181 public void run() 182 { 183 Graphics g = getGraphics(); 184 185 if (g != null) 186 { 187 drawSample(xPos, oneSample, g); 188 } 189 } 190 }); 191 } 192 193 public void paintComponent(Graphics g) 194 { 195 super.paintComponent(g); 196 197 synchronized (model.getSamples()) 198 { 199 Iterator e = model.getSamples().iterator(); 200 201 for (int i = 0; e.hasNext(); i++) 202 { 203 Sample s = (Sample) e.next(); 204 205 drawSample(i, s, g); 206 } 207 } 208 } 209 210 private void drawSample(int x, Sample oneSample, Graphics g) 211 { 212 int height = getHeight(); 214 log.debug("Drawing a sample at " + x); 215 if (wantData) 216 { 217 int data = (int) (oneSample.data * height / model.getGraphMax()); 218 219 if (!oneSample.error) 220 { 221 g.setColor(Color.black); 222 } 223 else 224 { 225 g.setColor(JMeterColor.YELLOW); 226 } 227 g.drawLine(x % width, height - data, x % width, height - data - 1); 228 log.debug( 229 "Drawing coords = " + (x % width) + "," + (height - data)); 230 } 231 232 if (wantAverage) 233 { 234 int average = 235 (int) (oneSample.average * height / model.getGraphMax()); 236 237 g.setColor(Color.blue); 238 g.drawLine( 239 x % width, 240 height - average, 241 x % width, 242 (height - average - 1)); 243 } 244 245 if (wantMedian) 246 { 247 int median = 248 (int) (oneSample.median * height / model.getGraphMax()); 249 250 g.setColor(JMeterColor.purple); 251 g.drawLine( 252 x % width, 253 height - median, 254 x % width, 255 (height - median - 1)); 256 } 257 258 if (wantDeviation) 259 { 260 int deviation = 261 (int) (oneSample.deviation * height / model.getGraphMax()); 262 263 g.setColor(Color.red); 264 g.drawLine( 265 x % width, 266 height - deviation, 267 x % width, 268 (height - deviation - 1)); 269 } 270 if (wantThroughput) 271 { 272 int throughput = 273 (int) (oneSample.throughput 274 * height 275 / model.getThroughputMax()); 276 277 g.setColor(JMeterColor.dark_green); 278 g.drawLine( 279 x % width, 280 height - throughput, 281 x % width, 282 (height - throughput - 1)); 283 } 284 } 285 } 286 | Popular Tags |