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