View Javadoc

1   package org.apache.maven.surefire.junitcore.pc;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.surefire.junitcore.JUnitCoreParameters;
23  import org.apache.maven.surefire.testset.TestSetFailedException;
24  import org.junit.AfterClass;
25  import org.junit.BeforeClass;
26  import org.junit.Rule;
27  import org.junit.experimental.theories.DataPoint;
28  import org.junit.experimental.theories.Theories;
29  import org.junit.experimental.theories.Theory;
30  import org.junit.rules.ExpectedException;
31  import org.junit.runner.RunWith;
32  
33  import java.util.Properties;
34  
35  import static org.apache.maven.surefire.junitcore.JUnitCoreParameters.*;
36  import static org.apache.maven.surefire.junitcore.pc.ParallelComputerUtil.*;
37  import static org.hamcrest.core.Is.is;
38  import static org.junit.Assert.*;
39  
40  /**
41   * Testing an algorithm in {@link ParallelComputerUtil} which configures
42   * optimized thread resources in ParallelComputer by given {@link org.apache.maven.surefire.junitcore.JUnitCoreParameters}.
43   *
44   * @author Tibor Digana (tibor17)
45   * @see ParallelComputerUtil
46   * @since 2.17
47   */
48  @RunWith( Theories.class )
49  public final class OptimizedParallelComputerTest
50  {
51      @DataPoint
52      public static final int CPU_1 = 1;
53  
54      @DataPoint
55      public static final int CPU_4 = 4;
56  
57      @Rule
58      public final ExpectedException exception = ExpectedException.none();
59  
60      @Rule
61      public final Stopwatch runtime = new Stopwatch();
62  
63      @BeforeClass
64      public static void beforeClass()
65      {
66          overrideAvailableProcessors( 1 );
67      }
68  
69      @AfterClass
70      public static void afterClass()
71      {
72          setDefaultAvailableProcessors();
73      }
74  
75      @Theory
76      public void threadCountSuites( int cpu )
77          throws TestSetFailedException
78      {
79          overrideAvailableProcessors( cpu );
80          Properties properties = new Properties();
81          properties.setProperty( PARALLEL_KEY, "suites" );
82          properties.setProperty( THREADCOUNT_KEY, "3" );
83          JUnitCoreParameters params = new JUnitCoreParameters( properties );
84          RunnerCounter counter = new RunnerCounter( 5, 10, 20 );
85          Concurrency concurrency = resolveConcurrency( params, counter );
86          assertTrue( params.isParallelSuites() );
87          assertFalse( params.isParallelClasses() );
88          assertFalse( params.isParallelMethods() );
89          assertThat( concurrency.capacity, is( 0 ) );
90          assertThat( concurrency.suites, is( (int) Math.min( 3 * cpu, counter.suites ) ) );
91          assertThat( concurrency.classes, is( 0 ) );
92          assertThat( concurrency.methods, is( 0 ) );
93      }
94  
95      @Theory
96      public void threadCountClasses( int cpu )
97          throws TestSetFailedException
98      {
99          overrideAvailableProcessors( cpu );
100         Properties properties = new Properties();
101         properties.setProperty( PARALLEL_KEY, "classes" );
102         properties.setProperty( THREADCOUNT_KEY, "3" );
103         JUnitCoreParameters params = new JUnitCoreParameters( properties );
104         RunnerCounter counter = new RunnerCounter( 1, 5, 10 );
105         Concurrency concurrency = resolveConcurrency( params, counter );
106         assertFalse( params.isParallelSuites() );
107         assertTrue( params.isParallelClasses() );
108         assertFalse( params.isParallelMethods() );
109         assertThat( concurrency.capacity, is( 0 ) );
110         assertThat( concurrency.suites, is( 0 ) );
111         assertThat( concurrency.classes, is( (int) Math.min( 3 * cpu, counter.classes ) ) );
112         assertThat( concurrency.methods, is( 0 ) );
113     }
114 
115     @Theory
116     public void threadCountMethods( int cpu )
117         throws TestSetFailedException
118     {
119         overrideAvailableProcessors( cpu );
120         Properties properties = new Properties();
121         properties.setProperty( PARALLEL_KEY, "methods" );
122         properties.setProperty( THREADCOUNT_KEY, "3" );
123         JUnitCoreParameters params = new JUnitCoreParameters( properties );
124         RunnerCounter counter = new RunnerCounter( 1, 2, 5 );
125         Concurrency concurrency = resolveConcurrency( params, counter );
126         assertFalse( params.isParallelSuites() );
127         assertFalse( params.isParallelClasses() );
128         assertTrue( params.isParallelMethods() );
129         assertThat( concurrency.capacity, is( 0 ) );
130         assertThat( concurrency.suites, is( 0 ) );
131         assertThat( concurrency.classes, is( 0 ) );
132         assertThat( concurrency.methods, is( (int) Math.min( 3 * cpu, counter.methods ) ) );
133     }
134 
135     @Theory
136     public void threadCountBoth( int cpu )
137         throws TestSetFailedException
138     {
139         overrideAvailableProcessors( cpu );
140         Properties properties = new Properties();
141         properties.setProperty( PARALLEL_KEY, "both" );
142         properties.setProperty( THREADCOUNT_KEY, "3" );
143         JUnitCoreParameters params = new JUnitCoreParameters( properties );
144         RunnerCounter counter = new RunnerCounter( 1, 2, 5 );
145         Concurrency concurrency = resolveConcurrency( params, counter );
146         assertFalse( params.isParallelSuites() );
147         assertTrue( params.isParallelClasses() );
148         assertTrue( params.isParallelMethods() );
149         assertThat( concurrency.capacity, is( 3 * cpu ) );
150         assertThat( concurrency.suites, is( 0 ) );
151         assertThat( concurrency.classes, is( (int) Math.min( ( 3d / 2 ) * cpu, 2 ) ) );
152         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
153     }
154 
155     @Theory
156     public void threadCountClassesAndMethods( int cpu )
157         throws TestSetFailedException
158     {
159         overrideAvailableProcessors( cpu );
160         Properties properties = new Properties();
161         properties.setProperty( PARALLEL_KEY, "classesAndMethods" );
162         properties.setProperty( THREADCOUNT_KEY, "3" );
163         JUnitCoreParameters params = new JUnitCoreParameters( properties );
164         RunnerCounter counter = new RunnerCounter( 1, 2, 5 );
165         Concurrency concurrency = resolveConcurrency( params, counter );
166         assertFalse( params.isParallelSuites() );
167         assertTrue( params.isParallelClasses() );
168         assertTrue( params.isParallelMethods() );
169         assertThat( concurrency.capacity, is( 3 * cpu ) );
170         assertThat( concurrency.suites, is( 0 ) );
171         assertThat( concurrency.classes, is( (int) Math.min( ( 3d / 2 ) * cpu, 2 ) ) );
172         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
173     }
174 
175     @Theory
176     public void threadCountSuitesAndMethods( int cpu )
177         throws TestSetFailedException
178     {
179         overrideAvailableProcessors( cpu );
180         Properties properties = new Properties();
181         properties.setProperty( PARALLEL_KEY, "suitesAndMethods" );
182         properties.setProperty( THREADCOUNT_KEY, "3" );
183         JUnitCoreParameters params = new JUnitCoreParameters( properties );
184         RunnerCounter counter = new RunnerCounter( 2, 3, 5 );
185         Concurrency concurrency = resolveConcurrency( params, counter );
186         assertTrue( params.isParallelSuites() );
187         assertFalse( params.isParallelClasses() );
188         assertTrue( params.isParallelMethods() );
189         assertThat( concurrency.capacity, is( 3 * cpu ) );
190         assertThat( concurrency.suites, is( (int) Math.min( ( 3d / 2 ) * cpu, 2 ) ) );
191         assertThat( concurrency.classes, is( 0 ) );
192         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
193     }
194 
195     @Theory
196     public void threadCountSuitesAndClasses( int cpu )
197         throws TestSetFailedException
198     {
199         overrideAvailableProcessors( cpu );
200         Properties properties = new Properties();
201         properties.setProperty( PARALLEL_KEY, "suitesAndClasses" );
202         properties.setProperty( THREADCOUNT_KEY, "3" );
203         JUnitCoreParameters params = new JUnitCoreParameters( properties );
204         RunnerCounter counter = new RunnerCounter( 2, 5, 20 );
205         Concurrency concurrency = resolveConcurrency( params, counter );
206         assertTrue( params.isParallelSuites() );
207         assertTrue( params.isParallelClasses() );
208         assertFalse( params.isParallelMethods() );
209         assertThat( concurrency.capacity, is( 3 * cpu ) );
210         assertThat( concurrency.suites, is( (int) Math.min( ( 2d * 3 / 7 ) * cpu, 2 ) ) );
211         assertThat( concurrency.classes, is( Integer.MAX_VALUE ) );
212         assertThat( concurrency.methods, is( 0 ) );
213     }
214 
215     @Theory
216     public void threadCountAll( int cpu )
217         throws TestSetFailedException
218     {
219         overrideAvailableProcessors( cpu );
220         Properties properties = new Properties();
221         properties.setProperty( PARALLEL_KEY, "all" );
222         properties.setProperty( THREADCOUNT_KEY, "3" );
223         JUnitCoreParameters params = new JUnitCoreParameters( properties );
224         RunnerCounter counter = new RunnerCounter( 2, 5, 20 );
225         Concurrency concurrency = resolveConcurrency( params, counter );
226         assertTrue( params.isParallelSuites() );
227         assertTrue( params.isParallelClasses() );
228         assertTrue( params.isParallelMethods() );
229         assertThat( concurrency.capacity, is( 3 * cpu ) );
230         assertThat( concurrency.suites, is( (int) Math.min( ( 2d * 3 / 11 ) * cpu, 2 ) ) );
231         assertThat( concurrency.classes, is( (int) Math.min( ( 5d * 3 / 11 ) * cpu, 5 ) ) );
232         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
233     }
234 
235     @Theory
236     public void reusableThreadCountSuitesAndClasses( int cpu )
237         throws TestSetFailedException
238     {
239         // 4 * cpu to 5 * cpu threads to run test classes
240         overrideAvailableProcessors( cpu );
241         Properties properties = new Properties();
242         properties.setProperty( PARALLEL_KEY, "suitesAndClasses" );
243         properties.setProperty( THREADCOUNT_KEY, "6" );
244         properties.setProperty( THREADCOUNTSUITES_KEY, "2" );
245         JUnitCoreParameters params = new JUnitCoreParameters( properties );
246         RunnerCounter counter = new RunnerCounter( 3, 5, 20 );
247         Concurrency concurrency = resolveConcurrency( params, counter );
248         assertTrue( params.isParallelSuites() );
249         assertTrue( params.isParallelClasses() );
250         assertFalse( params.isParallelMethods() );
251         assertThat( concurrency.capacity, is( 6 * cpu ) );
252         assertThat( concurrency.suites, is( Math.min( 2 * cpu, 3 ) ) );
253         assertThat( concurrency.classes, is( Integer.MAX_VALUE ) );
254         assertThat( concurrency.methods, is( 0 ) );
255     }
256 
257     @Theory
258     public void reusableThreadCountSuitesAndMethods( int cpu )
259         throws TestSetFailedException
260     {
261         // 4 * cpu to 5 * cpu threads to run test methods
262         overrideAvailableProcessors( cpu );
263         Properties properties = new Properties();
264         properties.setProperty( PARALLEL_KEY, "suitesAndMethods" );
265         properties.setProperty( THREADCOUNT_KEY, "6" );
266         properties.setProperty( THREADCOUNTSUITES_KEY, "2" );
267         JUnitCoreParameters params = new JUnitCoreParameters( properties );
268         RunnerCounter counter = new RunnerCounter( 3, 5, 20 );
269         Concurrency concurrency = resolveConcurrency( params, counter );
270         assertTrue( params.isParallelSuites() );
271         assertFalse( params.isParallelClasses() );
272         assertTrue( params.isParallelMethods() );
273         assertThat( concurrency.capacity, is( 6 * cpu ) );
274         assertThat( concurrency.suites, is( Math.min( 2 * cpu, 3 ) ) );
275         assertThat( concurrency.classes, is( 0 ) );
276         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
277     }
278 
279     @Theory
280     public void reusableThreadCountClassesAndMethods( int cpu )
281         throws TestSetFailedException
282     {
283         // 4 * cpu to 5 * cpu threads to run test methods
284         overrideAvailableProcessors( cpu );
285         Properties properties = new Properties();
286         properties.setProperty( PARALLEL_KEY, "classesAndMethods" );
287         properties.setProperty( THREADCOUNT_KEY, "6" );
288         properties.setProperty( THREADCOUNTCLASSES_KEY, "2" );
289         JUnitCoreParameters params = new JUnitCoreParameters( properties );
290         RunnerCounter counter = new RunnerCounter( 3, 5, 20 );
291         Concurrency concurrency = resolveConcurrency( params, counter );
292         assertFalse( params.isParallelSuites() );
293         assertTrue( params.isParallelClasses() );
294         assertTrue( params.isParallelMethods() );
295         assertThat( concurrency.capacity, is( 6 * cpu ) );
296         assertThat( concurrency.suites, is( 0 ) );
297         assertThat( concurrency.classes, is( Math.min( 2 * cpu, 5 ) ) );
298         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
299     }
300 
301     @Theory
302     public void reusableThreadCountAll( int cpu )
303         throws TestSetFailedException
304     {
305         // 8 * cpu to 13 * cpu threads to run test methods
306         overrideAvailableProcessors( cpu );
307         Properties properties = new Properties();
308         properties.setProperty( PARALLEL_KEY, "all" );
309         properties.setProperty( THREADCOUNT_KEY, "14" );
310         properties.setProperty( THREADCOUNTSUITES_KEY, "2" );
311         properties.setProperty( THREADCOUNTCLASSES_KEY, "4" );
312         JUnitCoreParameters params = new JUnitCoreParameters( properties );
313         RunnerCounter counter = new RunnerCounter( 3, 5, 20 );
314         Concurrency concurrency = resolveConcurrency( params, counter );
315         assertTrue( params.isParallelSuites() );
316         assertTrue( params.isParallelClasses() );
317         assertTrue( params.isParallelMethods() );
318         assertThat( concurrency.capacity, is( 14 * cpu ) );
319         assertThat( concurrency.suites, is( Math.min( 2 * cpu, 3 ) ) );
320         assertThat( concurrency.classes, is( Math.min( 4 * cpu, 5 ) ) );
321         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
322     }
323 }