View Javadoc

1   /**
2    * Copyright (c) 2012, University of Konstanz, Distributed Systems Group
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    * * Redistributions of source code must retain the above copyright
8    * notice, this list of conditions and the following disclaimer.
9    * * Redistributions in binary form must reproduce the above copyright
10   * notice, this list of conditions and the following disclaimer in the
11   * documentation and/or other materials provided with the distribution.
12   * * Neither the name of the University of Konstanz nor the
13   * names of its contributors may be used to endorse or promote products
14   * derived from this software without specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Test case for {@link TabularSummaryOutput}
51   * 
52   * @author Sebastian Graf, University of Konstanz
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       * Simple Constructor.
71       * 
72       * @throws NoSuchMethodException
73       *             if declaration fails
74       * @throws SecurityException
75       *             if declaration fails
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      * Simple Constructor.
104      * 
105      * @throws java.lang.Exception
106      */
107     @After
108     public void tearDown() throws Exception {
109         System.setOut(consoleOut);
110     }
111 
112     /**
113      * Test method for
114      * {@link org.perfidix.ouput.TabularSummaryOutput#visitBenchmark(org.perfidix.result.BenchmarkResult)} .
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      * Test method for
161      * {@link org.perfidix.ouput.TabularSummaryOutput#listenToResultSet(java.lang.reflect.Method, org.perfidix.meter.AbstractMeter, double)}
162      * .
163      * 
164      * @throws IOException
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      * Test method for
224      * {@link org.perfidix.ouput.TabularSummaryOutput#listenToException(org.perfidix.exceptions.AbstractPerfidixMethodException)}
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             // Simple skeleton
243         }
244 
245     }
246 
247 }