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.result;
28  
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertTrue;
31  
32  import java.io.IOException;
33  import java.lang.reflect.Method;
34  
35  import org.junit.Before;
36  import org.junit.Test;
37  import org.perfidix.annotation.Bench;
38  import org.perfidix.exceptions.PerfidixMethodInvocationException;
39  import org.perfidix.meter.CountingMeter;
40  
41  /**
42   * Test class for the whole package of the results.
43   * 
44   * @author Sebastian Graf, University of Konstanz
45   */
46  public class ResultContainerTest {
47  
48      private final static int NUMBEROFTICKS = 10;
49      private final static int TICKFACTOR = 2;
50  
51      private transient BenchmarkResult benchRes;
52  
53      private transient ClassResult classRes1;
54      private transient ClassResult classRes2;
55  
56      private transient MethodResult methodRes11;
57      private transient MethodResult methodRes12;
58  
59      private transient MethodResult methodRes21;
60      private transient MethodResult methodRes22;
61  
62      private transient CountingMeter meter;
63  
64      private transient Exception testException;
65  
66      /**
67       * Simple setUp.
68       * 
69       * @throws java.lang.Exception
70       */
71      @Before
72      public void setUp() throws Exception {
73  
74          benchRes = new BenchmarkResult();
75  
76          testException = new IOException();
77  
78          final Class<?> class1 = Class1.class;
79          final Class<?> class2 = Class2.class;
80  
81          final Method meth11 = class1.getDeclaredMethod("method1");
82  
83          meter = new CountingMeter("Meter1");
84  
85          for (int i = 0; i < NUMBEROFTICKS; i++) {
86              meter.tick();
87              benchRes.addData(meth11, meter, meter.getValue());
88          }
89  
90          final Method meth12 = class1.getDeclaredMethod("method2");
91  
92          for (int i = 0; i < NUMBEROFTICKS * TICKFACTOR; i++) {
93              meter.tick();
94              benchRes.addData(meth12, meter, meter.getValue());
95          }
96  
97          final Method meth21 = class2.getDeclaredMethod("method1");
98  
99          for (int i = 0; i < NUMBEROFTICKS * TICKFACTOR * TICKFACTOR; i++) {
100             meter.tick();
101             benchRes.addData(meth21, meter, meter.getValue());
102         }
103 
104         final Method meth22 = class2.getDeclaredMethod("method2");
105         for (int i = 0; i < NUMBEROFTICKS * TICKFACTOR * TICKFACTOR * TICKFACTOR; i++) {
106             meter.tick();
107             benchRes.addData(meth22, meter, meter.getValue());
108         }
109 
110         classRes1 = benchRes.getResultForObject(class1);
111         classRes2 = benchRes.getResultForObject(class2);
112 
113         methodRes11 = classRes1.getResultForObject(meth11);
114         methodRes12 = classRes1.getResultForObject(meth12);
115 
116         methodRes21 = classRes2.getResultForObject(meth21);
117         methodRes22 = classRes2.getResultForObject(meth22);
118 
119         benchRes.addException(new PerfidixMethodInvocationException(testException, meth11, Bench.class));
120 
121     }
122 
123     /**
124      * Test method for
125      * {@link org.perfidix.result.BenchmarkResult#addException(org.perfidix.exceptions.AbstractPerfidixMethodException)}
126      * and {@link org.perfidix.result.BenchmarkResult#getExceptions()} .
127      */
128     @Test
129     public void testResultWithException() {
130         assertTrue("Check if benchRes.exceptions contains the desired exception", benchRes.getExceptions()
131             .contains(
132                 new PerfidixMethodInvocationException(testException, (Method)methodRes11.getRelatedElement(),
133                     Bench.class)));
134     }
135 
136     /**
137      * Test method1 for {@link org.perfidix.result.MethodResult} .
138      */
139     @Test
140     public void testMethodRes11() {
141 
142         assertEquals("Mean should be the same as given by method11", 5.5, methodRes11.mean(meter), 0);
143         assertEquals("Min should be the same as given by method11", 1.0, methodRes11.min(meter), 0);
144         assertEquals("Max should be the same as given by method11", 10.0, methodRes11.max(meter), 0);
145         assertEquals("Conf05 should be the same as given by method11", 1.0, methodRes11.getConf05(meter), 0);
146         assertEquals("Conf95 should be the same as given by method11", 10.0, methodRes11.getConf95(meter),
147             0.00001);
148         assertEquals("Stdev should be the same as given by method11", 3.0276503540974917, methodRes11
149             .getStandardDeviation(meter), 0.000001);
150         assertEquals("Sum should be the same as given by method11", 55.0, methodRes11.sum(meter), 0);
151         assertEquals("SquareSum should be the same as given by method11", 385.0,
152             methodRes11.squareSum(meter), 0);
153         assertEquals("Number of result should be the same as given by method11", 10, methodRes11
154             .getNumberOfResult(meter));
155 
156     }
157 
158     /**
159      * Test method2 for {@link org.perfidix.result.MethodResult} .
160      */
161     @Test
162     public void testMethodRes12() {
163         assertEquals("Mean should be the same as given by method12", 20.5, methodRes12.mean(meter), 0);
164         assertEquals("Min should be the same as given by method12", 11.0, methodRes12.min(meter), 0);
165         assertEquals("Max should be the same as given by method12", 30.0, methodRes12.max(meter), 0);
166         assertEquals("Conf05 should be the same as given by method12", 11.05, methodRes12.getConf05(meter), 0);
167         assertEquals("Con95 should be the same as given by method12", 29.95, methodRes12.getConf95(meter),
168             0.00001);
169         assertEquals("Stdev should be the same as given by method12", 5.916079783099616, methodRes12
170             .getStandardDeviation(meter), 0.000001);
171         assertEquals("Sum should be the same as given by method12", 410.0, methodRes12.sum(meter), 0);
172         assertEquals("SquareSum should be the same as given by method12", 9070.0, methodRes12
173             .squareSum(meter), 0);
174         assertEquals("Number of results should be the same as given by method12", 20, methodRes12
175             .getNumberOfResult(meter));
176 
177     }
178 
179     /**
180      * Test method3 for {@link org.perfidix.result.MethodResult} .
181      */
182     @Test
183     public void testMethodRes21() {
184         assertEquals("Mean should be the same as given by method21", 50.5, methodRes21.mean(meter), 0);
185         assertEquals("Min should be the same as given by method21", 31.0, methodRes21.min(meter), 0);
186         assertEquals("Max should be the same as given by method21", 70.0, methodRes21.max(meter), 0);
187         assertEquals("Conf05 should be the same as given by method21", 32.05, methodRes21.getConf05(meter), 0);
188         assertEquals("Conf95 should be the same as given by method21", 68.95, methodRes21.getConf95(meter), 0);
189         assertEquals("Stdev should be the same as given by method21", 11.69045194450012, methodRes21
190             .getStandardDeviation(meter), 0.000001);
191         assertEquals("Sum should be the same as given by method21", 2020.0, methodRes21.sum(meter), 0);
192         assertEquals("Squaresum should be the same as given by method21", 107340.0, methodRes21
193             .squareSum(meter), 0);
194         assertEquals("Number of results should be the same as given by method21", 40, methodRes21
195             .getNumberOfResult(meter));
196 
197     }
198 
199     /**
200      * Test method3 for {@link org.perfidix.result.MethodResult} .
201      */
202     @Test
203     public void testMethodRes22() {
204         assertEquals("Mean should be the same as given by method22", 110.5, methodRes22.mean(meter), 0);
205         assertEquals("Min should be the same as given by method22", 71.0, methodRes22.min(meter), 0);
206         assertEquals("Max should be the same as given by method22", 150.0, methodRes22.max(meter), 0);
207         assertEquals("Conf05 should be the same as given by method22", 74.05, methodRes22.getConf05(meter), 0);
208         assertEquals("Conf95 should be the same as given by method22", 146.95, methodRes22.getConf95(meter),
209             0.00001);
210         assertEquals("Stdev should be the same as given by method22", 23.2379000772445, methodRes22
211             .getStandardDeviation(meter), 0.000001);
212         assertEquals("Sum should be the same as given by method22", 8840.0, methodRes22.sum(meter), 0);
213         assertEquals("SquareSum should be the same as given by method22", 1019480.0, methodRes22
214             .squareSum(meter), 0);
215         assertEquals("Number of runs should be the same as given by method22", 80, methodRes22
216             .getNumberOfResult(meter));
217     }
218 
219     /**
220      * Test method for {@link org.perfidix.result.ClassResult} .
221      */
222     @Test
223     public void testClassResults() {
224         assertEquals("Mean should be the same as given by class1", 15.5, classRes1.mean(meter), 0);
225         assertEquals("Min should be the same as given by class1", 1.0, classRes1.min(meter), 0);
226         assertEquals("Max should be the same as given by class1", 30.0, classRes1.max(meter), 0);
227         assertEquals("Conf05 should be the same as given by class1", 1.55, classRes1.getConf05(meter), 0);
228         assertEquals("Conf95 should be the same as given by class1", 29.45, classRes1.getConf95(meter), 0);
229         assertEquals("Stdev should be the same as given by class1", 8.803408430829505, classRes1
230             .getStandardDeviation(meter), 0.000001);
231         assertEquals("Sum should be the same as given by class1", 465.0, classRes1.sum(meter), 0);
232         assertEquals("SquareSum should be the same as given by class1", 9455.0, classRes1.squareSum(meter), 0);
233         assertEquals("Number of runs should be the same as given by class1", 30, classRes1
234             .getNumberOfResult(meter), 0);
235 
236         assertEquals("Mean should be the same as given by class2", 90.5, classRes2.mean(meter), 0);
237         assertEquals("Min should be the same as given by class2", 31.0, classRes2.min(meter), 0);
238         assertEquals("Max should be the same as given by class2", 150.0, classRes2.max(meter), 0);
239         assertEquals("Conf05 should be the same as given by class2", 36.05, classRes2.getConf05(meter), 0);
240         assertEquals("Conf95 should be the same as given by class2", 144.95, classRes2.getConf95(meter), 0);
241         assertEquals("Stdev should be the same as given by class2", 34.785054261852174, classRes2
242             .getStandardDeviation(meter), 0.000001);
243         assertEquals("Sum should be the same as given by class2", 10860.0, classRes2.sum(meter), 0);
244         assertEquals("SquareSum should be the same as given by class2", 1126820.0,
245             classRes2.squareSum(meter), 0);
246         assertEquals("Number of runs should be the same as given by class2", 120, classRes2
247             .getNumberOfResult(meter), 0);
248     }
249 
250     /**
251      * Test method for {@link org.perfidix.result.BenchmarkResult} .
252      */
253     @Test
254     public void testBenchmarkResults() {
255 
256         assertEquals("Mean should be the same as given by benchmark", 75.5, benchRes.mean(meter), 0);
257         assertEquals("Min should be the same as given by benchmark", 1.0, benchRes.min(meter), 0);
258         assertEquals("Max should be the same as given by benchmark", 150.0, benchRes.max(meter), 0);
259         assertEquals("Conf05 should be the same as given by benchmark", 7.55, benchRes.getConf05(meter), 0);
260         assertEquals("Conf95 should be the same as given by benchmark", 143.45, benchRes.getConf95(meter), 0);
261         assertEquals("Stdev should be the same as given by benchmark", 43.445367992456916, benchRes
262             .getStandardDeviation(meter), 0.000001);
263         assertEquals("Sum should be the same as given by benchmark", 11325.0, benchRes.sum(meter), 0);
264         assertEquals("SquareSum should be the same as given by benchmark", 1136275.0, benchRes
265             .squareSum(meter), 0);
266         assertEquals("Number of runs should be the same as given by benchmark", 150, benchRes
267             .getNumberOfResult(meter));
268 
269     }
270 
271     class Class1 {
272         public void method1() {
273             // empty method for class1#method1 invocation
274         }
275 
276         public void method2() {
277             // empty method for class1#method2 invocation
278         }
279     }
280 
281     class Class2 {
282         public void method1() {
283             // empty method for class2#method1 invocation
284         }
285 
286         public void method2() {
287             // empty method for class2#method2 invocation
288         }
289     }
290 
291 }