KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/visualizers/Attic/StatVisualizerModel.java,v 1.15 2004/02/13 01:48:46 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; // java
20

21
22 import java.util.Collections JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Vector JavaDoc;
29
30 import org.apache.jmeter.samplers.Clearable;
31 import org.apache.jmeter.samplers.SampleResult;
32
33
34 /**
35  * Aggregrate Table-Based Reporting Model for JMeter. Props to the people
36  * who've done the other visualizers ahead of me (Stefano Mazzocchi), who I
37  * borrowed code from to start me off (and much code may still exist). Thank
38  * you!
39  *
40  * @author James Boutcher
41  * Created March 21, 2002
42  * @version $Revision: 1.15 $ Last updated: $Date: 2004/02/13 01:48:46 $
43  */

44 public class StatVisualizerModel implements Clearable
45 {
46     private String JavaDoc name;
47     private List JavaDoc listeners;
48     private Vector JavaDoc runningSamples;
49     private Map JavaDoc labelMap;
50     private RunningSample total;
51
52     /**
53      * Default Constuctor.
54      */

55     public StatVisualizerModel()
56     {
57         listeners = new LinkedList JavaDoc();
58         runningSamples = new Vector JavaDoc(0, 10);
59         labelMap = Collections.synchronizedMap(new HashMap JavaDoc(10));
60         total = new RunningSample("__TOTAL__", -1);
61     }
62
63     /**
64      * Sets the Name attribute of the StatVisualizerModel object.
65      *
66      * @param name the new Name value
67      */

68     public void setName(String JavaDoc name)
69     {
70         this.name = name;
71     }
72
73     /**
74      * Gets the GuiClass attribute of the StatVisualizerModel object.
75      *
76      * @return the GuiClass value
77      */

78     public Class JavaDoc getGuiClass()
79     {
80         return StatVisualizer.class;
81     }
82
83     /**
84      * Gets the Name attribute of the StatVisualizerModel object.
85      *
86      * @return the Name value
87      */

88     public String JavaDoc getName()
89     {
90         return name;
91     }
92
93     /**
94      * Registers a listener (a visualizer, graph, etc) to this model. This will
95      * allow the model to fire GUI updates to anyone when data changes, etc.
96      */

97     public void addGraphListener(GraphListener listener)
98     {
99         listeners.add(listener);
100     }
101
102     public void addAccumListener(AccumListener listener)
103     {
104         listeners.add(listener);
105     }
106
107     public int getRunningSampleCount()
108     {
109         return runningSamples.size();
110     }
111
112     public RunningSample getRunningSample(int index)
113     {
114         return (RunningSample) runningSamples.get(index);
115     }
116
117     public RunningSample getRunningSample(String JavaDoc label)
118     {
119         return (RunningSample) labelMap.get(label);
120     }
121
122     public RunningSample getRunningSampleTotal()
123     {
124         return total;
125     }
126
127     public void addNewSample(SampleResult res)
128     {
129         String JavaDoc aLabel = res.getSampleLabel();
130         RunningSample s;
131
132         synchronized (labelMap)
133         {
134             s = (RunningSample) labelMap.get(aLabel);
135             if (s == null)
136             {
137                 s = new RunningSample(aLabel, runningSamples.size());
138                 runningSamples.add(s);
139                 labelMap.put(aLabel, s);
140             }
141         }
142         s.addSample(res);
143         total.addSample(res);
144         this.fireDataChanged(s);
145     }
146
147     /**
148      * Reset everything we can in the model.
149      */

150     public void clear()
151     {
152         // clear the data structures
153
runningSamples.clear();
154         labelMap.clear();
155         total = new RunningSample("__TOTAL__", -1);
156         this.fireDataChanged();
157     }
158
159     /**
160      * Called when the model changes - then we call out to all registered
161      * listeners and tell them to update themselves.
162      */

163     protected void fireDataChanged()
164     {
165         Iterator JavaDoc iter = listeners.iterator();
166
167         while (iter.hasNext())
168         {
169             Object JavaDoc myObj = iter.next();
170
171             if (!(myObj instanceof GraphListener))
172             {
173                 continue;
174             }
175             ((GraphListener) myObj).updateGui();
176         }
177     }
178
179     protected void fireDataChanged(RunningSample s)
180     {
181         Iterator JavaDoc iter = listeners.iterator();
182
183         while (iter.hasNext())
184         {
185             Object JavaDoc myObj = iter.next();
186
187             if (!(myObj instanceof AccumListener))
188             {
189                 continue;
190             }
191             ((AccumListener) myObj).updateGui(s);
192         }
193     }
194
195     public static class Test extends junit.framework.TestCase
196     {
197         public Test(String JavaDoc name)
198         {
199             super(name);
200         }
201
202         private SampleResult sample(String JavaDoc label, long start,
203                 long elapsed, boolean ok)
204         {
205             SampleResult res = SampleResult.createTestSample(start, start+elapsed);
206
207             res.setSampleLabel(label);
208             res.setSuccessful(ok);
209             assertEquals(elapsed,res.getTime());
210             return res;
211         }
212
213         public void testStatisticsCalculation()
214         {
215             StatVisualizerModel m = new StatVisualizerModel();
216             long t0 = System.currentTimeMillis();
217
218
219             //m.addNewSample(sample("1", t0 + 100, 100, true));
220
//m.addNewSample(sample("2", t0 + 350, 200, true));
221
//m.addNewSample(sample("1", t0 + 600, 300, true));
222

223             /*
224              * Create 3 samples lasting a total of 600 ms
225              */

226             m.addNewSample(sample("1", t0 + 000, 100, true));
227             m.addNewSample(sample("2", t0 + 150, 200, true));
228             m.addNewSample(sample("1", t0 + 300, 300, true));
229             
230             assertEquals(2, m.getRunningSampleCount());
231             assertEquals(2, m.labelMap.size());
232
233             {
234                 RunningSample s = m.getRunningSample("1");
235
236                 assertEquals("1", s.getLabel());
237                 assertEquals(2, s.getNumSamples());
238                 assertEquals(100, s.getMin());
239                 assertEquals(300, s.getMax());
240                 assertEquals(200, s.getAverage());
241                 assertEquals(600, s.getElapsed());
242             }
243
244             {
245                 RunningSample s = m.getRunningSample("2");
246
247                 assertEquals("2", s.getLabel());
248                 assertEquals(1, s.getNumSamples());
249                 assertEquals(200, s.getMin());
250                 assertEquals(200, s.getMax());
251                 assertEquals(200, s.getAverage());
252                 assertEquals(200, s.getElapsed());
253             }
254
255             {
256                 RunningSample s = m.getRunningSampleTotal();
257
258                 assertEquals(3, s.getNumSamples());
259                 assertEquals(100, s.getMin());
260                 assertEquals(300, s.getMax());
261                 assertEquals(200, s.getAverage());
262                 assertEquals(600, s.getElapsed());
263                 assertEquals(5.0, s.getRate(), 1e-6);
264             }
265         }
266     }
267 }
268
Popular Tags