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