KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > reporters > FileReporter


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/reporters/FileReporter.java,v 1.15 2004/02/19 00:12:34 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.reporters;
20
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Color 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.io.BufferedReader JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.FileReader JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.text.DecimalFormat JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.Hashtable JavaDoc;
35 import java.util.Vector JavaDoc;
36
37 import javax.swing.JFrame JavaDoc;
38 import javax.swing.JLabel JavaDoc;
39 import javax.swing.JOptionPane JavaDoc;
40 import javax.swing.JPanel JavaDoc;
41
42 import org.apache.jorphan.logging.LoggingManager;
43 import org.apache.log.Logger;
44
45
46 /**
47  * This class loads data from a saved file and displays
48  * statistics about it.
49  *
50  *
51  * @author Tom Schneider
52  * @version $Revision: 1.15 $
53  */

54 public class FileReporter extends JPanel JavaDoc
55 {
56     transient private static Logger log
57         = LoggingManager.getLoggerForClass();
58     Hashtable JavaDoc data = new Hashtable JavaDoc();
59
60     /** initalize a file reporter from a file */
61     public void init(String JavaDoc file) throws IOException JavaDoc
62     {
63         File JavaDoc datafile = new File JavaDoc(file);
64         BufferedReader JavaDoc reader = null;
65
66         try
67         {
68             if (datafile.canRead())
69             {
70                 reader = new BufferedReader JavaDoc(new FileReader JavaDoc(datafile));
71             }
72             else
73             {
74                 JOptionPane.showMessageDialog(
75                         null, "The file you specified cannot be read.",
76                         "Information", JOptionPane.INFORMATION_MESSAGE);
77                 return;
78             }
79             String JavaDoc line;
80             
81             while ((line = reader.readLine()) != null)
82             {
83                 try
84                 {
85                     line = line.trim();
86                     if (line.startsWith("#") || line.length() == 0)
87                     {
88                         continue;
89                     }
90                     int splitter = line.lastIndexOf(' ');
91                     String JavaDoc key = line.substring(0, splitter);
92                     int len = line.length() - 1;
93                     Integer JavaDoc value = null;
94             
95                     if (line.charAt(len) == ',')
96                     {
97                         value = new Integer JavaDoc(
98                                 line.substring(splitter + 1, len));
99                     }
100                     else
101                     {
102                         value = new Integer JavaDoc(
103                                 line.substring(splitter + 1));
104                     }
105                     Vector JavaDoc v = getData(key);
106             
107                     if (v == null)
108                     {
109                         v = new Vector JavaDoc();
110                         this.data.put(key, v);
111                     }
112                     v.addElement(value);
113                 }
114                 catch (NumberFormatException JavaDoc nfe)
115                 {
116                     log.error("This line could not be parsed: " + line, nfe);
117                 }
118                 catch (Exception JavaDoc e)
119                 {
120                     log.error("This line caused a problem: " + line, e);
121                 }
122             }
123         }
124         finally
125         {
126             if (reader != null) reader.close();
127         }
128         showPanel();
129     }
130
131     public Vector JavaDoc getData(String JavaDoc key)
132     {
133         return (Vector JavaDoc) data.get(key);
134     }
135
136     /**
137      * Show main panel with length, graph, and stats.
138      */

139     public void showPanel()
140     {
141         JFrame JavaDoc f = new JFrame JavaDoc("Data File Report");
142
143         setLayout(new BorderLayout JavaDoc());
144         graphPanel gp = new graphPanel(data);
145
146         add(gp, "Center");
147         add(gp.getStats(), BorderLayout.EAST);
148         add(gp.getLegend(), BorderLayout.NORTH);
149         f.setSize(500, 300);
150         f.getContentPane().add(this);
151         f.show();
152     }
153 }
154
155
156 /**
157  * Graph panel generates all the panels for this reporter.
158  * Data is organized based on thread name in a hashtable.
159  * The data itself is a Vector of Integer objects
160  */

