KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > visualizers > MonitorGraph


1 // $Header: /home/cvs/jakarta-jmeter/src/monitor/components/org/apache/jmeter/visualizers/MonitorGraph.java,v 1.6.2.1 2005/03/05 01:18:39 sebb Exp $
2
/*
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.jmeter.visualizers;
18
19 import java.awt.Color JavaDoc;
20 import java.awt.Graphics JavaDoc;
21 import java.awt.event.MouseEvent JavaDoc;
22 import java.awt.event.MouseListener JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.swing.JComponent JavaDoc;
27
28 import org.apache.jmeter.samplers.Clearable;
29
30 /**
31  * MonitorGraph will draw the performance history of a given
32  * server. It displays 4 lines:<p>
33  */

34 public class MonitorGraph
35     extends JComponent JavaDoc
36     implements MouseListener JavaDoc, 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     /**
53      *
54      * @deprecated Only for use in unit testing
55      */

56     public MonitorGraph()
57     {
58         //log.warn("Only for use in unit testing");
59
}
60
61     /**
62      *
63      */

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     /* (non-Javadoc)
96      * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
97      */

98     public void mouseClicked(MouseEvent JavaDoc e)
99     {
100     }
101
102     /* (non-Javadoc)
103      * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
104      */

105     public void mouseEntered(MouseEvent JavaDoc e)
106     {
107     }
108
109     /* (non-Javadoc)
110      * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
111      */

112     public void mouseExited(MouseEvent JavaDoc e)
113     {
114     }
115
116     /* (non-Javadoc)
117      * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
118      */

119     public void mousePressed(MouseEvent JavaDoc e)
120     {
121     }
122
123     /* (non-Javadoc)
124      * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
125      */

126     public void mouseReleased(MouseEvent JavaDoc e)
127     {
128
129     }
130
131     /**
132      * The method will fist check to see if the graph is
133      * visible. If it is, it will repaint the graph.
134      */

135     public void updateGui(final MonitorModel model)
136     {
137         if (this.isShowing()){
138             this.CURRENT = model;
139             repaint();
140         }
141     }
142
143     /**
144      * painComponent is responsible for drawing the actual
145      * graph. This is because of how screen works. Tried
146      * to use clipping, but I don't understand it well
147      * enough to get the desired effect.
148      */

149     public void paintComponent(Graphics JavaDoc g)
150     {
151         super.paintComponent(g);
152         if (this.CURRENT != null){
153             synchronized(MODEL){
154                 List JavaDoc samples = MODEL.getAllSamples(this.CURRENT.getURL());
155                 int size = samples.size();
156                 synchronized (samples)
157                 {
158                     Iterator JavaDoc 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     /**
180      * updateGui() will call repaint
181      */

182     public void updateGui()
183     {
184         repaint();
185     }
186
187     /**
188      * clear will repaint the graph
189      */

190     public void clear()
191     {
192         paintComponent(getGraphics());
193         this.repaint();
194     }
195     
196     private void drawSample(int x, MonitorModel model,
197         Graphics JavaDoc 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         // draw grid only when x is 1. If we didn't
205
// the grid line would draw over the data
206
// lines making it look bad.
207
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