KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/visualizers/SplineVisualizer.java,v 1.13 2004/03/05 01:33:33 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.BorderLayout JavaDoc;
22 import java.awt.Color JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Graphics JavaDoc;
25 import java.awt.GridLayout JavaDoc;
26 import java.awt.Image JavaDoc;
27
28 import javax.swing.BorderFactory JavaDoc;
29 import javax.swing.JComponent JavaDoc;
30 import javax.swing.JLabel JavaDoc;
31 import javax.swing.JPanel JavaDoc;
32 import javax.swing.border.Border JavaDoc;
33 import javax.swing.border.EmptyBorder JavaDoc;
34
35 import org.apache.jmeter.samplers.Clearable;
36 import org.apache.jmeter.samplers.SampleResult;
37 import org.apache.jmeter.util.JMeterUtils;
38 import org.apache.jmeter.visualizers.gui.AbstractVisualizer;
39 import org.apache.jorphan.gui.layout.VerticalLayout;
40
41 /**
42  * This class implements a statistical analyser that takes samples to process a
43  * Spline interpolated curve. Currently, it tries to look mostly like the
44  * GraphVisualizer.
45  *
46  * @version $Revision: 1.13 $ on $Date: 2004/03/05 01:33:33 $
47  */

48 public class SplineVisualizer
49     extends AbstractVisualizer
50     implements ImageVisualizer, GraphListener, Clearable
51 {
52
53     protected final Color JavaDoc BACKGROUND_COLOR = getBackground();
54     protected final Color JavaDoc MINIMUM_COLOR = new Color JavaDoc(0F, 0.5F, 0F);
55     protected final Color JavaDoc MAXIMUM_COLOR = new Color JavaDoc(0.9F, 0F, 0F);
56     protected final Color JavaDoc AVERAGE_COLOR = new Color JavaDoc(0F, 0F, 0.75F);
57     protected final Color JavaDoc INCOMING_COLOR = Color.black;
58     protected final int NUMBERS_TO_DISPLAY = 4;
59
60     protected final boolean FILL_UP_WITH_ZEROS = false;
61
62     transient private SplineGraph graph = null;
63
64     private JLabel JavaDoc minimumLabel = null;
65     private JLabel JavaDoc maximumLabel = null;
66     private JLabel JavaDoc averageLabel = null;
67     private JLabel JavaDoc incomingLabel = null;
68
69     private JLabel JavaDoc minimumNumberLabel = null;
70     private JLabel JavaDoc maximumNumberLabel = null;
71     private JLabel JavaDoc averageNumberLabel = null;
72     private JLabel JavaDoc incomingNumberLabel = null;
73     transient private SplineModel model;
74
75     public SplineVisualizer()
76     {
77         super();
78         model = new SplineModel();
79         graph = new SplineGraph();
80         this.model.setListener(this);
81         setGUI();
82     }
83
84     public void add(SampleResult res)
85     {
86         model.add(res);
87     }
88
89     public String JavaDoc getLabelResource()
90     {
91         return "spline_visualizer_title";
92     }
93
94     public void updateGui(Sample s)
95     {
96         updateGui();
97     }
98
99     public void clear()
100     {
101         model.clear();
102     }
103
104     public void setGUI()
105     {
106         Color JavaDoc backColor = BACKGROUND_COLOR;
107
108         this.setBackground(backColor);
109
110         this.setLayout(new BorderLayout JavaDoc());
111
112         // MAIN PANEL
113
JPanel JavaDoc mainPanel = new JPanel JavaDoc();
114         Border JavaDoc margin = new EmptyBorder JavaDoc(10, 10, 5, 10);
115
116         mainPanel.setBorder(margin);
117         mainPanel.setLayout(new VerticalLayout(5, VerticalLayout.LEFT));
118
119         // NAME
120
mainPanel.add(makeTitlePanel());
121
122         maximumLabel =
123             new JLabel JavaDoc(JMeterUtils.getResString("spline_visualizer_maximum"));
124         maximumLabel.setForeground(MAXIMUM_COLOR);
125         maximumLabel.setBackground(backColor);
126
127         averageLabel =
128             new JLabel JavaDoc(JMeterUtils.getResString("spline_visualizer_average"));
129         averageLabel.setForeground(AVERAGE_COLOR);
130         averageLabel.setBackground(backColor);
131
132         incomingLabel =
133             new JLabel JavaDoc(JMeterUtils.getResString("spline_visualizer_incoming"));
134         incomingLabel.setForeground(INCOMING_COLOR);
135         incomingLabel.setBackground(backColor);
136
137         minimumLabel =
138             new JLabel JavaDoc(JMeterUtils.getResString("spline_visualizer_minimum"));
139         minimumLabel.setForeground(MINIMUM_COLOR);
140         minimumLabel.setBackground(backColor);
141
142         maximumNumberLabel = new JLabel JavaDoc("0 ms");
143         maximumNumberLabel.setHorizontalAlignment(JLabel.RIGHT);
144         maximumNumberLabel.setForeground(MAXIMUM_COLOR);
145         maximumNumberLabel.setBackground(backColor);
146
147         averageNumberLabel = new JLabel JavaDoc("0 ms");
148         averageNumberLabel.setHorizontalAlignment(JLabel.RIGHT);
149         averageNumberLabel.setForeground(AVERAGE_COLOR);
150         averageNumberLabel.setBackground(backColor);
151
152         incomingNumberLabel = new JLabel JavaDoc("0 ms");
153         incomingNumberLabel.setHorizontalAlignment(JLabel.RIGHT);
154         incomingNumberLabel.setForeground(INCOMING_COLOR);
155         incomingNumberLabel.setBackground(backColor);
156
157         minimumNumberLabel = new JLabel JavaDoc("0 ms");
158         minimumNumberLabel.setHorizontalAlignment(JLabel.RIGHT);
159         minimumNumberLabel.setForeground(MINIMUM_COLOR);
160         minimumNumberLabel.setBackground(backColor);
161
162         // description Panel
163
JPanel JavaDoc labelPanel = new JPanel JavaDoc();
164
165         labelPanel.setLayout(new GridLayout JavaDoc(0, 1));
166         labelPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20));
167         labelPanel.setBackground(backColor);
168         labelPanel.add(maximumLabel);
169         labelPanel.add(averageLabel);
170         if (model.SHOW_INCOMING_SAMPLES)
171         {
172             labelPanel.add(incomingLabel);
173         }
174         labelPanel.add(minimumLabel);
175         // number Panel
176
JPanel JavaDoc numberPanel = new JPanel JavaDoc();
177
178         numberPanel.setLayout(new GridLayout JavaDoc(0, 1));
179         numberPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20));
180         numberPanel.setBackground(backColor);
181         numberPanel.add(maximumNumberLabel);
182         numberPanel.add(averageNumberLabel);
183         if (model.SHOW_INCOMING_SAMPLES)
184         {
185             numberPanel.add(incomingNumberLabel);
186         }
187         numberPanel.add(minimumNumberLabel);
188         // information display Panel
189
JPanel JavaDoc infoPanel = new JPanel JavaDoc();
190
191         infoPanel.setLayout(new BorderLayout JavaDoc());
192         infoPanel.add(labelPanel, BorderLayout.CENTER);
193         infoPanel.add(numberPanel, BorderLayout.EAST);
194
195         this.add(mainPanel, BorderLayout.NORTH);
196         this.add(infoPanel, BorderLayout.WEST);
197         this.add(graph, BorderLayout.CENTER);
198         // everyone is free to swing on its side :)
199
// add(infoPanel, BorderLayout.EAST);
200
}
201
202     public void updateGui()
203     {
204         repaint();
205         synchronized (this)
206         {
207             setMinimum(model.getMinimum());
208             setMaximum(model.getMaximum());
209             setAverage(model.getAverage());
210             setIncoming(model.getCurrent());
211         }
212     }
213
214     public String JavaDoc toString()
215     {
216         return "Show the samples analysis as a Spline curve";
217     }
218
219     public String JavaDoc formatMeasureToDisplay(long measure)
220     {
221         String JavaDoc numberString = String.valueOf(measure);
222
223         if (FILL_UP_WITH_ZEROS)
224         {
225             for (int i = numberString.length(); i < NUMBERS_TO_DISPLAY; i++)
226             {
227                 numberString = "0" + numberString;
228             }
229         }
230         return numberString;
231     }
232
233     public void setMinimum(long n)
234     {
235         String JavaDoc text = this.formatMeasureToDisplay(n) + " ms";
236
237         this.minimumNumberLabel.setText(text);
238     }
239
240     public void setMaximum(long n)
241     {
242         String JavaDoc text = this.formatMeasureToDisplay(n) + " ms";
243
244         this.maximumNumberLabel.setText(text);
245     }
246
247     public void setAverage(long n)
248     {
249         String JavaDoc text = this.formatMeasureToDisplay(n) + " ms";
250
251         this.averageNumberLabel.setText(text);
252     }
253
254     public void setIncoming(long n)
255     {
256         String JavaDoc text = this.formatMeasureToDisplay(n) + " ms";
257
258         this.incomingNumberLabel.setText(text);
259     }
260
261     public JPanel JavaDoc getControlPanel()
262     {
263         return this;
264     }
265
266     public Image JavaDoc getImage()
267     {
268         Image JavaDoc result = graph.createImage(graph.getWidth(), graph.getHeight());
269
270         graph.paintComponent(result.getGraphics());
271
272         return result;
273     }
274
275     /**
276      * Component showing a Spline curve.
277      *
278      * @version $Revision: 1.13 $
279      */

