1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.perfidix.output;
28
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertTrue;
31
32 import java.io.ByteArrayOutputStream;
33 import java.io.IOException;
34 import java.io.PrintStream;
35 import java.lang.reflect.Method;
36
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.perfidix.annotation.Bench;
41 import org.perfidix.exceptions.AbstractPerfidixMethodException;
42 import org.perfidix.exceptions.PerfidixMethodInvocationException;
43 import org.perfidix.meter.AbstractMeter;
44 import org.perfidix.meter.CountingMeter;
45 import org.perfidix.ouput.TabularSummaryOutput;
46 import org.perfidix.result.BenchmarkResult;
47 import org.perfidix.result.MethodResult;
48
49
50
51
52
53
54 public class TabularSummaryOutputTest {
55
56 private final static String CLASSSTRING = "Class: Class1#method1\n";
57 private final static String METERSTRING = "Meter: Meter1\n";
58
59 private final static int NUMBEROFTICKS = 10;
60
61 private transient BenchmarkResult benchRes;
62
63 private transient PrintStream consoleOut;
64
65 private transient ByteArrayOutputStream bytes;
66
67 private transient AbstractPerfidixMethodException testException;
68
69
70
71
72
73
74
75
76
77 @Before
78 public void setUp() throws SecurityException, NoSuchMethodException {
79 benchRes = new BenchmarkResult();
80
81 final Class<?> class1 = Class1.class;
82
83 final Method meth11 = class1.getDeclaredMethod("method1");
84
85 final CountingMeter meter = new CountingMeter("Meter1");
86
87 for (int i = 0; i < NUMBEROFTICKS; i++) {
88 meter.tick();
89 benchRes.addData(meth11, meter, meter.getValue());
90 }
91
92 testException =
93 new PerfidixMethodInvocationException(new IOException(), new Class1().getClass()
94 .getDeclaredMethod("method1"), Bench.class);
95
96 benchRes.addException(testException);
97 consoleOut = System.out;
98 bytes = new ByteArrayOutputStream();
99 System.setOut(new PrintStream(bytes));
100 }
101
102
103
104
105
106
107 @After
108 public void tearDown() throws Exception {
109 System.setOut(consoleOut);
110 }
111
112
113
114
115
116 @Test
117 public final void testVisitBenchmark() {
118 final TabularSummaryOutput output = new TabularSummaryOutput();
119 output.visitBenchmark(benchRes);
120 final StringBuilder builder = new StringBuilder();
121 builder
122 .append("|= Benchmark ======================================================================|\n");
123 builder
124 .append("| - | unit | sum | min | max | avg | stddev | conf95 | runs |\n");
125 builder
126 .append("|===================================== Meter1 =====================================|\n");
127 builder
128 .append("|. Class1 .........................................................................|\n");
129 builder
130 .append("| method1 | ticks | 55.00 | 01.00 | 10.00 | 05.50 | 03.03 | [01.00-10.00] | 10.00 |\n");
131 builder
132 .append("|_ Summary for Class1 _____________________________________________________________|\n");
133 builder
134 .append("| | ticks | 55.00 | 01.00 | 10.00 | 05.50 | 03.03 | [01.00-10.00] | 10.00 |\n");
135 builder
136 .append("|----------------------------------------------------------------------------------|\n");
137 builder
138 .append("|======================== Summary for the whole benchmark =========================|\n");
139 builder
140 .append("| | ticks | 55.00 | 01.00 | 10.00 | 05.50 | 03.03 | [01.00-10.00] | 10.00 |\n");
141 builder
142 .append("|=================================== Exceptions ===================================|\n");
143 builder
144 .append("| Related exception: IOException |\n");
145 builder
146 .append("| Related place: method invocation |\n");
147 builder
148 .append("| Related method: method1 |\n");
149 builder
150 .append("| Related annotation: Bench |\n");
151 builder
152 .append("|----------------------------------------------------------------------------------|\n");
153 builder
154 .append("|==================================================================================|\n");
155 final String result = bytes.toString();
156 assertTrue("Complete Output check", result.startsWith(builder.toString()));
157 }
158
159
160
161
162
163
164
165
166 @Test
167 public final void testListenToResultSet() throws IOException {
168
169 final MethodResult methRes =
170 benchRes.getIncludedResults().iterator().next().getIncludedResults().iterator().next();
171 final AbstractMeter meter = methRes.getRegisteredMeters().iterator().next();
172 final TabularSummaryOutput output = new TabularSummaryOutput();
173 for (final double d : methRes.getResultSet(meter)) {
174 output.listenToResultSet((Method)methRes.getRelatedElement(), meter, d);
175 }
176 final StringBuilder builder = new StringBuilder();
177
178 builder.append(CLASSSTRING);
179 builder.append(METERSTRING);
180 builder.append("Data: 1.0\n");
181 builder.append("\n");
182 builder.append(CLASSSTRING);
183 builder.append(METERSTRING);
184 builder.append("Data: 2.0\n");
185 builder.append("\n");
186 builder.append(CLASSSTRING);
187 builder.append(METERSTRING);
188 builder.append("Data: 3.0\n");
189 builder.append("\n");
190 builder.append(CLASSSTRING);
191 builder.append(METERSTRING);
192 builder.append("Data: 4.0\n");
193 builder.append("\n");
194 builder.append(CLASSSTRING);
195 builder.append(METERSTRING);
196 builder.append("Data: 5.0\n");
197 builder.append("\n");
198 builder.append(CLASSSTRING);
199 builder.append(METERSTRING);
200 builder.append("Data: 6.0\n");
201 builder.append("\n");
202 builder.append(CLASSSTRING);
203 builder.append(METERSTRING);
204 builder.append("Data: 7.0\n");
205 builder.append("\n");
206 builder.append(CLASSSTRING);
207 builder.append(METERSTRING);
208 builder.append("Data: 8.0\n");
209 builder.append("\n");
210 builder.append(CLASSSTRING);
211 builder.append(METERSTRING);
212 builder.append("Data: 9.0\n");
213 builder.append("\n");
214 builder.append(CLASSSTRING);
215 builder.append(METERSTRING);
216 builder.append("Data: 10.0\n");
217 builder.append("\n");
218
219 assertEquals("Complete listener test", builder.toString(), bytes.toString());
220 }
221
222
223
224
225
226
227 @Test
228 public final void testListenToException() {
229 final TabularSummaryOutput output = new TabularSummaryOutput();
230 output.listenToException(testException);
231
232 final StringBuilder builder = new StringBuilder();
233 builder.append("Class: Class1#method1\n");
234 builder.append("Annotation: Bench\n");
235 builder.append("Exception: PerfidixMethodInvocationException/java.io.IOException\n");
236 builder.append("java.io.IOException\n");
237 assertTrue("Exception listener test", bytes.toString().startsWith(builder.toString()));
238 }
239
240 class Class1 {
241 public void method1() {
242
243 }
244
245 }
246
247 }