KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/visualizers/SplineModel.java,v 1.6 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
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24
25 import org.apache.jmeter.samplers.Clearable;
26 import org.apache.jmeter.samplers.SampleResult;
27 import org.apache.jmeter.util.JMeterUtils;
28
29
30 /**
31  * @author Michael Stover
32  * @version $Revision: 1.6 $
33  */

34 public class SplineModel implements Clearable
35 {
36     public final int DEFAULT_NUMBER_OF_NODES = 10;
37     public final int DEFAULT_REFRESH_PERIOD = 1;
38
39     protected final boolean SHOW_INCOMING_SAMPLES = true;
40     protected int numberOfNodes = DEFAULT_NUMBER_OF_NODES;
41     protected int refreshPeriod = DEFAULT_REFRESH_PERIOD;
42
43     /** Current Spline curve. */
44     protected Spline3 dataCurve = null;
45
46     /** Sum of the samples. */
47     protected long sum = 0;
48
49     /** Average of the samples. */
50     protected long average = 0;
51
52     /** Number of collected samples. */
53     protected long n = 0;
54
55     ArrayList JavaDoc samples;
56
57     private GraphListener listener;
58
59     private long minimum = Integer.MAX_VALUE;
60     private long maximum = Integer.MIN_VALUE;
61     private long incoming;
62
63     private String JavaDoc name;
64
65     public SplineModel()
66     {
67         samples = new ArrayList JavaDoc();
68     }
69
70     public void setListener(GraphListener vis)
71     {
72         listener = vis;
73     }
74
75     public void setName(String JavaDoc newName)
76     {
77         name = newName;
78     }
79
80     public boolean isEditable()
81     {
82         return true;
83     }
84
85     public Spline3 getDataCurve()
86     {
87         return dataCurve;
88     }
89
90     public Class JavaDoc getGuiClass()
91     {
92         return org.apache.jmeter.visualizers.SplineVisualizer.class;
93     }
94
95     public Collection JavaDoc getAddList()
96     {
97         return null;
98     }
99
100     public String JavaDoc getClassLabel()
101     {
102         return JMeterUtils.getResString("spline_visualizer_title");
103     }
104
105     public long getMinimum()
106     {
107         return minimum;
108     }
109
110     public long getMaximum()
111     {
112         return maximum;
113     }
114
115     public long getAverage()
116     {
117         return average;
118     }
119
120     public long getCurrent()
121     {
122         return incoming;
123     }
124
125     public long[] getSamples()
126     {
127         int n = samples.size();
128         long[] longSample = new long[n];
129
130         for (int i = 0; i < n; i++)
131         {
132             longSample[i] = ((Long JavaDoc) samples.get(i)).longValue();
133         }
134         return longSample;
135     }
136
137     public long getSample(int i)
138     {
139         Long JavaDoc sample = (Long JavaDoc) this.samples.get(i);
140
141         return sample.longValue();
142     }
143
144     public int getNumberOfCollectedSamples()
145     {
146         return this.samples.size();
147     }
148
149     public String JavaDoc getName()
150     {
151         return name;
152     }
153
154     public void uncompile()
155     {
156         clear();
157     }
158
159     public synchronized void clear()
160     {
161         // this.graph.clear();
162
samples.clear();
163
164         this.n = 0;
165         this.sum = 0;
166         this.average = 0;
167
168         minimum = Integer.MAX_VALUE;
169         maximum = Integer.MIN_VALUE;
170
171         this.dataCurve = null;
172
173         if (listener != null)
174         {
175             listener.updateGui();
176         }
177     }
178
179     public synchronized void add(SampleResult sampleResult)
180     {
181         long sample = sampleResult.getTime();
182
183         this.n++;
184         this.sum += sample;
185         this.average = this.sum / this.n;
186         if (SHOW_INCOMING_SAMPLES)
187         {
188             incoming = sample;
189         }
190         if (sample > maximum)
191         {
192             maximum = sample;
193         }
194         if (sample < minimum)
195         {
196             minimum = sample;
197         }
198         samples.add(new Long JavaDoc(sample));
199         int n = getNumberOfCollectedSamples();
200
201         if ((n % (numberOfNodes * refreshPeriod)) == 0)
202         {
203             float[] floatNode = new float[numberOfNodes];
204             //NOTUSED: long[] longSample = getSamples();
205
// load each node
206
int loadFactor = n / numberOfNodes;
207
208             for (int i = 0; i < numberOfNodes; i++)
209             {
210                 for (int j = 0; j < loadFactor; j++)
211                 {
212                     floatNode[i] += getSample((i * loadFactor) + j);
213                 }
214                 floatNode[i] = floatNode[i] / loadFactor;
215             }
216             // compute the new Spline curve
217
dataCurve = new Spline3(floatNode);
218             if (listener != null)
219             {
220                 listener.updateGui();
221             }
222         }
223         else
224         {// do nothing, wait for the next pile to complete
225
}
226     }
227 }
228
Popular Tags