[maven-invoker-plugin] branch master updated: [MINVOKER-251] Allow options parallelThreads depends on available cores (#7)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[maven-invoker-plugin] branch master updated: [MINVOKER-251] Allow options parallelThreads depends on available cores (#7)

olamy
This is an automated email from the ASF dual-hosted git repository.

olamy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-invoker-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 0d7170c  [MINVOKER-251] Allow options parallelThreads depends on available cores (#7)
0d7170c is described below

commit 0d7170cda3ea1c3ab17fa5be80858ca70f71d3fc
Author: Slawomir Jaranowski <[hidden email]>
AuthorDate: Wed Nov 6 04:24:25 2019 +0100

    [MINVOKER-251] Allow options parallelThreads depends on available cores (#7)
   
    * [MINVOKER-251] Allow options parallelThreads depends on available cores
   
    * fix checkstyle in test class
---
 .../maven/plugins/invoker/AbstractInvokerMojo.java | 29 ++++++++++++++++------
 .../maven/plugins/invoker/InvokerMojoTest.java     | 22 ++++++++++++++++
 2 files changed, 43 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java b/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java
index 45eaf1a..17ec887 100644
--- a/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java
+++ b/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java
@@ -589,12 +589,16 @@ public abstract class AbstractInvokerMojo
     private boolean showVersion;
 
     /**
-     * number of threads for running tests in parallel. This will be the number of maven forked process in parallel.
+     * <p>Number of threads for running tests in parallel. This will be the number of maven forked process in parallel.
+     * When terminated with "C", the number part is multiplied by the number of processors (cores) available
+     * to the Java virtual machine. Floating point value are only accepted together with "C".</p>
+     *
+     * <p>Example values: "1.5C", "4"</p>
      *
      * @since 1.6
      */
     @Parameter( property = "invoker.parallelThreads", defaultValue = "1" )
-    private int parallelThreads;
+    private String parallelThreads;
 
     /**
      * @since 1.6
@@ -788,7 +792,7 @@ public abstract class AbstractInvokerMojo
         List<BuildJob> nonSetupBuildJobs = getNonSetupJobs( buildJobs );
         // We will run the non setup jobs with the configured
         // parallelThreads number.
-        runBuilds( projectsDir, nonSetupBuildJobs, parallelThreads );
+        runBuilds( projectsDir, nonSetupBuildJobs, getParallelThreadsCount() );
 
         writeSummaryFile( nonSetupBuildJobs );
 
@@ -2143,6 +2147,20 @@ public abstract class AbstractInvokerMojo
         return executionResult;
     }
 
+    int getParallelThreadsCount()
+    {
+        if ( parallelThreads.endsWith( "C" ) )
+        {
+            double parallelThreadsMultiple = Double.parseDouble(
+                    parallelThreads.substring( 0, parallelThreads.length() - 1 ) );
+            return (int) ( parallelThreadsMultiple * Runtime.getRuntime().availableProcessors() );
+        }
+        else
+        {
+            return Integer.parseInt( parallelThreads );
+        }
+    }
+
     private static class ExecutionResult
     {
         boolean executed;
@@ -2840,11 +2858,6 @@ public abstract class AbstractInvokerMojo
         return new InvokerProperties( props );
     }
 
-    protected boolean isParallelRun()
-    {
-        return parallelThreads > 1;
-    }
-
     static class ToolchainPrivateManager
     {
         private ToolchainManagerPrivate manager;
diff --git a/src/test/java/org/apache/maven/plugins/invoker/InvokerMojoTest.java b/src/test/java/org/apache/maven/plugins/invoker/InvokerMojoTest.java
index 5d51c40..41034ee 100644
--- a/src/test/java/org/apache/maven/plugins/invoker/InvokerMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/invoker/InvokerMojoTest.java
@@ -101,4 +101,26 @@ public class InvokerMojoTest
         assertFalse( AbstractInvokerMojo.alreadyCloned( "dirs", Collections.singletonList( "dir" ) ) );
     }
 
+    public void testParallelThreadsSettings()
+            throws IllegalAccessException
+    {
+        Object[][] testValues = {
+                {"4", 4},
+                {"1C", Runtime.getRuntime().availableProcessors()},
+                {"2.5C", (int) ( Double.parseDouble( "2.5" ) * Runtime.getRuntime().availableProcessors() )}
+        };
+
+        InvokerMojo invokerMojo = new InvokerMojo();
+
+        for ( Object[] testValue: testValues )
+        {
+            String parallelThreads = (String) testValue[0];
+            int expectedParallelThreads = (Integer) testValue[1];
+
+            setVariableValueToObject( invokerMojo, "parallelThreads", parallelThreads );
+
+            assertEquals( expectedParallelThreads, invokerMojo.getParallelThreadsCount() );
+        }
+    }
+
 }