View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.benchmark;
5   
6   import java.io.PrintStream;
7   import java.text.MessageFormat;
8   import java.util.ArrayList;
9   import java.util.Collections;
10  import java.util.List;
11  import java.util.Map;
12  import java.util.Set;
13  
14  import net.sourceforge.pmd.PMD;
15  import net.sourceforge.pmd.util.StringUtil;
16  
17  /**
18   *
19   *
20   */
21  public class TextReport implements BenchmarkReport {
22  
23  
24  	private static final int TIME_COLUMN		 = 48;
25  	private static final int NAME_COLUMN_WIDTH	 = 50;
26  	private static final int VALUE_COLUMN_WIDTH	 =  8;
27  
28  	public TextReport() {
29  
30  	}
31  
32  	/**
33  	 *
34  	 * @param stressResults Set<Result>
35  	 * @param out PrintStream
36  	 * @see net.sourceforge.pmd.benchmark.BenchmarkReport#generate(Set<Result>, PrintStream)
37  	 */
38  	public void generate(Set<RuleDuration> stressResults, PrintStream out) {
39  
40  		  out.println("=========================================================");
41            out.println("Rule\t\t\t\t\t\tTime in ms");
42            out.println("=========================================================");
43  
44            for (RuleDuration result: stressResults) {
45                StringBuilder buffer = new StringBuilder(result.rule.getName());
46                while (buffer.length() < TIME_COLUMN) {
47              	  buffer.append(' ');
48                }
49                buffer.append(result.time);
50                out.println(out.toString());
51            }
52  
53            out.println("=========================================================");
54  	}
55  
56  	/**
57  	 *
58  	 * @param benchmarksByName Map<String,BenchmarkResult>
59  	 */
60  	public void report(Map<String, BenchmarkResult> benchmarksByName) {
61  		generate(benchmarksByName, System.out);
62  	}
63  
64  	/**
65  	 *
66  	 * @param benchmarksByName Map<String,BenchmarkResult>
67  	 * @param out PrintStream
68  	 * @see net.sourceforge.pmd.benchmark.BenchmarkReport#generate(Map<String,BenchmarkResult>, PrintStream)
69  	 */
70  	public void generate(Map<String, BenchmarkResult> benchmarksByName, PrintStream out) {
71  
72  		List<BenchmarkResult> results = new ArrayList<BenchmarkResult>(benchmarksByName.values());
73  
74  		long[] totalTime = new long[Benchmark.TotalPMD.index + 1];
75  		long[] totalCount = new long[Benchmark.TotalPMD.index + 1];
76  
77  		for (BenchmarkResult benchmarkResult: results) {
78  			totalTime[benchmarkResult.type.index] += benchmarkResult.getTime();
79  			totalCount[benchmarkResult.type.index] += benchmarkResult.getCount();
80  			if (benchmarkResult.type.index < Benchmark.MeasuredTotal.index) {
81  				totalTime[Benchmark.MeasuredTotal.index] += benchmarkResult.getTime();
82  			}
83  		}
84  		results.add(new BenchmarkResult(Benchmark.RuleTotal, 		totalTime[Benchmark.RuleTotal.index], 0));
85  		results.add(new BenchmarkResult(Benchmark.RuleChainTotal, 	totalTime[Benchmark.RuleChainTotal.index], 0));
86  		results.add(new BenchmarkResult(Benchmark.MeasuredTotal, 	totalTime[Benchmark.MeasuredTotal.index], 0));
87  		results.add(new BenchmarkResult(Benchmark.NonMeasuredTotal, totalTime[Benchmark.TotalPMD.index] - totalTime[Benchmark.MeasuredTotal.index], 0));
88  		Collections.sort(results);
89  
90  		StringBuilderCR buf = new StringBuilderCR(PMD.EOL);
91  		boolean writeRuleHeader = true;
92  		boolean writeRuleChainRuleHeader = true;
93  		long ruleCount = 0;
94  		long ruleChainCount = 0;
95  
96  		for (BenchmarkResult benchmarkResult: results) {
97  			StringBuilder buf2 = new StringBuilder(benchmarkResult.name);
98  			buf2.append(':');
99  			while (buf2.length() <= NAME_COLUMN_WIDTH) {
100 				buf2.append(' ');
101 			}
102 			String result = MessageFormat.format("{0,number,0.000}", Double.valueOf(benchmarkResult.getTime()/1000000000.0));
103 			buf2.append(StringUtil.lpad(result, VALUE_COLUMN_WIDTH));
104 			if (benchmarkResult.type.index <= Benchmark.RuleChainRule.index) {
105 				buf2.append(StringUtil.lpad(MessageFormat.format("{0,number,###,###,###,###,###}", benchmarkResult.getCount()), 20));
106 			}
107 			switch (benchmarkResult.type) {
108 			case Rule:
109 				if (writeRuleHeader) {
110 					writeRuleHeader = false;
111 					buf.appendLn();
112 					buf.appendLn("---------------------------------<<< Rules >>>---------------------------------");
113 					buf.appendLn("Rule name                                       Time (secs)    # of Evaluations");
114 					buf.appendLn();
115 				}
116 				ruleCount++;
117 				break;
118 			case RuleChainRule:
119 				if (writeRuleChainRuleHeader) {
120 					writeRuleChainRuleHeader = false;
121 					buf.appendLn();
122 					buf.appendLn("----------------------------<<< RuleChain Rules >>>----------------------------");
123 					buf.appendLn("Rule name                                       Time (secs)         # of Visits");
124 					buf.appendLn();
125 				}
126 				ruleChainCount++;
127 				break;
128 			case CollectFiles:
129 				buf.appendLn();
130 				buf.appendLn("--------------------------------<<< Summary >>>--------------------------------");
131 				buf.appendLn("Segment                                         Time (secs)");
132 				buf.appendLn();
133 				break;
134 			case MeasuredTotal:
135 				String s = MessageFormat.format("{0,number,###,###,###,###,###}", ruleCount);
136 				String t = MessageFormat.format("{0,number,0.000}", ruleCount==0 ? 0 : total(totalTime,Benchmark.Rule,ruleCount));
137 				buf.appendLn("Rule Average (", s, " rules):", StringUtil.lpad(t, 37-s.length()));
138 				s = MessageFormat.format("{0,number,###,###,###,###,###}", ruleChainCount);
139 				t = MessageFormat.format("{0,number,0.000}", ruleChainCount==0 ? 0 : total(totalTime,Benchmark.RuleChainRule, ruleChainCount));
140 				buf.appendLn("RuleChain Average (", s, " rules):", StringUtil.lpad(t, 32-s.length()));
141 
142 				buf.appendLn();
143 				buf.appendLn("-----------------------------<<< Final Summary >>>-----------------------------");
144 				buf.appendLn("Total                                           Time (secs)");
145 				buf.appendLn();
146 				break;
147 			default:
148 				// Do nothing
149 				break;
150 			}
151 			buf.appendLn(buf2.toString());
152 		}
153 
154 		out.print(buf.toString());
155 	}
156 
157 	/**
158 	 *
159 	 * @param timeTotals long[]
160 	 * @param index Benchmark
161 	 * @param count long
162 	 * @return double
163 	 */
164 	private static double total(long[] timeTotals, Benchmark index, long count) {
165 		return timeTotals[index.index]/1000000000.0d/count;
166 	}
167 }