View Javadoc

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