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.cpd.FileFinder; |
11 |
| import net.sourceforge.pmd.cpd.JavaLanguage; |
12 |
| |
13 |
| import java.io.File; |
14 |
| import java.io.FileReader; |
15 |
| import java.io.IOException; |
16 |
| import java.util.Iterator; |
17 |
| import java.util.List; |
18 |
| import java.util.Set; |
19 |
| import java.util.TreeSet; |
20 |
| |
21 |
| public class Benchmark { |
22 |
| |
23 |
| private static class Result implements Comparable { |
24 |
| public Rule rule; |
25 |
| public long time; |
26 |
| |
27 |
0
| public int compareTo(Object o) {
|
28 |
0
| Result other = (Result)o;
|
29 |
0
| if (other.time < time) {
|
30 |
0
| return -1;
|
31 |
0
| } else if (other.time > time) {
|
32 |
0
| return 1;
|
33 |
| } |
34 |
0
| return 0;
|
35 |
| } |
36 |
| |
37 |
0
| public Result(long elapsed, Rule rule) {
|
38 |
0
| this.rule = rule;
|
39 |
0
| this.time = elapsed;
|
40 |
| } |
41 |
| } |
42 |
| |
43 |
| private static final String JAVA_SRC_DIR = "/usr/local/java/src/java/lang/"; |
44 |
| private static final boolean DEBUG = false; |
45 |
| |
46 |
0
| public static void main(String[] args) throws RuleSetNotFoundException, IOException, PMDException {
|
47 |
0
| Set results = new TreeSet();
|
48 |
| |
49 |
0
| FileFinder finder = new FileFinder();
|
50 |
0
| List files = finder.findFilesFrom(JAVA_SRC_DIR, new JavaLanguage.JavaFileOrDirectoryFilter(), true);
|
51 |
| |
52 |
0
| RuleSetFactory factory = new RuleSetFactory();
|
53 |
0
| Iterator i = factory.getRegisteredRuleSets();
|
54 |
0
| while (i.hasNext()) {
|
55 |
0
| stress((RuleSet)i.next(), files, results);
|
56 |
| } |
57 |
0
| System.out.println("=========================================================");
|
58 |
0
| System.out.println("Rule\t\t\t\t\t\tTime in ms");
|
59 |
0
| System.out.println("=========================================================");
|
60 |
0
| for (Iterator j = results.iterator(); j.hasNext();) {
|
61 |
0
| Result result = (Result)j.next();
|
62 |
| |
63 |
0
| StringBuffer out = new StringBuffer(result.rule.getName());
|
64 |
0
| while (out.length() < 48) {
|
65 |
0
| out.append(' ');
|
66 |
| } |
67 |
0
| out.append(result.time);
|
68 |
0
| System.out.println(out.toString());
|
69 |
| } |
70 |
0
| System.out.println("=========================================================");
|
71 |
| } |
72 |
| |
73 |
0
| private static void stress(RuleSet ruleSet, List files, Set results) throws PMDException, IOException {
|
74 |
0
| Set rules = ruleSet.getRules();
|
75 |
0
| for (Iterator j = rules.iterator(); j.hasNext();) {
|
76 |
0
| Rule rule = (Rule)j.next();
|
77 |
0
| if (DEBUG) System.out.println("Starting " + rule.getName());
|
78 |
| |
79 |
0
| RuleSet working = new RuleSet();
|
80 |
0
| working.addRule(rule);
|
81 |
| |
82 |
0
| PMD p = new PMD();
|
83 |
0
| RuleContext ctx = new RuleContext();
|
84 |
0
| long start = System.currentTimeMillis();
|
85 |
0
| for (Iterator k = files.iterator(); k.hasNext();) {
|
86 |
0
| File file = (File)k.next();
|
87 |
0
| FileReader reader = new FileReader(file);
|
88 |
0
| ctx.setSourceCodeFilename(file.getName());
|
89 |
0
| p.processFile(reader, working, ctx);
|
90 |
0
| reader.close();
|
91 |
| } |
92 |
0
| long end = System.currentTimeMillis();
|
93 |
0
| long elapsed = end - start;
|
94 |
0
| results.add(new Result(elapsed, rule));
|
95 |
0
| if (DEBUG) System.out.println("Done timing " + rule.getName() + "; elapsed time was " + elapsed);
|
96 |
| } |
97 |
| } |
98 |
| } |