161 class graphPanel extends JPanel JavaDoc
162 {
163     //boolean autoScale = true;
164
Hashtable JavaDoc data;
165     Vector JavaDoc keys = new Vector JavaDoc();
166     Vector JavaDoc colorList = new Vector JavaDoc();
167     public graphPanel()
168     {}
169
170     public graphPanel(Hashtable JavaDoc data)
171     {
172         this.data = data;
173         Enumeration JavaDoc e = data.keys();
174
175         while (e.hasMoreElements())
176         {
177             String JavaDoc key = (String JavaDoc) e.nextElement();
178
179             keys.addElement(key);
180         }
181         for (int a = 0x33; a < 0xFF; a += 0x66)
182         {
183             for (int b = 0x33; b < 0xFF; b += 0x66)
184             {
185                 for (int c = 0x33; c < 0xFF; c += 0x66)
186                 {
187                     colorList.addElement(new Color JavaDoc(a, b, c));
188                 }
189             }
190         }
191     }
192
193     /**
194      * Get the maximum for all the data.
195      */

196     public float getMax()
197     {
198         float maxValue = 0;
199
200         for (int t = 0; t < keys.size(); t++)
201         {
202             String JavaDoc key = (String JavaDoc) keys.elementAt(t);
203             Vector JavaDoc temp = (Vector JavaDoc) data.get(key);
204
205             for (int j = 0; j < temp.size(); j++)
206             {
207                 float f = ((Integer JavaDoc) temp.elementAt(j)).intValue();
208
209                 maxValue = Math.max(f, maxValue);
210             }
211         }
212         return (float) (maxValue + maxValue * 0.1);
213     }
214
215     /**
216      * Get the minimum for all the data.
217      */

218     public float getMin()
219     {
220         float minValue = 9999999;
221
222         for (int t = 0; t < keys.size(); t++)
223         {
224             String JavaDoc key = (String JavaDoc) keys.elementAt(t);
225             Vector JavaDoc temp = (Vector JavaDoc) data.get(key);
226
227             for (int j = 0; j < temp.size(); j++)
228             {
229                 float f = ((Integer JavaDoc) temp.elementAt(j)).intValue();
230
231                 minValue = Math.min(f, minValue);
232             }
233         }
234         return (float) (minValue - minValue * 0.1);
235     }
236
237     /**
238      * Get the legend panel.
239      */

240     public JPanel JavaDoc getLegend()
241     {
242         JPanel JavaDoc main = new JPanel JavaDoc();
243         GridBagLayout JavaDoc g = new GridBagLayout JavaDoc();
244
245         main.setLayout(g);
246         GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
247
248         c.insets = new Insets JavaDoc(3, 3, 3, 3);
249         c.fill = GridBagConstraints.BOTH;
250         c.gridwidth = 1;
251         c.gridheight = 1;
252         for (int t = 0; t < keys.size(); t++)
253         {
254             String JavaDoc key = (String JavaDoc) keys.elementAt(t);
255             JLabel JavaDoc colorSwatch = new JLabel JavaDoc(" ");
256
257             colorSwatch.setBackground(
258                 (Color JavaDoc) colorList.elementAt(t % colorList.size()));
259             colorSwatch.setOpaque(true);
260             c.gridx = 1;
261             c.gridy = t;
262             g.setConstraints(colorSwatch, c);
263             main.add(colorSwatch);
264             JLabel JavaDoc name = new JLabel JavaDoc(key);
265
266             c.gridx = 2;
267             c.gridy = t;
268             g.setConstraints(name, c);
269             main.add(name);
270         }
271         return main;
272     }
273
274     /**
275      * Get the stats panel.
276      */

277     public JPanel JavaDoc getStats()
278     {
279         int total = 0;
280         float totalValue = 0;
281         float maxValue = 0;
282         float minValue = 999999;
283
284         for (int t = 0; t < keys.size(); t++)
285         {
286             String JavaDoc key = (String JavaDoc) keys.elementAt(t);
287             Vector JavaDoc temp = (Vector JavaDoc) data.get(key);
288
289             for (int j = 0; j < temp.size(); j++)
290             {
291                 float f = ((Integer JavaDoc) temp.elementAt(j)).intValue();
292
293                 minValue = Math.min(f, minValue);
294                 maxValue = Math.max(f, maxValue);
295                 totalValue += f;
296                 total++;
297             }
298         }
299         float averageValue = totalValue / total;
300         JPanel JavaDoc main = new JPanel JavaDoc();
301         GridBagLayout JavaDoc g = new GridBagLayout JavaDoc();
302
303         main.setLayout(g);
304         DecimalFormat JavaDoc df = new DecimalFormat JavaDoc("#0.0");
305         GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
306
307         c.insets = new Insets JavaDoc(3, 6, 3, 6);
308         c.fill = GridBagConstraints.BOTH;
309         c.gridwidth = 1;
310         c.gridheight = 1;
311         JLabel JavaDoc count = new JLabel JavaDoc("Count: " + total);
312
313         c.gridx = 1;
314         c.gridy = 1;
315         g.setConstraints(count, c);
316         JLabel JavaDoc min = new JLabel JavaDoc("Min: " + df.format(new Float JavaDoc(minValue)));
317
318         c.gridx = 1;
319         c.gridy = 2;
320         g.setConstraints(min, c);
321         JLabel JavaDoc max = new JLabel JavaDoc("Max: " + df.format(new Float JavaDoc(maxValue)));
322
323         c.gridx = 1;
324         c.gridy = 3;
325         g.setConstraints(max, c);
326         JLabel JavaDoc average =
327             new JLabel JavaDoc("Average: " + df.format(new Float JavaDoc(averageValue)));
328
329         c.gridx = 1;
330         c.gridy = 4;
331         g.setConstraints(average, c);
332         main.add(count);
333         main.add(min);
334         main.add(max);
335         main.add(average);
336         return main;
337     }
338
339     /**
340      * Gets the size of the biggest Vector.
341      */

342     public int getDataWidth()
343     {
344         int size = 0;
345
346         for (int t = 0; t < keys.size(); t++)
347         {
348             String JavaDoc key = (String JavaDoc) keys.elementAt(t);
349             Vector JavaDoc v = (Vector JavaDoc) data.get(key);
350
351             size = Math.max(size, v.size());
352         }
353         return size;
354     }
355
356     /**
357      * Draws the graph.
358      */

359     public void update(Graphics JavaDoc g)
360     {
361         // setup drawing area
362
int base = 10;
363
364         g.setColor(Color.white);
365         g.fillRect(0, 0, getSize().width, getSize().height);
366         int width = getSize().width;
367         int height = getSize().height;
368         float maxValue = getMax();
369         float minValue = getMin();
370
371         // draw grid
372
g.setColor(Color.gray);
373         int dataWidth = getDataWidth();
374         int increment = Math.round((width - 1) / (dataWidth - 1));
375
376         /* for (int t = 0; t < dataWidth; t++) {
377          g.drawLine(t * increment, 0, t * increment, height);
378          }*/

379         int yIncrement = Math.round(((float) height - (1 + base)) / (10 - 1));
380
381         /* for (int t = 0; t < 10; t++) {
382          g.drawLine(0, height - t * yIncrement, width, height - t * yIncrement);
383          }*/

384         // draw axis
385
for (int t = 1; t < dataWidth; t += (dataWidth / 25 + 1))
386         {
387             g.drawString(
388                 (new Integer JavaDoc(t)).toString(),
389                 t * increment + 2,
390                 height - 2);
391         }
392         float incrementValue = (maxValue - minValue) / (10 - 1);
393
394         for (int t = 0; t < 10; t++)
395         {
396             g.drawString(
397                 new Integer JavaDoc(Math.round(minValue + (t * incrementValue)))
398                     .toString(),
399                 2,
400                 height - t * yIncrement - 2 - base);
401         }
402         // draw data lines
403
int start = 0;
404
405         for (int t = 0; t < keys.size(); t++)
406         {
407             String JavaDoc key = (String JavaDoc) keys.elementAt(t);
408             Vector JavaDoc v = (Vector JavaDoc) data.get(key);
409
410             start = 0;
411             g.setColor((Color JavaDoc) colorList.elementAt(t % colorList.size()));
412             for (int i = 0; i < v.size() - 1; i++)
413             {
414                 float y1 = (float) ((Integer JavaDoc) v.elementAt(i)).intValue();
415                 float y2 = (float) ((Integer JavaDoc) v.elementAt(i + 1)).intValue();
416
417                 y1 = y1 - minValue;
418                 y2 = y2 - minValue;
419                 int Y1 = Math.round((height * y1) / (maxValue - minValue));
420                 int Y2 = Math.round((height * y2) / (maxValue - minValue));
421
422                 Y1 = height - Y1 - base;
423                 Y2 = height - Y2 - base;
424                 g.drawLine(start, Y1, start + increment, Y2);
425                 
426                 start += increment;
427             }
428         }
429     }
430
431     public void paint(Graphics JavaDoc g)
432     {
433         update(g);
434     }
435 }
436
Popular Tags