KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/visualizers/GraphAccum.java,v 1.10.2.1 2004/06/12 18:32:05 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
22 import java.awt.Color JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Graphics JavaDoc;
25 import java.awt.GridBagConstraints JavaDoc;
26 import java.awt.GridBagLayout JavaDoc;
27 import java.awt.Insets JavaDoc;
28 import java.awt.Point JavaDoc;
29 import java.awt.Rectangle JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import javax.swing.JComponent JavaDoc;
33 import javax.swing.JLabel JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35 import javax.swing.Scrollable JavaDoc;
36 import javax.swing.SwingUtilities JavaDoc;
37
38 import org.apache.jmeter.samplers.SampleResult;
39 import org.apache.jmeter.util.ColorHelper;
40 import org.apache.jorphan.logging.LoggingManager;
41 import org.apache.log.Logger;
42
43
44 /**
45  * Draws the graph.
46  *
47  * @author Khor Soon Hin
48  * Created 2001/08/11
49  * @version $Revision: 1.10.2.1 $ Last updated: $Date: 2004/06/12 18:32:05 $
50  */

51 public class GraphAccum extends JComponent JavaDoc implements Scrollable JavaDoc,
52         GraphAccumListener
53 {
54     protected GraphAccumModel model;
55     protected GraphAccumVisualizer visualizer;
56
57     /** Ensure that the legends are only drawn once. */
58     protected boolean noLegendYet = true;
59
60     /**
61      * Keep track of previous point. Needed to draw a line joining the
62      * previous point with the current one.
63      */

64     protected Point JavaDoc[] previousPts;
65
66     /**
67      * Ensure that previousPts is allocated once only. It'll be reused at each
68      * drawSample. It can't be allocated outside drawSample 'cos the sample
69      * is only passed in here.
70      */

71     protected boolean previousPtsAlloc = false;
72
73     protected final static int width = 2000;
74
75     protected final static int PLOT_X_WIDTH = 10;
76     transient private static Logger log = LoggingManager.getLoggerForClass();
77
78
79     /**
80      * Constructor.
81      */

82     public GraphAccum()
83     {
84         log.debug("Start : GraphAnnum1");
85         log.debug("End : GraphAnnum1");
86     }
87
88     /**
89      * Constructor with model set.
90      *
91      * @param model model which this object represents
92      */

93     public GraphAccum(GraphAccumModel model)
94     {
95         this();
96         log.debug("Start : GraphAnnum2");
97         setModel(model);
98         log.debug("End : GraphAnnum2");
99     }
100
101     /**
102      * Set model which this object represents.
103      *
104      * @param model model which this object represents
105      */

106     private void setModel(Object JavaDoc model)
107     {
108         log.debug("Start : setModel1");
109         this.model = (GraphAccumModel) model;
110         this.model.addGraphAccumListener(this);
111         repaint();
112         log.debug("End : setModel1");
113     }
114
115     /**
116      * Set the visualizer.
117      *
118      * @param visualizer visualizer of this object
119      */

120     public void setVisualizer(Object JavaDoc visualizer)
121     {
122         if (log.isDebugEnabled())
123         {
124             log.debug("setVisualizer1 : Setting visualizer - " + visualizer);
125         }
126         this.visualizer = (GraphAccumVisualizer) visualizer;
127     }
128
129     /**
130      * The legend is only printed once during sampling. This sets the variable
131      * that indicates whether the legend has been printed yet or not.
132      *
133      * @param value variable that indicates whether the legend has been
134      * printed yet
135      */

136     public void setNoLegendYet(boolean value)
137     {
138         noLegendYet = value;
139     }
140
141     /**
142      * Gets the PreferredScrollableViewportSize attribute of the Graph object.
143      *
144      * @return the PreferredScrollableViewportSize value
145      */

146     public Dimension JavaDoc getPreferredScrollableViewportSize()
147     {
148         return this.getPreferredSize();
149     }
150
151     /**
152      * Gets the ScrollableUnitIncrement attribute of the Graph object.
153      * @return the ScrollableUnitIncrement value
154      */

155     public int getScrollableUnitIncrement(
156         Rectangle JavaDoc visibleRect,
157         int orientation,
158         int direction)
159     {
160         return 5;
161     }
162
163     /**
164      * Gets the ScrollableBlockIncrement attribute of the Graph object.
165      * @return the ScrollableBlockIncrement value
166      */

167     public int getScrollableBlockIncrement(
168         Rectangle JavaDoc visibleRect,
169         int orientation,
170         int direction)
171     {
172         return (int) (visibleRect.width * .9);
173     }
174
175     /**
176      * Gets the ScrollableTracksViewportWidth attribute of the Graph object.
177      *
178      * @return the ScrollableTracksViewportWidth value
179      */

180     public boolean getScrollableTracksViewportWidth()
181     {
182         return false;
183     }
184
185     /**
186      * Gets the ScrollableTracksViewportHeight attribute of the Graph object.
187      *
188      * @return the ScrollableTracksViewportHeight value
189      */

190     public boolean getScrollableTracksViewportHeight()
191     {
192         return true;
193     }
194
195     /**
196      * The legend is only printed once during sampling. This returns the
197      * variable that indicates whether the legend has been printed yet or not.
198      *
199      * @return value variable that indicates whether the legend has been
200      * printed yet
201      */

202     public boolean getNoLegendYet()
203     {
204         return noLegendYet;
205     }
206
207     /**
208      * Redraws the gui.
209      */

210     public void updateGui()
211     {
212         log.debug("Start : updateGui1");
213         repaint();
214         log.debug("End : updateGui1");
215     }
216
217     /**
218      * Redraws the gui if no rescaling of the graph is needed.
219      *
220      * @param oneSample sample to be added
221      */

222     public void updateGui(final SampleResult oneSample)
223     {
224         log.debug("Start : updateGui2");
225         final int xPos = model.getSampleCount();
226
227         SwingUtilities.invokeLater(
228                 new Runnable JavaDoc()
229                 {
230                     public void run()
231                     {
232                         Graphics JavaDoc g = getGraphics();
233
234                         if (g != null)
235                         {
236                             drawSample(xPos * PLOT_X_WIDTH, oneSample, g);
237                         }
238                     }
239                 }
240                 );
241         log.debug("End : updateGui2");
242     }
243
244     public void paintComponent(Graphics JavaDoc g)
245     {
246         super.paintComponent(g);
247         log.debug("Start : paintComponent1");
248         
249         synchronized (model.getList())
250         {
251             // For repainting set this to false because all the points needs to
252
// be redrawn so no need(shouldn't) use the previousPts.
253
previousPtsAlloc = false;
254             Iterator JavaDoc e = model.getList().iterator();
255
256             for (int i = 0; e.hasNext(); i++)
257             {
258                 SampleResult s = (SampleResult) e.next();
259
260                 drawSample(i * PLOT_X_WIDTH, s, g);
261             }
262         }
263         log.debug("End : paintComponent1");
264     }
265
266     /**
267      * Clears this graph.
268      */

269     public void clear()
270     {
271         setNoLegendYet(true);
272         ((JPanel JavaDoc) visualizer.getWhiteCanvas()).removeAll();
273         previousPts = null;
274     }
275
276     private void drawSample(int x, SampleResult oneSample, Graphics JavaDoc g)
277     {
278         log.debug("Start : drawSample1");
279
280         // Used to keep track of accumulated load times of components.
281
int lastLevel = 0;
282
283         // Number of components
284
int compCount = 0;
285
286         SampleResult[] resultList = oneSample.getSubResults();
287         int resultListCount = 0;
288
289         // Allocate previousPts only the first time
290
if (!previousPtsAlloc)
291         {
292             resultListCount += resultList.length;
293             previousPts = new Point JavaDoc[resultListCount + 2];
294         }
295
296         Color JavaDoc currColor = Color.black;
297         JPanel JavaDoc lPanel = (JPanel JavaDoc) visualizer.getWhiteCanvas();
298         JPanel JavaDoc legendPanel = new JPanel JavaDoc();
299         GridBagLayout JavaDoc gridBag = new GridBagLayout JavaDoc();
300         GridBagConstraints JavaDoc gbc = new GridBagConstraints JavaDoc();
301
302         legendPanel.setLayout(gridBag);
303         lPanel.add(legendPanel);
304         Dimension JavaDoc d = this.getSize();
305
306         // Set the total time to load the sample
307
long totalTime = oneSample.getTime();
308
309         // If the page has other components then set the total time to be that
310
// including all its components' load time.
311
if (log.isDebugEnabled())
312         {
313             log.debug("drawSample1 : total time - " + totalTime);
314         }
315         int data = (int) (totalTime * d.height / model.getMax());
316
317         g.setColor(currColor);
318         if (!previousPtsAlloc)
319         {
320             // If first dot, just draw the point.
321
g.drawLine(
322                 x % width,
323                 d.height - data,
324                 x % width,
325                 d.height - data - 1);
326         }
327         else
328         {
329             // Otherwise, draw from previous point.
330
g.drawLine((previousPts[0].x) % width, previousPts[0].y, x % width,
331                     d.height - data);
332         }
333         
334         // Store current total time point
335
previousPts[0] = new Point JavaDoc(x % width, d.height - data);
336         if (noLegendYet)
337         {
338             gbc.gridx = 0;
339             gbc.gridy = compCount++;
340             gbc.anchor = GridBagConstraints.WEST;
341             gbc.weightx = 1.0;
342             gbc.insets = new Insets JavaDoc(0, 10, 0, 0);
343             JLabel JavaDoc totalTimeLabel =
344                 new JLabel JavaDoc("Total time - " + oneSample.toString());
345
346             totalTimeLabel.setForeground(currColor);
347             gridBag.setConstraints(totalTimeLabel, gbc);
348             legendPanel.add(totalTimeLabel);
349         }
350
351         // Plot the time of the page itself without all its components
352
if (log.isDebugEnabled())
353         {
354             log.debug(
355                 "drawSample1 : main page load time - " + oneSample.getTime());
356         }
357         data = (int) (oneSample.getTime() * d.height / model.getMax());
358         currColor = ColorHelper.changeColorCyclicIncrement(currColor, 40);
359         g.setColor(currColor);
360         if (!previousPtsAlloc)
361         {
362             // If first dot, just draw the point
363
g.drawLine(
364                 x % width,
365                 d.height - data,
366                 x % width,
367                 d.height - data - 1);
368         }
369         else
370         {
371             // Otherwise, draw from previous point
372
g.drawLine((previousPts[1].x) % width, previousPts[1].y, x % width,
373                     d.height - data);
374         }
375         // Store load time without components
376
previousPts[1] = new Point JavaDoc(x % width, d.height - data);
377         if (noLegendYet)
378         {
379             gbc.gridx = 0;
380             gbc.gridy = compCount++;
381             gbc.anchor = GridBagConstraints.WEST;
382             gbc.weightx = 1.0;
383             gbc.insets = new Insets JavaDoc(0, 10, 0, 0);
384             JLabel JavaDoc mainTimeLabel = new JLabel JavaDoc(oneSample.toString());
385
386             mainTimeLabel.setForeground(currColor);
387             gridBag.setConstraints(mainTimeLabel, gbc);
388             legendPanel.add(mainTimeLabel);
389         }
390         lastLevel += data;
391         // Plot the times of the total times components
392
int currPreviousPts = 2;
393
394         if (resultList != null)
395         {
396             for (int i = 0; i < resultList.length; i++)
397             {
398                 SampleResult componentRes = (SampleResult) resultList[i];
399
400                 if (log.isDebugEnabled())
401                 {
402                     log.debug(
403                         "drawSample1 : componentRes - "
404                             + componentRes.getSampleLabel()
405                             + " loading time - "
406                             + componentRes.getTime());
407                 }
408                 data =
409                     (int) (componentRes.getTime() * d.height / model.getMax());
410                 data += lastLevel;
411                 currColor =
412                     ColorHelper.changeColorCyclicIncrement(currColor, 100);
413                 g.setColor(currColor);
414                 if (!previousPtsAlloc)
415                 {
416                     // If first dot, just draw the point
417
g.drawLine(
418                         x % width,
419                         d.height - data,
420                         x % width,
421                         d.height - data - 1);
422                 }
423                 else
424                 {
425                     // Otherwise, draw from previous point
426
g.drawLine(
427                         (previousPts[currPreviousPts].x) % width,
428                         previousPts[currPreviousPts].y,
429                         x % width,
430                         d.height - data);
431                 }
432                 // Store the current plot
433
previousPts[currPreviousPts++] =
434                     new Point JavaDoc(x % width, d.height - data);
435                 if (noLegendYet)
436                 {
437                     gbc.gridx = 0;
438                     gbc.gridy = compCount++;
439                     gbc.anchor = GridBagConstraints.WEST;
440                     gbc.weightx = 1.0;
441                     gbc.insets = new Insets JavaDoc(0, 10, 0, 0);
442                     JLabel JavaDoc compTimeLabel =
443                         new JLabel JavaDoc(componentRes.getSampleLabel());
444
445                     compTimeLabel.setForeground(currColor);
446                     gridBag.setConstraints(compTimeLabel, gbc);
447                     legendPanel.add(compTimeLabel);
448                 }
449                 lastLevel = data;
450             }
451         }
452
453         if (noLegendYet)
454         {
455             noLegendYet = false;
456             lPanel.repaint();
457             lPanel.revalidate();
458         }
459         
460         // Set the previousPtsAlloc to true here and not after allocation
461
// because the rest of the codes also depend on previousPtsAlloc to be
462
// false if first time plotting the graph i.e. there are no previous
463
// points.
464
if (!previousPtsAlloc)
465         {
466             previousPtsAlloc = true;
467         }
468         log.debug("End : drawSample1");
469     }
470 }
471
Popular Tags