Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ public interface MavenOptions extends Options {
/**
* Returns the number of threads used for parallel builds.
*
* @return an {@link Optional} containing the number of threads (or "1C" for one thread per CPU core), or empty if not specified
* @return an {@link Optional} containing the number of threads (e.g. {@code 4} for an integer,
* {@code 2C} or {@code 2.5C} for a CPU multiplier, or {@code max} for available processors minus 1,
* minimum 1), or empty if not specified
*/
@Nonnull
Optional<String> threads();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ public CLIManager() {
options.addOption(Option.builder(THREADS)
.longOpt("threads")
.hasArg()
.desc("Thread count, for instance 4 (int) or 2C/2.5C (int/float) where C is core multiplied")
.desc(
"Thread count, for instance 4 (int) or 2C/2.5C (int/float) where C is core multiplied, or 'max' (available processors minus 1, minimum 1)")
.build());
options.addOption(Option.builder(BUILDER)
.longOpt("builder")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,9 @@ private void enableOnPresentOption(
}

int calculateDegreeOfConcurrency(String threadConfiguration) {
if ("max".equalsIgnoreCase(threadConfiguration)) {
return Math.max(1, Runtime.getRuntime().availableProcessors() - 1);
}
try {
if (threadConfiguration.endsWith("C")) {
String str = threadConfiguration.substring(0, threadConfiguration.length() - 1);
Expand All @@ -1647,7 +1650,7 @@ int calculateDegreeOfConcurrency(String threadConfiguration) {
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid threads value: '" + threadConfiguration
+ "'. Supported are int and float values ending with C.");
+ "'. Supported are 'max', int and float values ending with C.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ void testCalculateDegreeOfConcurrency() {
int cpus = Runtime.getRuntime().availableProcessors();
assertEquals((int) (cpus * 2.2), cli.calculateDegreeOfConcurrency("2.2C"));
assertEquals(1, cli.calculateDegreeOfConcurrency("0.0001C"));
assertEquals(Math.max(1, cpus - 1), cli.calculateDegreeOfConcurrency("max"));
assertEquals(Math.max(1, cpus - 1), cli.calculateDegreeOfConcurrency("MAX"));
assertEquals(Math.max(1, cpus - 1), cli.calculateDegreeOfConcurrency("Max"));
assertThrows(IllegalArgumentException.class, () -> cli.calculateDegreeOfConcurrency("-2.2C"));
assertThrows(IllegalArgumentException.class, () -> cli.calculateDegreeOfConcurrency("0C"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,9 @@ protected void populateRequestFromSettings(MavenExecutionRequest request, Settin
}

protected int calculateDegreeOfConcurrency(String threadConfiguration) {
if ("max".equalsIgnoreCase(threadConfiguration)) {
Comment thread
gnodet marked this conversation as resolved.
Resolved
return Math.max(1, Runtime.getRuntime().availableProcessors() - 1);
}
try {
if (threadConfiguration.endsWith("C")) {
String str = threadConfiguration.substring(0, threadConfiguration.length() - 1);
Expand All @@ -959,7 +962,7 @@ protected int calculateDegreeOfConcurrency(String threadConfiguration) {
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid threads value: '" + threadConfiguration
+ "'. Supported are int and float values ending with C.");
+ "'. Supported are 'max', int and float values ending with C.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ protected void prepareOptions(org.apache.commons.cli.Options options) {
options.addOption(Option.builder(THREADS)
.longOpt("threads")
.hasArg()
.desc("Thread count, for instance 4 (int) or 2C/2.5C (int/float) where C is core multiplied")
.desc(
"Thread count, for instance 4 (int) or 2C/2.5C (int/float) where C is core multiplied, or 'max' (available processors minus 1, minimum 1)")
.get());
options.addOption(Option.builder(BUILDER)
.longOpt("builder")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.cling.invoker;

import org.apache.maven.api.cli.InvokerRequest;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class LookupInvokerTest {

private final LookupInvoker<LookupContext> invoker =
new LookupInvoker<>(ProtoLookup.builder().build(), null) {
@Override
protected LookupContext createContext(InvokerRequest invokerRequest) {
return new LookupContext(
invokerRequest, false, invokerRequest.options().orElse(null));
}

@Override
protected int execute(LookupContext context) {
return 0;
}
};

@Test
void testCalculateDegreeOfConcurrency() {
int cpus = Runtime.getRuntime().availableProcessors();
assertEquals(Math.max(1, cpus - 1), invoker.calculateDegreeOfConcurrency("max"));
assertEquals(Math.max(1, cpus - 1), invoker.calculateDegreeOfConcurrency("MAX"));
assertEquals(Math.max(1, cpus - 1), invoker.calculateDegreeOfConcurrency("Max"));

assertEquals((int) (cpus * 2.2), invoker.calculateDegreeOfConcurrency("2.2C"));
assertEquals(1, invoker.calculateDegreeOfConcurrency("0.0001C"));
assertEquals(4, invoker.calculateDegreeOfConcurrency("4"));

assertThrows(IllegalArgumentException.class, () -> invoker.calculateDegreeOfConcurrency("0"));
assertThrows(IllegalArgumentException.class, () -> invoker.calculateDegreeOfConcurrency("-1"));
assertThrows(IllegalArgumentException.class, () -> invoker.calculateDegreeOfConcurrency("0C"));
assertThrows(IllegalArgumentException.class, () -> invoker.calculateDegreeOfConcurrency("-2.2C"));
assertThrows(IllegalArgumentException.class, () -> invoker.calculateDegreeOfConcurrency("1.0"));
assertThrows(IllegalArgumentException.class, () -> invoker.calculateDegreeOfConcurrency("2C2"));
assertThrows(IllegalArgumentException.class, () -> invoker.calculateDegreeOfConcurrency("CXXX"));
assertThrows(IllegalArgumentException.class, () -> invoker.calculateDegreeOfConcurrency("XXXC"));

IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, () -> invoker.calculateDegreeOfConcurrency("invalid"));
assertTrue(e.getMessage().contains("'max'"), "Error message should mention 'max'");
}
}