280     public class SplineGraph extends JComponent JavaDoc
281     {
282         public boolean reinterpolated = false;
283         protected final Color JavaDoc WAITING_COLOR = Color.darkGray;
284         protected int lastWidth = -1;
285         protected int lastHeight = -1;
286         protected int[] plot = null;
287
288         public SplineGraph()
289         {
290         }
291
292         /**
293          * Clear the Spline graph and get ready for the next wave.
294          */

295         public void clear()
296         {
297             lastWidth = -1;
298             lastHeight = -1;
299             plot = null;
300             this.repaint();
301         }
302
303         public void paintComponent(Graphics JavaDoc g)
304         {
305             super.paintComponent(g);
306
307             Dimension JavaDoc dimension = this.getSize();
308             int width = dimension.width;
309             int height = dimension.height;
310
311             if (model.getDataCurve() == null)
312             {
313                 g.setColor(this.getBackground());
314                 g.fillRect(0, 0, width, height);
315                 g.setColor(WAITING_COLOR);
316                 g.drawString(
317                     JMeterUtils.getResString(
318                         "spline_visualizer_waitingmessage"),
319                     (width - 120) / 2,
320                     height - (height - 12) / 2);
321                 return;
322             }
323
324             //boolean resized = true;
325

326             if (width == lastWidth && height == lastHeight)
327             {
328                 // dimension of the SplineGraph is the same
329
//resized = false;
330
}
331             else
332             {
333                 // dimension changed
334
//resized = true;
335
lastWidth = width;
336                 lastHeight = height;
337             }
338
339             this.plot = model.getDataCurve().getPlots(width, height); // rounds!
340

341             int n = plot.length;
342             int curY = plot[0];
343
344             for (int i = 1; i < n; i++)
345             {
346                 g.setColor(Color.black);
347                 g.drawLine(i - 1, height - curY - 1, i, height - plot[i] - 1);
348                 curY = plot[i];
349             }
350         }
351     }
352 }
353
Popular Tags