1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
/*******************************************************************************
* Copyright (c) 2000, 2012 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.core.tests.dom;
import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import junit.framework.Test;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.tests.model.AbstractJavaModelTests;
public class ProfilingASTConvertionTest extends AbstractJavaModelTests {
/**
* Internal synonynm for deprecated constant AST.JSL3
* to alleviate deprecation warnings.
* @deprecated
*/
/*package*/ static final int JLS3_INTERNAL = AST.JLS3;
static class Result implements Comparable {
long length;
long time;
String unitName;
Result(String unitName, long time, long length) {
this.time = time;
this.unitName = unitName;
this.length = length / 1024;
}
/* (non-Javadoc)
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
public int compareTo(Object o) {
Result result = (Result) o;
if (this.time < result.time) {
return -1;
} else if (this.time == result.time) {
return 0;
}
return 1;
}
}
private static final int INCREMENTS = 100;
private static boolean RESOLVE_BINDINGS = false;
public static Test suite() {
return buildModelTestSuite(ProfilingASTConvertionTest.class);
}
ICompilationUnit[] compilationUnits;
public ProfilingASTConvertionTest(String name) {
super(name);
}
public String display(int value, int numberOfFiguresForRange) {
int numberOfFigures = value == 0 ? 1 : (int) (Math.log(value)/ Math.log(10));
if ((value % 10) == 0) {
numberOfFigures = (int) (Math.log(value + 1)/ Math.log(10));
}
StringBuffer buffer = new StringBuffer();
while(numberOfFigures < numberOfFiguresForRange) {
buffer.append(" ");
numberOfFigures++;
}
buffer.append(value);
return String.valueOf(buffer);
}
/**
* @param array
* @param increment
*/
private void printDistribution(long[] array, int increment) {
int bound = increment;
int counter = 0;
int totalCounter = 0;
int length = array.length;
long max = array[length - 1];
int numberOfFiguresForRange = (int) (Math.log(max)/ Math.log(10));
if ((max % increment) == 0) {
numberOfFiguresForRange = (int) (Math.log(max + 1)/ Math.log(10));
}
int numberOfFiguresForCounter = (int) (Math.log(length)/ Math.log(10));
if ((length % increment) == 0) {
numberOfFiguresForCounter = (int) (Math.log(length + 1)/ Math.log(10));
}
for (int i = 0; i < length; i++) {
if (array[i] < bound) {
counter++;
} else {
i--;
totalCounter += counter;
printRange(counter, bound, increment, totalCounter, length, numberOfFiguresForRange, numberOfFiguresForCounter);
counter = 0;
bound += increment;
}
}
totalCounter += counter;
printRange(counter, bound, increment, totalCounter, length, numberOfFiguresForRange, numberOfFiguresForCounter);
}
/**
* @param counter
* @param bound
*/
private void printRange(int counter, int bound, int increment, int totalCounter, int length, int numberOfFiguresForRange, int numberOfFiguresForCounters) {
if (counter != 0) {
StringBuffer buffer = new StringBuffer();
int low = bound - increment;
if (low != 0) {
low++;
}
DecimalFormat format = new DecimalFormat("###.##");
buffer
.append(display(low, numberOfFiguresForRange))
.append(" - ")
.append(display(bound, numberOfFiguresForRange))
.append(" : ")
.append(display(counter, numberOfFiguresForCounters))
.append("\t\t")
.append(format.format(100.0 * ((double) totalCounter / length)));
System.out.println(String.valueOf(buffer));
}
}
/**
* @param totalTime
* @param length
* @param times
* @param arrayList
* @deprecated using deprecated code
*/
private void reportResults(int apiLevel, long totalTime, int length, long[] times, ArrayList arrayList) {
System.out.println("===============================================================================");
System.out.print("================================ ");
switch(apiLevel) {
case AST.JLS2 :
System.out.print("JLS2");
break;
case JLS3_INTERNAL :
System.out.print("JLS3");
break;
}
System.out.print(" BINDING IS ");
System.out.print(RESOLVE_BINDINGS ? "ON " : "OFF ");
System.out.println("==========================");
System.out.println("===============================================================================");
Arrays.sort(times);
System.out.println("===================================== TIMES ===================================");
System.out.println("Fastest = " + times[0] + "ms");
long maxTime = times[length - 1];
System.out.println("Slowest = " + maxTime + "ms");
System.out.println("Total = " + totalTime + "ms");
System.out.println("================================== DISTRIBUTION ===============================");
printDistribution(times, INCREMENTS);
System.out.println("================================= SORTED BY TIME ==============================");
Collections.sort(arrayList);
for (Iterator iterator = arrayList.iterator(); iterator.hasNext(); ) {
final Result next = (Result) iterator.next();
System.out.println(next.unitName + "(" + next.length + "KB) - " + next.time + "ms");
}
System.out.println("================================ SORTED BY LENGTH =============================");
Collections.sort(arrayList, new Comparator() {
/* (non-Javadoc)
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
public int compare(Object o1, Object o2) {
Result r1 = (Result) o1;
Result r2 = (Result) o2;
if (r1.length < r2.length) {
return -1;
} else if (r1.length == r2.length) {
return 0;
}
return 1;
}
});
for (Iterator iterator = arrayList.iterator(); iterator.hasNext(); ) {
final Result next = (Result) iterator.next();
System.out.println(next.unitName + "(" + next.length + "KB) - " + next.time + "ms");
}
}
public void setUpSuite() throws Exception {
super.setUpSuite();
// ensure variables are set
if (JavaCore.getClasspathVariable("CONVERTER_JCL_LIB") == null) { //$NON-NLS-1$
setupExternalJCL("converterJclMin");
JavaCore.setClasspathVariables(
new String[] {"CONVERTER_JCL_LIB", "CONVERTER_JCL_SRC", "CONVERTER_JCL_SRCROOT"}, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
new Path[] {new Path(getExternalPath() + "converterJclMin.jar"), new Path(getExternalPath() + "converterJclMinsrc.zip"), new Path("")},
null);
}
IJavaProject javaProject = setUpJavaProject("Compiler", "1.4"); //$NON-NLS-1$ //$NON-NLS-2$
assertNotNull("No java project", javaProject);
IPackageFragment[] packageFragments = javaProject.getPackageFragments();
assertNotNull("No package fragments", packageFragments);
ArrayList collector = new ArrayList();
for (int i = 0, max = packageFragments.length; i < max; i++) {
ICompilationUnit[] units = packageFragments[i].getCompilationUnits();
if (units != null) {
for (int j = 0, max2 = units.length; j < max2; j++) {
collector.add(units[j]);
}
}
}
this.compilationUnits = new ICompilationUnit[collector.size()];
collector.toArray(this.compilationUnits);
}
public void test0000() throws JavaModelException {
try {
RESOLVE_BINDINGS = true;
final int apiLevel = JLS3_INTERNAL;
ASTParser parser = ASTParser.newParser(apiLevel);
parser.setResolveBindings(RESOLVE_BINDINGS);
int length = this.compilationUnits.length;
long[] times = new long[length];
ArrayList arrayList = new ArrayList(length);
for (int i = 0; i < length; i++) {
parser.setSource(this.compilationUnits[i]);
parser.setResolveBindings(RESOLVE_BINDINGS);
long time = System.currentTimeMillis();
ASTNode node = parser.createAST(null);
times[i] = System.currentTimeMillis() - time;
assertNotNull("No node", node);
assertEquals("Wrong type", ASTNode.COMPILATION_UNIT, node.getNodeType());
CompilationUnit unit = (CompilationUnit) node;
assertEquals("Has problem", 0, unit.getProblems().length);
TypeDeclaration typeDeclaration = (TypeDeclaration) unit.types().get(0);
StringBuffer buffer = new StringBuffer();
buffer.append(unit.getPackage().getName()).append(".").append(typeDeclaration.getName());
IResource resource = this.compilationUnits[i].getResource();
if (resource instanceof IFile) {
IFile file = (IFile) resource;
File f = new File(file.getLocation().toOSString());
if (f.exists()) {
arrayList.add(new Result(String.valueOf(buffer), times[i], f.length()));
}
}
}
} finally {
RESOLVE_BINDINGS = false;
}
}
public void test0001() throws JavaModelException {
try {
RESOLVE_BINDINGS = true;
final int apiLevel = JLS3_INTERNAL;
ASTParser parser = ASTParser.newParser(apiLevel);
parser.setResolveBindings(RESOLVE_BINDINGS);
long totalTime = 0;
int length = this.compilationUnits.length;
long[] times = new long[length];
ArrayList arrayList = new ArrayList(length);
for (int i = 0; i < length; i++) {
parser.setSource(this.compilationUnits[i]);
parser.setResolveBindings(RESOLVE_BINDINGS);
long time = System.currentTimeMillis();
ASTNode node = parser.createAST(null);
times[i] = System.currentTimeMillis() - time;
totalTime += times[i];
assertNotNull("No node", node);
assertEquals("Wrong type", ASTNode.COMPILATION_UNIT, node.getNodeType());
CompilationUnit unit = (CompilationUnit) node;
assertEquals("Has problem", 0, unit.getProblems().length);
TypeDeclaration typeDeclaration = (TypeDeclaration) unit.types().get(0);
StringBuffer buffer = new StringBuffer();
buffer.append(unit.getPackage().getName()).append(".").append(typeDeclaration.getName());
IResource resource = this.compilationUnits[i].getResource();
if (resource instanceof IFile) {
IFile file = (IFile) resource;
File f = new File(file.getLocation().toOSString());
if (f.exists()) {
arrayList.add(new Result(String.valueOf(buffer), times[i], f.length()));
}
}
}
reportResults(apiLevel, totalTime, length, times, arrayList);
} finally {
RESOLVE_BINDINGS = false;
}
}
public void test0002() throws JavaModelException {
try {
RESOLVE_BINDINGS = false;
final int apiLevel = JLS3_INTERNAL;
ASTParser parser = ASTParser.newParser(apiLevel);
parser.setResolveBindings(RESOLVE_BINDINGS);
long totalTime = 0;
int length = this.compilationUnits.length;
long[] times = new long[length];
ArrayList arrayList = new ArrayList(length);
for (int i = 0; i < length; i++) {
parser.setSource(this.compilationUnits[i]);
parser.setResolveBindings(RESOLVE_BINDINGS);
long time = System.currentTimeMillis();
ASTNode node = parser.createAST(null);
times[i] = System.currentTimeMillis() - time;
totalTime += times[i];
assertNotNull("No node", node);
assertEquals("Wrong type", ASTNode.COMPILATION_UNIT, node.getNodeType());
CompilationUnit unit = (CompilationUnit) node;
assertEquals("Has problem", 0, unit.getProblems().length);
TypeDeclaration typeDeclaration = (TypeDeclaration) unit.types().get(0);
StringBuffer buffer = new StringBuffer();
buffer.append(unit.getPackage().getName()).append(".").append(typeDeclaration.getName());
IResource resource = this.compilationUnits[i].getResource();
if (resource instanceof IFile) {
IFile file = (IFile) resource;
File f = new File(file.getLocation().toOSString());
if (f.exists()) {
arrayList.add(new Result(String.valueOf(buffer), times[i], f.length()));
}
}
}
reportResults(apiLevel, totalTime, length, times, arrayList);
} finally {
RESOLVE_BINDINGS = false;
}
}
/** @deprecated using deprecated code */
public void test0003() throws JavaModelException {
try {
RESOLVE_BINDINGS = true;
final int apiLevel = AST.JLS2;
ASTParser parser = ASTParser.newParser(apiLevel);
parser.setResolveBindings(RESOLVE_BINDINGS);
long totalTime = 0;
int length = this.compilationUnits.length;
long[] times = new long[length];
ArrayList arrayList = new ArrayList(length);
for (int i = 0; i < length; i++) {
parser.setSource(this.compilationUnits[i]);
parser.setResolveBindings(RESOLVE_BINDINGS);
long time = System.currentTimeMillis();
ASTNode node = parser.createAST(null);
times[i] = System.currentTimeMillis() - time;
totalTime += times[i];
assertNotNull("No node", node);
assertEquals("Wrong type", ASTNode.COMPILATION_UNIT, node.getNodeType());
CompilationUnit unit = (CompilationUnit) node;
assertEquals("Has problem", 0, unit.getProblems().length);
TypeDeclaration typeDeclaration = (TypeDeclaration) unit.types().get(0);
StringBuffer buffer = new StringBuffer();
buffer.append(unit.getPackage().getName()).append(".").append(typeDeclaration.getName());
IResource resource = this.compilationUnits[i].getResource();
if (resource instanceof IFile) {
IFile file = (IFile) resource;
File f = new File(file.getLocation().toOSString());
if (f.exists()) {
arrayList.add(new Result(String.valueOf(buffer), times[i], f.length()));
}
}
}
reportResults(apiLevel, totalTime, length, times, arrayList);
} finally {
RESOLVE_BINDINGS = false;
}
}
/** @deprecated using deprecated code */
public void test0004() throws JavaModelException {
try {
RESOLVE_BINDINGS = false;
final int apiLevel = AST.JLS2;
ASTParser parser = ASTParser.newParser(apiLevel);
parser.setResolveBindings(RESOLVE_BINDINGS);
long totalTime = 0;
int length = this.compilationUnits.length;
long[] times = new long[length];
ArrayList arrayList = new ArrayList(length);
for (int i = 0; i < length; i++) {
parser.setSource(this.compilationUnits[i]);
parser.setResolveBindings(RESOLVE_BINDINGS);
long time = System.currentTimeMillis();
ASTNode node = parser.createAST(null);
times[i] = System.currentTimeMillis() - time;
totalTime += times[i];
assertNotNull("No node", node);
assertEquals("Wrong type", ASTNode.COMPILATION_UNIT, node.getNodeType());
CompilationUnit unit = (CompilationUnit) node;
assertEquals("Has problem", 0, unit.getProblems().length);
TypeDeclaration typeDeclaration = (TypeDeclaration) unit.types().get(0);
StringBuffer buffer = new StringBuffer();
buffer.append(unit.getPackage().getName()).append(".").append(typeDeclaration.getName());
IResource resource = this.compilationUnits[i].getResource();
if (resource instanceof IFile) {
IFile file = (IFile) resource;
File f = new File(file.getLocation().toOSString());
if (f.exists()) {
arrayList.add(new Result(String.valueOf(buffer), times[i], f.length()));
}
}
}
reportResults(apiLevel, totalTime, length, times, arrayList);
} finally {
RESOLVE_BINDINGS = false;
}
}
}
|