KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/visualizers/Attic/GraphModel.java,v 1.14 2004/02/13 01:48:46 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.io.Serializable JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.jmeter.samplers.Clearable;
28 import org.apache.jmeter.samplers.SampleResult;
29 import org.apache.jorphan.math.StatCalculator;
30
31 /**
32  * @author Michael Stover
33  * Created February 8, 2001
34  * @version $Revision: 1.14 $ Last updated: $Date: 2004/02/13 01:48:46 $
35  */

36 public class GraphModel implements Clearable, Serializable JavaDoc
37 {
38
39     private String JavaDoc name;
40     private List JavaDoc samples;
41     private List JavaDoc listeners;
42     //private long previous = 0;
43
private boolean bigChange = false;
44     private Sample current = new Sample(0, 0, 0, 0, 0,false);
45     private long startTime = Long.MAX_VALUE;
46     private long endTime = Long.MIN_VALUE;
47     private int throughputMax = 20;
48     private long graphMax = 20;
49     private StatCalculator statCalc = new StatCalculator();
50
51     /**
52      * Constructor for the GraphModel object.
53      */

54     public GraphModel()
55     {
56         listeners = new LinkedList JavaDoc();
57         samples = Collections.synchronizedList(new LinkedList JavaDoc());
58     }
59
60     /**
61      * Sets the Name attribute of the GraphModel object.
62      *
63      * @param name the new Name value
64      */

65
66     public void setName(String JavaDoc name)
67     {
68         this.name = name;
69     }
70
71     /**
72      * Gets the CurrentData attribute of the GraphModel object.
73      *
74      * @return the CurrentData value
75      */

76     public long getCurrentData()
77     {
78         return current.data;
79     }
80
81     /**
82      * Gets the CurrentAverage attribute of the GraphModel object.
83      *
84      * @return the CurrentAverage value
85      */

86     public long getCurrentAverage()
87     {
88         return current.average;
89     }
90     
91     public long getCurrentMedian()
92     {
93         return current.median;
94     }
95
96     /**
97      * Gets the CurrentDeviation attribute of the GraphModel object.
98      *
99      * @return the CurrentDeviation value
100      */

101     public long getCurrentDeviation()
102     {
103         return current.deviation;
104     }
105
106     public float getCurrentThroughput()
107     {
108         return current.throughput;
109     }
110
111     /**
112      * Gets the SampleCount attribute of the GraphModel object.
113      *
114      * @return the SampleCount value
115      */

116     public int getSampleCount()
117     {
118         return samples.size();
119     }
120
121     /**
122      * Gets the Samples attribute of the GraphModel object.
123      *
124      * @return the Samples value
125      */

126     public List JavaDoc getSamples()
127     {
128         return samples;
129     }
130
131     /**
132      * Gets the GuiClass attribute of the GraphModel object.
133      *
134      * @return the GuiClass value
135      */

136     public Class JavaDoc getGuiClass()
137     {
138         return GraphVisualizer.class;
139     }
140
141     /**
142      * Gets the Name attribute of the GraphModel object.
143      *
144      * @return the Name value
145      */

146     public String JavaDoc getName()
147     {
148         return name;
149     }
150
151     /**
152      * Gets the Max attribute of the GraphModel object.
153      *
154      * @return the Max value
155      */

156     public long getMaxSample()
157     {
158         return statCalc.getMax().longValue();
159     }
160
161     public long getGraphMax()
162     {
163         return graphMax;
164     }
165
166     public int getThroughputMax()
167     {
168         return throughputMax;
169     }
170
171     /**
172      * Adds a feature to the ModelListener attribute of the GraphModel object.
173      *
174      * @param listener the feature to be added to the ModelListener
175      * attribute
176      */

177     public void addGraphListener(GraphListener listener)
178     {
179         listeners.add(listener);
180     }
181
182     /**
183      * Adds a feature to the Sample attribute of the GraphModel object.
184      *
185      * @param e the feature to be added to the Sample attribute
186      */

187     public Sample addSample(SampleResult e)
188     {
189         Sample s =
190             addNewSample(e.getTime(), e.getTimeStamp(), e.isSuccessful());
191
192         fireDataChanged();
193         return s;
194     }
195
196     public void clear()
197     {
198         samples.clear();
199         //previous = 0;
200
graphMax = 1;
201         bigChange = true;
202         current = new Sample(0, 0, 0, 0, 0,false);
203         statCalc.clear();
204         startTime = Long.MAX_VALUE;
205         endTime = Long.MIN_VALUE;
206         this.fireDataChanged();
207     }
208
209     protected void fireDataChanged()
210     {
211         Iterator JavaDoc iter = listeners.iterator();
212
213         if (bigChange)
214         {
215             while (iter.hasNext())
216             {
217                 ((GraphListener) iter.next()).updateGui();
218             }
219             bigChange = false;
220         }
221         else
222         {
223             quickUpdate(current);
224         }
225     }
226
227     protected void quickUpdate(Sample s)
228     {
229         Iterator JavaDoc iter = listeners.iterator();
230         {
231             while (iter.hasNext())
232             {
233                 ((GraphListener) iter.next()).updateGui(s);
234             }
235         }
236     }
237
238     /**
239      * Adds a feature to the NewSample attribute of the GraphModel object.
240      *
241      * @param sample the feature to be added to the NewSample attribute
242      */

243     protected Sample addNewSample(long sample, long timeStamp, boolean success)
244     {
245         //NOTUSED int counter = 0;
246
float average;
247         long deviation, median;
248         synchronized (statCalc)
249         {
250             statCalc.addValue(sample);
251             //NOTUSED counter = statCalc.getCount();
252
average = (float) statCalc.getMean();
253             deviation = (long) statCalc.getStandardDeviation();
254             median = statCalc.getMedian().longValue();
255             long start = timeStamp - sample;
256             if (startTime > start)
257             {
258                 startTime = start;
259             }
260             if(endTime < timeStamp)
261             {
262                 endTime = timeStamp;
263             }
264         }
265
266         float throughput = 0;
267
268         if (endTime - startTime > 0)
269         {
270             throughput =
271                 (float) (((float) (samples.size() + 1))
272                     / ((float) (timeStamp - startTime))
273                     * 60000);
274         }
275         if (throughput > throughputMax)
276         {
277             bigChange = true;
278             throughputMax = (int) (throughput * 1.5F);
279         }
280         if (average > graphMax)
281         {
282             bigChange = true;
283             graphMax = (long) average * 3;
284         }
285         if (deviation > graphMax)
286         {
287             bigChange = true;
288             graphMax = deviation * 3;
289         }
290         Sample s =
291             new Sample(
292                 sample,
293                 (long) average,
294                 deviation,
295                 throughput,
296                 median,
297                 !success);
298
299         //previous = sample;
300
current = s;
301         samples.add(s);
302         return s;
303     }
304 }
305
Popular Tags