001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jmeter.visualizers;
018:
019: import java.awt.Color;
020: import java.awt.Graphics;
021: import java.awt.event.MouseEvent;
022: import java.awt.event.MouseListener;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import javax.swing.JComponent;
027:
028: import org.apache.jmeter.samplers.Clearable;
029:
030: /**
031: * MonitorGraph will draw the performance history of a given server. It displays
032: * 4 lines:
033: * <p>
034: */
035: public class MonitorGraph extends JComponent implements MouseListener,
036: MonitorGuiListener, Clearable {
037: // NOTUSED protected static int width = 500;
038: private MonitorAccumModel MODEL;
039:
040: private MonitorModel CURRENT;
041:
042: private boolean CPU = false;// TODO is this needed? It's never read
043:
044: private boolean HEALTH = true;
045:
046: private boolean LOAD = true;
047:
048: private boolean MEM = true;
049:
050: private boolean THREAD = true;
051:
052: private boolean YGRID = true;
053:
054: private boolean XGRID = true;
055:
056: private int GRAPHMAX = 0;// TODO is this needed? It's never read
057:
058: /**
059: *
060: * @deprecated Only for use in unit testing
061: */
062: public MonitorGraph() {
063: // log.warn("Only for use in unit testing");
064: }
065:
066: /**
067: *
068: */
069: public MonitorGraph(MonitorAccumModel model) {
070: this .MODEL = model;
071: GRAPHMAX = model.getBufferSize();
072: init();
073: }
074:
075: private void init() {
076: repaint();
077: }
078:
079: public void setCpu(boolean cpu) {
080: this .CPU = cpu;
081: }
082:
083: public void setHealth(boolean health) {
084: this .HEALTH = health;
085: }
086:
087: public void setLoad(boolean load) {
088: this .LOAD = load;
089: }
090:
091: public void setMem(boolean mem) {
092: this .MEM = mem;
093: }
094:
095: public void setThread(boolean thread) {
096: this .THREAD = thread;
097: }
098:
099: /*
100: * (non-Javadoc)
101: *
102: * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
103: */
104: public void mouseClicked(MouseEvent e) {
105: }
106:
107: /*
108: * (non-Javadoc)
109: *
110: * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
111: */
112: public void mouseEntered(MouseEvent e) {
113: }
114:
115: /*
116: * (non-Javadoc)
117: *
118: * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
119: */
120: public void mouseExited(MouseEvent e) {
121: }
122:
123: /*
124: * (non-Javadoc)
125: *
126: * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
127: */
128: public void mousePressed(MouseEvent e) {
129: }
130:
131: /*
132: * (non-Javadoc)
133: *
134: * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
135: */
136: public void mouseReleased(MouseEvent e) {
137:
138: }
139:
140: /**
141: * The method will fist check to see if the graph is visible. If it is, it
142: * will repaint the graph.
143: */
144: public void updateGui(final MonitorModel model) {
145: if (this .isShowing()) {
146: this .CURRENT = model;
147: repaint();
148: }
149: }
150:
151: /**
152: * painComponent is responsible for drawing the actual graph. This is
153: * because of how screen works. Tried to use clipping, but I don't
154: * understand it well enough to get the desired effect.
155: */
156: public void paintComponent(Graphics g) {
157: super .paintComponent(g);
158: if (this .CURRENT != null) {
159: synchronized (MODEL) {
160: List samples = MODEL.getAllSamples(this .CURRENT
161: .getURL());
162: int size = samples.size();
163: synchronized (samples) {
164: Iterator e;
165: if (size > getWidth()) {
166: e = samples.listIterator(size - getWidth());
167: } else {
168: e = samples.iterator();
169: }
170: MonitorModel last = null;
171: for (int i = 0; e.hasNext(); i++) {
172: MonitorModel s = (MonitorModel) e.next();
173: if (last == null) {
174: last = s;
175: }
176: drawSample(i, s, g, last);
177: last = s;
178: }
179: }
180: }
181: }
182: }
183:
184: /**
185: * updateGui() will call repaint
186: */
187: public void updateGui() {
188: repaint();
189: }
190:
191: /**
192: * clear will repaint the graph
193: */
194: public void clearData() {
195: paintComponent(getGraphics());
196: this .repaint();
197: }
198:
199: private void drawSample(int x, MonitorModel model, Graphics g,
200: MonitorModel last) {
201: double width = getWidth();
202: double height = getHeight() - 10.0;
203: int xaxis = (int) (width * (x / width));
204: int lastx = (int) (width * ((x - 1) / width));
205:
206: // draw grid only when x is 1. If we didn't
207: // the grid line would draw over the data
208: // lines making it look bad.
209: if (YGRID && x == 1) {
210: int q1 = (int) (height * 0.25);
211: int q2 = (int) (height * 0.50);
212: int q3 = (int) (height * 0.75);
213: g.setColor(Color.lightGray);
214: g.drawLine(0, q1, getWidth(), q1);
215: g.drawLine(0, q2, getWidth(), q2);
216: g.drawLine(0, q3, getWidth(), q3);
217: }
218: if (XGRID && x == 1) {
219: int x1 = (int) (width * 0.25);
220: int x2 = (int) (width * 0.50);
221: int x3 = (int) (width * 0.75);
222: g.drawLine(x1, 0, x1, getHeight());
223: g.drawLine(x2, 0, x2, getHeight());
224: g.drawLine(x3, 0, x3, getHeight());
225: g.drawLine(getWidth(), 0, getWidth(), getHeight());
226: }
227:
228: if (HEALTH) {
229: int hly = (int) (height - (height * (model.getHealth() / 3.0)));
230: int lasty = (int) (height - (height * (last.getHealth() / 3.0)));
231:
232: g.setColor(Color.green);
233: g.drawLine(lastx, lasty, xaxis, hly);
234: }
235:
236: if (LOAD) {
237: int ldy = (int) (height - (height * (model.getLoad() / 100.0)));
238: int lastldy = (int) (height - (height * (last.getLoad() / 100.0)));
239:
240: g.setColor(Color.blue);
241: g.drawLine(lastx, lastldy, xaxis, ldy);
242: }
243:
244: if (MEM) {
245: int mmy = (int) (height - (height * (model.getMemload() / 100.0)));
246: int lastmmy = (int) (height - (height * (last.getMemload() / 100.0)));
247:
248: g.setColor(Color.orange);
249: g.drawLine(lastx, lastmmy, xaxis, mmy);
250: }
251:
252: if (THREAD) {
253: int thy = (int) (height - (height * (model.getThreadload() / 100.0)));
254: int lastthy = (int) (height - (height * (last
255: .getThreadload() / 100.0)));
256:
257: g.setColor(Color.red);
258: g.drawLine(lastx, lastthy, xaxis, thy);
259: }
260: }
261:
262: }
|