View Javadoc

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