KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/visualizers/Graph.java,v 1.12.2.1 2004/10/03 15:21:54 sebb Exp $
2
/*
3  * Copyright 2001-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 */

18
19 package org.apache.jmeter.visualizers;
20
21 import java.awt.Color JavaDoc;
22 import java.awt.Dimension JavaDoc;
23 import java.awt.Graphics JavaDoc;
24 import java.awt.Rectangle JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import javax.swing.JComponent JavaDoc;
28 import javax.swing.Scrollable JavaDoc;
29 import javax.swing.SwingUtilities JavaDoc;
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 /**
37  * Implements a simple graph for displaying performance results.
38  *
39  * @author Michael Stover
40  * Created March 21, 2002
41  * @version $Revision: 1.12.2.1 $ Last updated: $Date: 2004/10/03 15:21:54 $
42  */

43 public class Graph
44     extends JComponent JavaDoc
45     implements Scrollable JavaDoc, 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     /**
58      * Constructor for the Graph object.
59      */

60     public Graph()
61     {
62        this.setPreferredSize(new Dimension JavaDoc(width, 100));
63     }
64
65     /**
66      * Constructor for the Graph object.
67      */

68     public Graph(GraphModel model)
69     {
70         this();
71         setModel(model);
72     }
73
74     /**
75      * Sets the Model attribute of the Graph object.
76      */

77     private void setModel(Object JavaDoc model)
78     {
79         this.model = (GraphModel) model;
80         this.model.addGraphListener(this);
81         repaint();
82     }
83
84     /**
85      * Gets the PreferredScrollableViewportSize attribute of the Graph object.
86      *
87      * @return the PreferredScrollableViewportSize value
88      */

89     public Dimension JavaDoc getPreferredScrollableViewportSize()
90     {
91         return this.getPreferredSize();
92         // return new Dimension(width, 400);
93
}
94
95     /**
96      * Gets the ScrollableUnitIncrement attribute of the Graph object.
97      *@return the ScrollableUnitIncrement value
98      */

99     public int getScrollableUnitIncrement(
100         Rectangle JavaDoc visibleRect,
101         int orientation,
102         int direction)
103     {
104         return 5;
105     }
106
107     /**
108      * Gets the ScrollableBlockIncrement attribute of the Graph object.
109      * @return the ScrollableBlockIncrement value
110      */

111     public int getScrollableBlockIncrement(
112         Rectangle JavaDoc visibleRect,
113         int orientation,
114         int direction)
115     {
116         return (int) (visibleRect.width * .9);
117     }
118
119     /**
120      * Gets the ScrollableTracksViewportWidth attribute of the Graph object.
121      *
122      * @return the ScrollableTracksViewportWidth value
123      */

124     public boolean getScrollableTracksViewportWidth()
125     {
126         return false;
127     }
128
129     /**
130      * Gets the ScrollableTracksViewportHeight attribute of the Graph object.
131      *
132      * @return the ScrollableTracksViewportHeight value
133      */

134     public boolean getScrollableTracksViewportHeight()
135     {
136         return true;
137     }
138
139     /**
140      * Clears this graph.
141      */

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 JavaDoc()
180         {
181             public void run()
182             {
183                 Graphics JavaDoc g = getGraphics();
184
185                 if (g != null)
186                 {
187                     drawSample(xPos, oneSample, g);
188                 }
189             }
190         });
191     }
192
193     public void paintComponent(Graphics JavaDoc g)
194     {
195         super.paintComponent(g);
196
197         synchronized (model.getSamples())
198         {
199             Iterator JavaDoc 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 JavaDoc g)
211     {
212         //int width = getWidth();
213
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