1 17 package org.apache.jmeter.visualizers; 18 19 import java.awt.Color ; 20 import java.awt.Graphics ; 21 import java.awt.event.MouseEvent ; 22 import java.awt.event.MouseListener ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import javax.swing.JComponent ; 27 28 import org.apache.jmeter.samplers.Clearable; 29 30 34 public class MonitorGraph 35 extends JComponent 36 implements MouseListener , MonitorGuiListener, Clearable 37 { 38 protected MonitorAccumModel MODEL; 39 protected MonitorModel CURRENT; 40 41 protected boolean CPU = false; 42 protected boolean HEALTH = true; 43 protected boolean LOAD = true; 44 protected boolean MEM = true; 45 protected boolean THREAD = true; 46 protected boolean YGRID = true; 47 protected boolean XGRID = true; 48 49 protected int COUNT = 0; 50 protected int GRAPHMAX = 0; 51 52 56 public MonitorGraph() 57 { 58 } 60 61 64 public MonitorGraph(MonitorAccumModel model) 65 { 66 this.MODEL = model; 67 GRAPHMAX = model.getBufferSize(); 68 init(); 69 } 70 71 private void init(){ 72 repaint(); 73 } 74 75 public void setCpu(boolean cpu){ 76 this.CPU = cpu; 77 } 78 79 public void setHealth(boolean health){ 80 this.HEALTH = health; 81 } 82 83 public void setLoad(boolean load){ 84 this.LOAD = load; 85 } 86 87 public void setMem(boolean mem){ 88 this.MEM = mem; 89 } 90 91 public void setThread(boolean thread){ 92 this.THREAD = thread; 93 } 94 95 98 public void mouseClicked(MouseEvent e) 99 { 100 } 101 102 105 public void mouseEntered(MouseEvent e) 106 { 107 } 108 109 112 public void mouseExited(MouseEvent e) 113 { 114 } 115 116 119 public void mousePressed(MouseEvent e) 120 { 121 } 122 123 126 public void mouseReleased(MouseEvent e) 127 { 128 129 } 130 131 135 public void updateGui(final MonitorModel model) 136 { 137 if (this.isShowing()){ 138 this.CURRENT = model; 139 repaint(); 140 } 141 } 142 143 149 public void paintComponent(Graphics g) 150 { 151 super.paintComponent(g); 152 if (this.CURRENT != null){ 153 synchronized(MODEL){ 154 List samples = MODEL.getAllSamples(this.CURRENT.getURL()); 155 int size = samples.size(); 156 synchronized (samples) 157 { 158 Iterator e; 159 if (size > getWidth()){ 160 e = samples.listIterator(size - getWidth()); 161 } else { 162 e = samples.iterator(); 163 } 164 MonitorModel last = null; 165 for (int i = 0; e.hasNext(); i++) 166 { 167 MonitorModel s = (MonitorModel) e.next(); 168 if (last == null){ 169 last = s; 170 } 171 drawSample(i, s, g, last); 172 last = s; 173 } 174 } 175 } 176 } 177 } 178 179 182 public void updateGui() 183 { 184 repaint(); 185 } 186 187 190 public void clear() 191 { 192 paintComponent(getGraphics()); 193 this.repaint(); 194 } 195 196 private void drawSample(int x, MonitorModel model, 197 Graphics g, MonitorModel last) 198 { 199 double width = (double)getWidth(); 200 double height = (double)getHeight() - 10.0; 201 int xaxis = (int)(width * (x /width)); 202 int lastx = (int)(width * ((x - 1)/width)); 203 204 if (YGRID && x == 1){ 208 int q1 = (int)(height * 0.25); 209 int q2 = (int)(height * 0.50); 210 int q3 = (int)(height * 0.75); 211 g.setColor(Color.lightGray); 212 g.drawLine(0,q1,getWidth(),q1); 213 g.drawLine(0,q2,getWidth(),q2); 214 g.drawLine(0,q3,getWidth(),q3); 215 } 216 if (XGRID && x == 1){ 217 int x1 = (int)(width * 0.25); 218 int x2 = (int)(width * 0.50); 219 int x3 = (int)(width * 0.75); 220 g.drawLine(x1,0,x1,getHeight()); 221 g.drawLine(x2,0,x2,getHeight()); 222 g.drawLine(x3,0,x3,getHeight()); 223 g.drawLine(getWidth(),0,getWidth(),getHeight()); 224 } 225 226 if (HEALTH) 227 { 228 int hly = 229 (int)(height - (height * ((double)model.getHealth()/3.0))); 230 int lasty = 231 (int)(height - (height * ((double)last.getHealth()/3.0))); 232 233 g.setColor(Color.green); 234 g.drawLine(lastx,lasty,xaxis,hly); 235 } 236 237 if (LOAD) 238 { 239 int ldy = 240 (int)(height - (height * ((double)model.getLoad()/100.0))); 241 int lastldy = 242 (int)(height - (height * ((double)last.getLoad()/100.0))); 243 244 g.setColor(Color.blue); 245 g.drawLine(lastx,lastldy,xaxis,ldy); 246 } 247 248 if (MEM) 249 { 250 int mmy = 251 (int)(height - (height * ((double)model.getMemload()/100.0))); 252 int lastmmy = 253 (int)(height - (height * ((double)last.getMemload()/100.0))); 254 255 g.setColor(Color.orange); 256 g.drawLine(lastx,lastmmy,xaxis,mmy); 257 } 258 259 if (THREAD) 260 { 261 int thy = 262 (int)(height - (height * ((double)model.getThreadload()/100.0))); 263 int lastthy = 264 (int)(height - (height * ((double)last.getThreadload()/100.0))); 265 266 g.setColor(Color.red); 267 g.drawLine(lastx,lastthy,xaxis,thy); 268 } 269 } 270 271 } 272 | Popular Tags |