KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/visualizers/StatVisualizer.java,v 1.17 2004/03/05 01:33:33 sebb Exp $
2
/*
3  * Copyright 2002-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.BorderLayout JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.event.MouseAdapter JavaDoc;
25 import java.awt.event.MouseEvent JavaDoc;
26 import java.util.Arrays JavaDoc;
27
28 import javax.swing.BoxLayout JavaDoc;
29 import javax.swing.JPanel JavaDoc;
30 import javax.swing.JScrollPane JavaDoc;
31 import javax.swing.JTable JavaDoc;
32 import javax.swing.border.Border JavaDoc;
33 import javax.swing.border.EmptyBorder JavaDoc;
34 import javax.swing.event.TableModelEvent JavaDoc;
35 import javax.swing.table.AbstractTableModel JavaDoc;
36 import javax.swing.table.TableModel JavaDoc;
37
38 import org.apache.jmeter.samplers.Clearable;
39 import org.apache.jmeter.samplers.SampleResult;
40 import org.apache.jmeter.testelement.TestElement;
41 import org.apache.jmeter.util.JMeterUtils;
42 import org.apache.jmeter.visualizers.gui.AbstractVisualizer;
43
44
45 /**
46  * Aggregrate Table-Based Reporting Visualizer for JMeter. Props to the people
47  * who've done the other visualizers ahead of me (Stefano Mazzocchi), who I
48  * borrowed code from to start me off (and much code may still exist). Thank
49  * you!
50  *
51  * @version $Revision: 1.17 $ on $Date: 2004/03/05 01:33:33 $
52  */

53 public class StatVisualizer extends AbstractVisualizer
54         implements AccumListener, Clearable
55 {
56     protected JTable JavaDoc myJTable;
57
58     protected JScrollPane JavaDoc myScrollPane;
59     transient private StatVisualizerModel model;
60     transient private StatTableModel myStatTableModel;
61
62     public StatVisualizer()
63     {
64         super();
65         model = new StatVisualizerModel();
66         model.addAccumListener(this);
67         init();
68     }
69
70     public String JavaDoc getLabelResource()
71     {
72         return "aggregate_report";
73     }
74
75     public void add(SampleResult res)
76     {
77         model.addNewSample(res);
78     }
79
80     /**
81      * Clears this visualizer and its model, and forces a repaint of the table.
82      */

83     public void clear()
84     {
85         myStatTableModel.clear();
86         model.clear();
87     }
88
89     public synchronized void updateGui(RunningSample s)
90     {
91         myStatTableModel.rowChanged(s.getIndex());
92     }
93
94     // overrides AbstractVisualizer
95
// forces GUI update after sample file has been read
96
public TestElement createTestElement()
97     {
98         TestElement t = super.createTestElement();
99
100         //sleepTill = 0;
101
return t;
102     }
103
104     /**
105      * Main visualizer setup.
106      */

107     private void init()
108     {
109         this.setLayout(new BorderLayout JavaDoc());
110
111         // MAIN PANEL
112
JPanel JavaDoc mainPanel = new JPanel JavaDoc();
113         Border JavaDoc margin = new EmptyBorder JavaDoc(10, 10, 5, 10);
114
115         mainPanel.setBorder(margin);
116         mainPanel.setLayout(new BoxLayout JavaDoc(mainPanel, BoxLayout.Y_AXIS));
117
118         mainPanel.add(makeTitlePanel());
119         myStatTableModel = new StatTableModel(model);
120         // SortFilterModel mySortedModel =
121
// new SortFilterModel(myStatTableModel);
122
myJTable = new JTable JavaDoc(myStatTableModel);
123         myJTable.setPreferredScrollableViewportSize(new Dimension JavaDoc(500, 70));
124         myScrollPane = new JScrollPane JavaDoc(myJTable);
125         this.add(mainPanel, BorderLayout.NORTH);
126         this.add(myScrollPane, BorderLayout.CENTER);
127     }
128
129     /**
130      * Class which implements the model for our main table in this
131      * visualizer.
132      *
133      * @version $Revision: 1.17 $
134      */

135     class StatTableModel extends AbstractTableModel JavaDoc
136     {
137         private final String JavaDoc[] columnNames =
138             { "URL", "Count", "Average", "Min", "Max", "Error%", "Rate" };
139         private final Class JavaDoc[] columnClasses =
140             {
141                 String JavaDoc.class,
142                 Long JavaDoc.class,
143                 Long JavaDoc.class,
144                 Long JavaDoc.class,
145                 Long JavaDoc.class,
146                 String JavaDoc.class,
147                 String JavaDoc.class };
148         private final String JavaDoc TOTAL_LABEL =
149             JMeterUtils.getResString("aggregate_report_total_label");
150
151         private transient StatVisualizerModel model;
152         private int currentRowCount = 0;
153
154         public StatTableModel(StatVisualizerModel model)
155         {
156             super();
157             this.model = model;
158         }
159
160         public void rowChanged(int index)
161         {
162             TableModelEvent JavaDoc event;
163
164             // Create the table changed event, carefully handling the case
165
// where the table grows beyond its current known size.
166
synchronized (this)
167             {
168                 if (index >= currentRowCount - 1)
169                 {
170                     event =
171                         new TableModelEvent JavaDoc(
172                             this,
173                             currentRowCount - 1,
174                             index,
175                             TableModelEvent.ALL_COLUMNS,
176                             TableModelEvent.INSERT);
177                     currentRowCount = index + 2;
178                 }
179                 else
180                 {
181                     event = new TableModelEvent JavaDoc(this, index);
182                 }
183             }
184             // Fire the event:
185
fireTableChanged(event);
186             // No matter which row changes, the totals row will have changed
187
// too:
188
fireTableChanged(new TableModelEvent JavaDoc(this, currentRowCount));
189         }
190
191         public int getColumnCount()
192         {
193             return columnNames.length;
194         }
195
196         public int getRowCount()
197         {
198             currentRowCount = model.getRunningSampleCount() + 1;
199             return currentRowCount;
200         }
201
202         public String JavaDoc getColumnName(int col)
203         {
204             return columnNames[col];
205         }
206
207         public Object JavaDoc getValueAt(int row, int col)
208         {
209             RunningSample s;
210
211             if (row == model.getRunningSampleCount())
212             {
213                 if (col == 0)
214                 {
215                     return TOTAL_LABEL;
216                 }
217                 s = model.getRunningSampleTotal();
218             }
219             else
220             {
221                 s = model.getRunningSample(row);
222             }
223
224             switch (col)
225             {
226             case 0:
227                 return s.getLabel();
228
229             case 1:
230                 return new Long JavaDoc(s.getNumSamples());
231
232             case 2:
233                 return new Long JavaDoc(s.getAverage());
234
235             case 3:
236                 return new Long JavaDoc(s.getMin());
237
238             case 4:
239                 return new Long JavaDoc(s.getMax());
240
241             case 5:
242                 return s.getErrorPercentageString();
243
244             case 6:
245                 return s.getRateString();
246
247             default:
248                 return "__ERROR__";
249             }
250         }
251
252         public Class JavaDoc getColumnClass(int c)
253         {
254             return columnClasses[c];
255         }
256
257         public void clear()
258         {
259             fireTableDataChanged();
260         }
261     }
262 }
263
264
265 /**
266  * Pulled this mainly out of a Core Java book to implement a sorted table -
267  * haven't implemented this yet, it needs some non-trivial work done to it to
268  * support our dynamically-sizing TableModel for this visualizer.
269  *
270  * @version $Revision: 1.17 $
271  */

