1 package net.sourceforge.pmd.util; 2 3 import net.sourceforge.pmd.PMD; 4 import net.sourceforge.pmd.PMDException; 5 import net.sourceforge.pmd.Rule; 6 import net.sourceforge.pmd.RuleContext; 7 import net.sourceforge.pmd.RuleSet; 8 import net.sourceforge.pmd.RuleSetFactory; 9 import net.sourceforge.pmd.RuleSetNotFoundException; 10 import net.sourceforge.pmd.SimpleRuleSetNameMapper; 11 import net.sourceforge.pmd.SourceFileSelector; 12 import net.sourceforge.pmd.TargetJDK1_4; 13 import net.sourceforge.pmd.TargetJDK1_5; 14 import net.sourceforge.pmd.TargetJDKVersion; 15 import net.sourceforge.pmd.ast.JavaParser; 16 import net.sourceforge.pmd.cpd.FileFinder; 17 import net.sourceforge.pmd.cpd.SourceFileOrDirectoryFilter; 18 19 import java.io.File; 20 import java.io.FileNotFoundException; 21 import java.io.FileReader; 22 import java.io.IOException; 23 import java.util.Collection; 24 import java.util.Iterator; 25 import java.util.List; 26 import java.util.Set; 27 import java.util.TreeSet; 28 29 public class Benchmark { 30 31 private static class Result implements Comparable { 32 public Rule rule; 33 public long time; 34 35 public int compareTo(Object o) { 36 Result other = (Result) o; 37 if (other.time < time) { 38 return -1; 39 } else if (other.time > time) { 40 return 1; 41 } 42 43 return rule.getName().compareTo(((Result) o).rule.getName()); 44 } 45 46 public Result(long elapsed, Rule rule) { 47 this.rule = rule; 48 this.time = elapsed; 49 } 50 } 51 52 private static boolean findBooleanSwitch(String[] args, String name) { 53 for (int i = 0; i < args.length; i++) { 54 if (args[i].equals(name)) { 55 return true; 56 } 57 } 58 return false; 59 } 60 61 private static String findOptionalStringValue(String[] args, String name, String defaultValue) { 62 for (int i = 0; i < args.length; i++) { 63 if (args[i].equals(name)) { 64 return args[i + 1]; 65 } 66 } 67 return defaultValue; 68 } 69 70 public static void main(String[] args) throws RuleSetNotFoundException, IOException, PMDException { 71 72 String srcDir = findOptionalStringValue(args, "--source-directory", "/usr/local/java/src/java/lang/"); 73 List files = new FileFinder().findFilesFrom(srcDir, new SourceFileOrDirectoryFilter(new SourceFileSelector()), true); 74 75 TargetJDKVersion jdk = new TargetJDK1_4(); 76 if (findOptionalStringValue(args, "--targetjdk", "1.4").equals("1.5")) { 77 jdk = new TargetJDK1_5(); 78 } 79 boolean debug = findBooleanSwitch(args, "--debug"); 80 boolean parseOnly = findBooleanSwitch(args, "--parse-only"); 81 82 if (debug) System.out.println("Using JDK " + jdk.getVersionString()); 83 if (parseOnly) { 84 parseStress(jdk, files); 85 } else { 86 String ruleset = findOptionalStringValue(args, "--ruleset", ""); 87 if (debug) System.out.println("Checking directory " + srcDir); 88 Set results = new TreeSet(); 89 RuleSetFactory factory = new RuleSetFactory(); 90 if (ruleset.length() > 0) { 91 SimpleRuleSetNameMapper mapper = new SimpleRuleSetNameMapper(ruleset); 92 stress(jdk, factory.createRuleSet(mapper.getRuleSets()), files, results, debug); 93 } else { 94 Iterator i = factory.getRegisteredRuleSets(); 95 while (i.hasNext()) { 96 stress(jdk, (RuleSet) i.next(), files, results, debug); 97 } 98 } 99 System.out.println("========================================================="); 100 System.out.println("Rule\t\t\t\t\t\tTime in ms"); 101 System.out.println("========================================================="); 102 for (Iterator j = results.iterator(); j.hasNext();) { 103 Result result = (Result) j.next(); 104 StringBuffer out = new StringBuffer(result.rule.getName()); 105 while (out.length() < 48) { 106 out.append(' '); 107 } 108 out.append(result.time); 109 System.out.println(out.toString()); 110 } 111 } 112 113 System.out.println("========================================================="); 114 } 115 116 private static void parseStress(TargetJDKVersion jdk, List files) throws FileNotFoundException { 117 long start = System.currentTimeMillis(); 118 for (Iterator k = files.iterator(); k.hasNext();) { 119 File file = (File) k.next(); 120 JavaParser parser = jdk.createParser(new FileReader(file)); 121 parser.CompilationUnit(); 122 } 123 long end = System.currentTimeMillis(); 124 long elapsed = end - start; 125 System.out.println("That took " + elapsed + " ms"); 126 } 127 128 private static void stress(TargetJDKVersion jdk, RuleSet ruleSet, List files, Set results, boolean debug) throws PMDException, IOException { 129 Collection rules = ruleSet.getRules(); 130 for (Iterator j = rules.iterator(); j.hasNext();) { 131 Rule rule = (Rule) j.next(); 132 if (debug) System.out.println("Starting " + rule.getName()); 133 134 RuleSet working = new RuleSet(); 135 working.addRule(rule); 136 137 PMD p = new PMD(jdk); 138 RuleContext ctx = new RuleContext(); 139 long start = System.currentTimeMillis(); 140 for (Iterator k = files.iterator(); k.hasNext();) { 141 File file = (File) k.next(); 142 FileReader reader = new FileReader(file); 143 ctx.setSourceCodeFilename(file.getName()); 144 p.processFile(reader, working, ctx); 145 reader.close(); 146 } 147 long end = System.currentTimeMillis(); 148 long elapsed = end - start; 149 results.add(new Result(elapsed, rule)); 150 if (debug) System.out.println("Done timing " + rule.getName() + "; elapsed time was " + elapsed); 151 } 152 } 153 }