KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > testsuite > perf > BasePerfTest


1 /*
2  Copyright (C) 2002-2004 MySQL AB
3
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of version 2 of the GNU General Public License as
6  published by the Free Software Foundation.
7
8  There are special exceptions to the terms and conditions of the GPL
9  as it is applied to this software. View the full text of the
10  exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
11  software distribution.
12
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22
23
24  */

25 package testsuite.perf;
26
27 import testsuite.BaseTestCase;
28
29 import java.text.NumberFormat JavaDoc;
30
31 import java.util.ArrayList JavaDoc;
32 import java.util.List JavaDoc;
33
34 /**
35  * Base class for performance test cases. Handles statistics.
36  *
37  * @author Mark Matthews
38  */

39 public abstract class BasePerfTest extends BaseTestCase {
40     // ~ Static fields/initializers
41
// ---------------------------------------------
42

43     /**
44      * Confidence interval lookup table, indexed by degrees of freedom at 95%.
45      */

46     private static final double[] T95 = { 12.706, 4.303, 3.182, 2.776, 2.571,
47             2.447, 2.365, 2.306, 2.262, 2.228, 2.201, 2.179, 2.160, 2.145,
48             2.131, 2.120, 2.110, 2.101, 2.093, 2.086, 2.080, 2.074, 2.069,
49             2.064, 2.060, 2.056, 2.052, 2.048, 2.045, 2.042 };
50
51     /**
52      * Confidence interval lookup table, indexed by degrees of freedom at 99%.
53      */

54     private static final double[] T99 = { 63.657, 9.925, 5.841, 4.604, 4.032,
55             3.707, 3.499, 3.355, 3.250, 3.169, 3.106, 3.055, 3.012, 2.977,
56             2.947, 2.921, 2.898, 2.878, 2.861, 2.845, 2.831, 2.819, 2.807,
57             2.797, 2.787, 2.779, 2.771, 2.763, 2.756, 2.750 };
58
59     static NumberFormat JavaDoc numberFormatter = NumberFormat.getInstance();
60
61     static {
62         numberFormatter.setMaximumFractionDigits(4);
63         numberFormatter.setMinimumFractionDigits(4);
64     }
65
66     // ~ Instance fields
67
// --------------------------------------------------------
68

69     /**
70      * List of values for each iteration
71      */

72     private List JavaDoc testValuesList = new ArrayList JavaDoc();
73
74     private double confidenceLevel = 95; // 95% by default
75

76     private double confidenceValue = 0;
77
78     private double intervalWidth = 0.1;
79
80     private double meanValue = 0;
81
82     private double squareSumValue = 0;
83
84     private double sumValue = 0;
85
86     private double variationValue = 0;
87
88     /**
89      * The number of iterations that we have performed
90      */

91     private int numIterations = 0;
92
93     // ~ Constructors
94
// -----------------------------------------------------------
95

96     /**
97      * Creates a new BasePerfTest object.
98      *
99      * @param name
100      * the testcase name to perform.
101      */

102     public BasePerfTest(String JavaDoc name) {
103         super(name);
104     }
105
106     // ~ Methods
107
// ----------------------------------------------------------------
108

109     /**
110      * Returns the meanValue.
111      *
112      * @return double
113      */

114     public double getMeanValue() {
115         return this.meanValue;
116     }
117
118     /**
119      * Sub-classes should override this to perform the operation to be measured.
120      *
121      * @throws Exception
122      * if an error occurs.
123      */

124     protected abstract void doOneIteration() throws Exception JavaDoc;
125
126     /**
127      * Returns the current confidence level.
128      *
129      * @return the current confindence level.
130      */

131     protected double getCurrentConfidence() {
132         return (this.intervalWidth - this.confidenceValue) * 100;
133     }
134
135     /**
136      * Returns the current margin of error.
137      *
138      * @return the current margin of error.
139      */

140     protected double getMarginOfError() {
141         return getConfidenceLookup()
142                 * (getStandardDeviationP() / Math.sqrt(this.numIterations));
143     }
144
145     /**
146      * Returns the current STDDEV.
147      *
148      * @return the current STDDEV
149      */

150     protected double getStandardDeviationP() {
151         if (this.numIterations < 1) {
152             return 0;
153         }
154
155         return Math
156                 .sqrt(((this.numIterations * this.squareSumValue) - (this.sumValue * this.sumValue))
157                         / (this.numIterations * this.numIterations));
158     }
159
160     /**
161      * Adds one test result to the statistics.
162      *
163      * @param value
164      * a single result representing the value being measured in the
165      * test.
166      */

167     protected void addResult(double value) {
168         this.numIterations++;
169         this.testValuesList.add(new Double JavaDoc(value));
170
171         this.sumValue += value;
172         this.squareSumValue += (value * value);
173         this.meanValue = this.sumValue / this.numIterations;
174         this.variationValue = (this.squareSumValue / this.numIterations)
175                 - (this.meanValue * this.meanValue);
176
177         // Can only have confidence when more than one test
178
// has been completed
179
if (this.numIterations > 1) {
180             this.confidenceValue = this.intervalWidth
181                     - ((2.0 * getConfidenceLookup() * Math
182                             .sqrt(this.variationValue
183                                     / (this.numIterations - 1.0))) / this.meanValue);
184         }
185     }
186
187     /**
188      * Calls doIteration() the <code>numIterations</code> times, displaying
189      * the mean, std, margin of error and confidence level.
190      *
191      * @param numIterations
192      * the number of iterations to perform ( < 30)
193      * @throws Exception
194      * if an error occurs.
195      */

196     protected void doIterations(int numIterations) throws Exception JavaDoc {
197         for (int i = 0; i < numIterations; i++) {
198             doOneIteration();
199         }
200     }
201
202     /**
203      * Reports the current results to STDOUT, preceeded by
204      * <code>additionalMessage</code> if not null.
205      *
206      * @param additionalMessage
207      * the additional message to print, or null if no message.
208      */

209     protected synchronized void reportResults(String JavaDoc additionalMessage) {
210         StringBuffer JavaDoc messageBuf = new StringBuffer JavaDoc();
211
212         if (additionalMessage != null) {
213             messageBuf.append(additionalMessage);
214             messageBuf.append(": ");
215         }
216
217         messageBuf.append(" mean: ");
218         messageBuf.append(numberFormatter.format(this.meanValue));
219         messageBuf.append(" stdevp: ");
220         messageBuf.append(numberFormatter.format(getStandardDeviationP()));
221         messageBuf.append(" m-o-e: ");
222         messageBuf.append(numberFormatter.format(getMarginOfError()));
223
224         System.out.println(messageBuf.toString());
225     }
226
227     private double getConfidenceLookup() {
228         if (this.confidenceLevel == 95) {
229             return T95[this.numIterations - 1];
230         } else if (this.confidenceLevel == 99) {
231             return T99[this.numIterations - 1];
232         } else {
233             throw new IllegalArgumentException JavaDoc(
234                     "Confidence level must be 95 or 99");
235         }
236     }
237 }
238
Popular Tags