@@ -18,6 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
18
18
*/
19
19
package org .apache .batik .test ;
20
20
21
+ import java .util .Arrays ;
21
22
import java .util .Vector ;
22
23
23
24
/**
@@ -74,16 +75,20 @@ public void setAllowedScoreDeviation(double allowedScoreDeviation) {
74
75
/**
75
76
* Force implementations to only implement <code>runOp</code>
76
77
* and other performance specific methods.
78
+ * @return the results of AbstractTest's run method
77
79
*/
80
+ @ Override
78
81
public final TestReport run () {
79
82
return super .run ();
80
83
}
81
84
82
85
/**
83
86
* Force implementations to only implement <code>runOp</code>
84
87
* and other performance specific methods.
88
+ * @return false always, indicating failure
85
89
*/
86
- public final boolean runImplBasic () throws Exception {
90
+ @ Override
91
+ public final boolean runImplBasic () {
87
92
// Should never be called for a PerformanceTest
88
93
return false ;
89
94
}
@@ -95,27 +100,31 @@ public final boolean runImplBasic() throws Exception {
95
100
* or not the score is within the allowed deviation of the
96
101
* reference score.
97
102
*
103
+ * @return the test report
104
+ * @throws java.lang.Exception if an error occurred
98
105
* @see #runRef
99
106
* @see #runOp
100
107
*/
108
+ @ Override
101
109
public final TestReport runImpl () throws Exception {
102
110
int iter = 50 ;
103
111
104
- double refUnit = 0 ;
105
- long refStart = 0 ;
106
- long refEnd = 0 ;
107
- long opEnd = 0 ;
108
- long opStart = 0 ;
109
- double opLength = 0 ;
112
+ double refUnit ;
113
+ long refStart ;
114
+ long refEnd ;
115
+ long opEnd ;
116
+ long opStart ;
117
+ double opLength ;
110
118
111
119
// Run once to remove class load time from timing.
112
120
runRef ();
113
121
runOp ();
114
122
// System.gc();
115
123
116
124
double [] scores = new double [iter ];
117
-
118
- for (int i =0 ; i <iter ; i ++) {
125
+ // Count bad scores so it doesn't get stuck forever
126
+ int badScores = 0 ;
127
+ for (int i =0 ; i <iter ; ) {
119
128
if ( i %2 == 0 ) {
120
129
refStart = System .currentTimeMillis ();
121
130
runRef ();
@@ -133,17 +142,26 @@ public final TestReport runImpl() throws Exception {
133
142
refUnit = refEnd - opEnd ;
134
143
opLength = opEnd - opStart ;
135
144
}
145
+ // Only accept finite and non-zero scores
146
+ if (opLength > 0.0 && refUnit > 0.0 ) {
147
+ scores [i ++] = opLength / refUnit ;
148
+ System .err .print ("." );
149
+ } else {
150
+ System .err .print ("!" );
151
+ if (++badScores > iter ) {
152
+ // Too many bad scores
153
+ throw new IllegalStateException ("Unable to obtain reliable timings" );
136
154
137
- scores [ i ] = opLength / refUnit ;
138
- System . err . println ( "." );
155
+ }
156
+ }
139
157
// System.err.println(">>>>>>>> scores[" + i + "] = " + scores[i] + " (" + refUnit + " / " + opLength + ")");
140
158
System .gc ();
141
159
}
142
160
143
161
System .err .println ();
144
162
145
163
// Now, sort the scores
146
- sort (scores );
164
+ Arrays . sort (scores );
147
165
148
166
// Compute the mean score based on the scores, not accounting
149
167
// for the lowest and highest scores
@@ -184,22 +202,6 @@ public final TestReport runImpl() throws Exception {
184
202
}
185
203
}
186
204
187
- protected void sort (double [] a ) throws Exception {
188
- for (int i = a .length - 1 ; i >=0 ; i --) {
189
- boolean swapped = false ;
190
- for (int j = 0 ; j <i ; j ++) {
191
- if (a [j ] > a [j +1 ]) {
192
- double d = a [j ];
193
- a [j ] = a [j +1 ];
194
- a [j +1 ] = d ;
195
- swapped = true ;
196
- }
197
- }
198
- if (!swapped )
199
- return ;
200
- }
201
- }
202
-
203
205
/**
204
206
* Runs the reference operation.
205
207
* By default, this runs the same BufferedImage drawing
@@ -220,6 +222,7 @@ protected void runRef() {
220
222
221
223
/**
222
224
* Runs the tested operation
225
+ * @throws java.lang.Exception if an error occurred
223
226
*/
224
227
protected abstract void runOp () throws Exception ;
225
228
}
0 commit comments