272 class SortFilterModel extends AbstractTableModel JavaDoc
273 {
274     private TableModel JavaDoc model;
275     private int sortColumn;
276     private Row[] rows;
277
278     public SortFilterModel(TableModel JavaDoc m)
279     {
280         model = m;
281         rows = new Row[model.getRowCount()];
282         for (int i = 0; i < rows.length; i++)
283         {
284             rows[i] = new Row();
285             rows[i].index = i;
286         }
287     }
288
289     public SortFilterModel()
290     {
291     }
292
293     public void setValueAt(Object JavaDoc aValue, int r, int c)
294     {
295         model.setValueAt(aValue, rows[r].index, c);
296     }
297
298     public Object JavaDoc getValueAt(int r, int c)
299     {
300         return model.getValueAt(rows[r].index, c);
301     }
302
303     public boolean isCellEditable(int r, int c)
304     {
305         return model.isCellEditable(rows[r].index, c);
306     }
307
308     public int getRowCount()
309     {
310         return model.getRowCount();
311     }
312
313     public int getColumnCount()
314     {
315         return model.getColumnCount();
316     }
317
318     public String JavaDoc getColumnName(int c)
319     {
320         return model.getColumnName(c);
321     }
322
323     public Class JavaDoc getColumnClass(int c)
324     {
325         return model.getColumnClass(c);
326     }
327
328     public void sort(int c)
329     {
330         sortColumn = c;
331         Arrays.sort(rows);
332         fireTableDataChanged();
333     }
334
335     public void addMouseListener(final JTable JavaDoc table)
336     {
337         table.getTableHeader().addMouseListener(new MouseAdapter JavaDoc()
338         {
339             public void mouseClicked(MouseEvent JavaDoc event)
340             {
341                 if (event.getClickCount() < 2)
342                 {
343                     return;
344                 }
345                 int tableColumn = table.columnAtPoint(event.getPoint());
346                 int modelColumn = table.convertColumnIndexToModel(tableColumn);
347
348                 sort(modelColumn);
349             }
350         });
351     }
352
353     private class Row implements Comparable JavaDoc
354     {
355         public int index;
356
357         public int compareTo(Object JavaDoc other)
358         {
359             Row otherRow = (Row) other;
360             Object JavaDoc a = model.getValueAt(index, sortColumn);
361             Object JavaDoc b = model.getValueAt(otherRow.index, sortColumn);
362
363             if (a instanceof Comparable JavaDoc)
364             {
365                 return ((Comparable JavaDoc) a).compareTo(b);
366             }
367             else
368             {
369                 return index - otherRow.index;
370             }
371         }
372     }
373 } // class SortFilterModel
374
Popular Tags