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;
28  
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertTrue;
31  
32  import java.util.Map;
33  
34  import org.junit.After;
35  import org.junit.Before;
36  import org.junit.Test;
37  import org.perfidix.AbstractConfig.StandardConfig;
38  import org.perfidix.annotation.BeforeBenchClass;
39  import org.perfidix.benchmarktests.BeforeBenchClassError;
40  import org.perfidix.benchmarktests.NormalBenchForClassAndObjectAdd;
41  import org.perfidix.benchmarktests.NormalCompleteBench;
42  import org.perfidix.benchmarktests.NormalIncompleteBench;
43  import org.perfidix.element.BenchmarkMethod;
44  import org.perfidix.exceptions.AbstractPerfidixMethodException;
45  import org.perfidix.result.BenchmarkResult;
46  
47  /**
48   * Complete test for a normal Benchmark.
49   * 
50   * @author Sebastian Graf, University of Konstanz
51   */
52  public final class BenchmarkTest {
53  
54      private transient Benchmark benchmark;
55  
56      /**
57       * Simple setUp.
58       * 
59       * @throws java.lang.Exception
60       */
61      @Before
62      public void setUp() throws Exception {
63          benchmark = new Benchmark(new StandardConfig());
64      }
65  
66      /**
67       * Simple tearDown.
68       * 
69       * @throws java.lang.Exception
70       */
71      @After
72      public void tearDown() throws Exception {
73          NormalCompleteBench.reset();
74      }
75  
76      /**
77       * Test method for {@link org.perfidix.Benchmark#run()} .
78       */
79      @Test
80      public void testRunBeforeClassError() {
81          benchmark.add(BeforeBenchClassError.class);
82          final BenchmarkResult benchRes = benchmark.run();
83          assertEquals("Meters should be empty", 0, benchRes.getRegisteredMeters().size());
84          assertEquals("One Exception should be registered", 1, benchRes.getExceptions().size());
85          final AbstractPerfidixMethodException exec = benchRes.getExceptions().iterator().next();
86          assertEquals("The related Anno should be BeforeBenchClass", BeforeBenchClass.class, exec
87              .getRelatedAnno());
88          assertEquals("The related Exception should be an IllegalStateException", IllegalStateException.class,
89              exec.getExec().getClass());
90  
91      }
92  
93      /**
94       * Test method for {@link org.perfidix.Benchmark#run()} .
95       */
96      @Test
97      public void testNormalBenchrun() {
98          benchmark.add(NormalCompleteBench.class);
99          final Map<BenchmarkMethod, Integer> mapping = benchmark.getNumberOfMethodsAndRuns();
100         assertEquals("The mapping of methods and runs should be 2", 2, mapping.size());
101         assertTrue("The mapping contains the number of estimated runs", mapping.values().contains(
102             NormalCompleteBench.RUNS));
103         final BenchmarkResult benchRes = benchmark.run();
104         assertEquals("Only one meter is registered", 1, benchRes.getRegisteredMeters().size());
105         assertEquals("No expcetion was thrown", 0, benchRes.getExceptions().size());
106 
107         assertEquals("The BeforeClass-method was invoked once", 1, NormalCompleteBench
108             .getBeforeClassCounter());
109         assertEquals("The BeforeFirst-Run was invoked twice", 2, NormalCompleteBench
110             .getBeforeFirstRunCounter());
111         assertEquals("The number of runs should be equal to the before-each invocations",
112             NormalCompleteBench.RUNS + new StandardConfig().getRuns(), NormalCompleteBench
113                 .getBeforeEachRunCounter());
114         assertEquals("The number of runs should be equal to the bench1 invocations",
115             NormalCompleteBench.RUNS, NormalCompleteBench.getBenchCounter1());
116         assertEquals("The number of runs should be equal to the bench2 invocations", new StandardConfig()
117             .getRuns(), NormalCompleteBench.getBenchCounter2());
118         assertEquals("The number of runs should be equal to the after-each invocations",
119             NormalCompleteBench.RUNS + new StandardConfig().getRuns(), NormalCompleteBench
120                 .getAfterEachRunCounter());
121         assertEquals("The AfterLast-Run was invoked twice", 2, NormalCompleteBench.getAfterLastRunCounter());
122         assertEquals("The AfterClass-method was invoked once", 1, NormalCompleteBench.getAfterClassCounter());
123     }
124 
125     /**
126      * Test method for {@link org.perfidix.Benchmark#run()} .
127      */
128     @Test
129     public void testIncompleteBenchrun() {
130         benchmark.add(NormalIncompleteBench.class);
131         final BenchmarkResult benchRes = benchmark.run();
132         assertEquals("No Meter is given", 0, benchRes.getRegisteredMeters().size());
133         assertEquals("No Exception is thrown", 0, benchRes.getExceptions().size());
134     }
135 
136     /**
137      * Test method for {@link org.perfidix.Benchmark#add(Class)} .
138      */
139     @Test
140     public void testAddClazz() {
141         benchmark.add(NormalCompleteBench.class);
142         final BenchmarkResult benchRes = benchmark.run();
143         assertEquals("One meter is registered", 1, benchRes.getRegisteredMeters().size());
144         assertEquals("No exception is thrown", 0, benchRes.getExceptions().size());
145 
146         assertEquals("Before-Class is invoked once", 1, NormalCompleteBench.getBeforeClassCounter());
147         assertEquals("Before-First is invoked twice", 2, NormalCompleteBench.getBeforeFirstRunCounter());
148         assertEquals("Before-Each is invoked as much as bench", NormalCompleteBench.RUNS
149             + new StandardConfig().getRuns(), NormalCompleteBench.getBeforeEachRunCounter());
150         assertEquals("Bench is invoked as much as bench1", NormalCompleteBench.RUNS, NormalCompleteBench
151             .getBenchCounter1());
152         assertEquals("Bench is invoked as much as bench2", new StandardConfig().getRuns(),
153             NormalCompleteBench.getBenchCounter2());
154         assertEquals("After-Each is invoked as much as bench", NormalCompleteBench.RUNS
155             + new StandardConfig().getRuns(), NormalCompleteBench.getAfterEachRunCounter());
156         assertEquals("After-Last is invoked once", 2, NormalCompleteBench.getAfterLastRunCounter());
157         assertEquals("After-Class is invoked once", 1, NormalCompleteBench.getAfterClassCounter());
158     }
159 
160     /**
161      * Test method for {@link org.perfidix.Benchmark#add(Object)} .
162      */
163     @Test
164     public void testAddObject() {
165         final NormalCompleteBench obj = new NormalCompleteBench();
166         benchmark.add(obj);
167         final BenchmarkResult benchRes = benchmark.run();
168         assertEquals("One meter is registered", 1, benchRes.getRegisteredMeters().size());
169         assertEquals("No exception is thrown", 0, benchRes.getExceptions().size());
170 
171         assertEquals("Before-Class is invoked once", 1, NormalCompleteBench.getBeforeClassCounter());
172         assertEquals("Before-First is invoked twice", 2, NormalCompleteBench.getBeforeFirstRunCounter());
173         assertEquals("Before-Each is invoked as much as bench", NormalCompleteBench.RUNS
174             + new StandardConfig().getRuns(), NormalCompleteBench.getBeforeEachRunCounter());
175         assertEquals("Bench is invoked as much as bench1", NormalCompleteBench.RUNS, NormalCompleteBench
176             .getBenchCounter1());
177         assertEquals("Bench is invoked as much as bench2", new StandardConfig().getRuns(),
178             NormalCompleteBench.getBenchCounter2());
179         assertEquals("After-Each is invoked as much as bench", NormalCompleteBench.RUNS
180             + new StandardConfig().getRuns(), NormalCompleteBench.getAfterEachRunCounter());
181         assertEquals("After-Last is invoked twice", 2, NormalCompleteBench.getAfterLastRunCounter());
182         assertEquals("After-Class is invoked once", 1, NormalCompleteBench.getAfterClassCounter());
183     }
184 
185     /**
186      * Test method for {@link org.perfidix.Benchmark#add(Object)} .
187      */
188     @Test(expected = IllegalArgumentException.class)
189     public void testDuplicateObject() {
190         final NormalCompleteBench obj1 = new NormalCompleteBench();
191         benchmark.add(obj1);
192         final NormalCompleteBench obj2 = new NormalCompleteBench();
193         benchmark.add(obj2);
194     }
195 
196     /**
197      * Test method for {@link org.perfidix.Benchmark#add(Object)} and
198      * {@link org.perfidix.Benchmark#add(Class)}.
199      */
200     @Test(expected = IllegalArgumentException.class)
201     public void testAddObjectAndClass() {
202         final NormalCompleteBench obj = new NormalCompleteBench();
203         benchmark.add(obj);
204         benchmark.add(NormalCompleteBench.class);
205     }
206 
207     /**
208      * Test method for {@link org.perfidix.Benchmark#add(Object)} and
209      * {@link org.perfidix.Benchmark#add(Class)}.
210      */
211     @Test(expected = IllegalArgumentException.class)
212     public void testAddObjectAndClassWithoutBefore() {
213         final NormalBenchForClassAndObjectAdd obj = new NormalBenchForClassAndObjectAdd();
214         benchmark.add(obj);
215         benchmark.add(NormalBenchForClassAndObjectAdd.class);
216     }
217 
218 }