Clover coverage report - PMD - 3.9 Coverage timestamp: Tue Dec 19 2006 09:38:44 EST FRAMES NO FRAMES |
file stats: | LOC: | 172 |  | Methods: | 7
| NCLOC: | 154 |  | Classes: | 2
Source file | Conditionals | Statements | Methods | TOTAL
Benchmark.java | 0% | 0% | 0% | 0% |

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