diff --git a/HyperAlloc/pom.xml b/HyperAlloc/pom.xml index 58279bc..0cf03b6 100644 --- a/HyperAlloc/pom.xml +++ b/HyperAlloc/pom.xml @@ -47,11 +47,11 @@ 0.8.5 - 8 - 1.8 - 1.8 - 1.8 - 1.8 + 17 + 17 + 17 + 17 + 17 UTF-8 UTF-8 2.22.1 @@ -385,6 +385,10 @@ org.apache.maven.plugins maven-compiler-plugin + + 15 + 15 + org.apache.maven.plugins diff --git a/HyperAlloc/src/main/java/com/amazon/corretto/benchmark/hyperalloc/HyperAlloc.java b/HyperAlloc/src/main/java/com/amazon/corretto/benchmark/hyperalloc/HyperAlloc.java index e5df081..3539c4c 100644 --- a/HyperAlloc/src/main/java/com/amazon/corretto/benchmark/hyperalloc/HyperAlloc.java +++ b/HyperAlloc/src/main/java/com/amazon/corretto/benchmark/hyperalloc/HyperAlloc.java @@ -8,14 +8,29 @@ public final class HyperAlloc { private HyperAlloc() {} public static void main(String[] args) { + try { + runner(args); + } catch (Throwable t) { + System.out.println(t.getMessage()); + System.exit(1); + } + } - switch (findRunType(args)) { - case "simple" : - new SimpleRunner(new SimpleRunConfig(args)).start(); - break; - default: - System.out.println("Current supported run type (-u): simple."); - System.exit(1); + static void runner(String[] args) { + try { + String runType = findRunType(args); + + switch (runType) { + case "simple": + new SimpleRunner(new SimpleRunConfig(args)).start(); + break; + default: + throw new IllegalArgumentException(String.format("Unknown run type (-u %s), supported type is -u simple.", runType)); + } + } catch (IllegalArgumentException iae) { + throw iae; + } catch (Throwable t) { + throw new RuntimeException(t); } } diff --git a/HyperAlloc/src/main/java/com/amazon/corretto/benchmark/hyperalloc/SimpleRunConfig.java b/HyperAlloc/src/main/java/com/amazon/corretto/benchmark/hyperalloc/SimpleRunConfig.java index 5452de2..c8b202c 100644 --- a/HyperAlloc/src/main/java/com/amazon/corretto/benchmark/hyperalloc/SimpleRunConfig.java +++ b/HyperAlloc/src/main/java/com/amazon/corretto/benchmark/hyperalloc/SimpleRunConfig.java @@ -65,8 +65,7 @@ public SimpleRunConfig(final String[] args) { } else if (args[i].equals("-z")) { allocationSmoothnessFactor = Double.parseDouble(args[++i]); if (allocationSmoothnessFactor < 0 || allocationSmoothnessFactor > 1.0) { - usage(); - System.exit(1); + throw new IllegalArgumentException(String.format("AllocationSmoothness of %s not in range\n",allocationSmoothnessFactor) + usage()); } } else if (args[i].equals("-l")) { logFile = args[++i]; @@ -77,19 +76,13 @@ public SimpleRunConfig(final String[] args) { } else if (args[i].equals("-p") || args[i].equals("--ramp-up-seconds")) { rampUpSeconds = Double.parseDouble(args[++i]); } else { - usage(); - System.exit(1); + throw new IllegalArgumentException(String.format("Unrecognized argument: %s\n",args[i]) + usage()); } } } - private void usage() { - System.out.println("Usage: java -jar HyperAlloc.jar " + - "[-u run type] [-a allocRateInMb] [-s longLivedObjectsInMb] " + - "[-m midAgedObjectsInMb] [-d runDurationInSeconds ] [-t numOfThreads] [-n minObjectSize] " + - "[-x maxObjectSize] [-r pruneRatio] [-f reshuffleRatio] " + - "[-l outputFile] [-b|-allocation-log logFile] [-z allocationSmoothness (0 to 1.0)] " + - "[-p rampUpSeconds ]"); + private String usage() { + return "Usage: java -jar HyperAlloc.jar [-u run type] [-a allocRateInMb] [-s longLivedObjectsInMb] [-m midAgedObjectsInMb] [-d runDurationInSeconds ] [-t numOfThreads] [-n minObjectSize] [-x maxObjectSize] [-r pruneRatio] [-f reshuffleRatio] [-l outputFile] [-b|-allocation-log logFile] [-z allocationSmoothness (0 to 1.0)] [-p rampUpSeconds ]"; } /** diff --git a/HyperAlloc/src/main/java/com/amazon/corretto/benchmark/hyperalloc/SimpleRunner.java b/HyperAlloc/src/main/java/com/amazon/corretto/benchmark/hyperalloc/SimpleRunner.java index 12740a3..78722fb 100644 --- a/HyperAlloc/src/main/java/com/amazon/corretto/benchmark/hyperalloc/SimpleRunner.java +++ b/HyperAlloc/src/main/java/com/amazon/corretto/benchmark/hyperalloc/SimpleRunner.java @@ -2,7 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 package com.amazon.corretto.benchmark.hyperalloc; -import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.List; diff --git a/HyperAlloc/src/test/java/com/amazon/corretto/benchmark/hyperalloc/HyperAllocTest.java b/HyperAlloc/src/test/java/com/amazon/corretto/benchmark/hyperalloc/HyperAllocTest.java index c4281d0..0ed8c1c 100644 --- a/HyperAlloc/src/test/java/com/amazon/corretto/benchmark/hyperalloc/HyperAllocTest.java +++ b/HyperAlloc/src/test/java/com/amazon/corretto/benchmark/hyperalloc/HyperAllocTest.java @@ -4,9 +4,7 @@ import org.junit.jupiter.api.Test; -import static com.github.stefanbirkner.systemlambda.SystemLambda.catchSystemExit; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.*; class HyperAllocTest { @@ -21,9 +19,11 @@ void DefaultRunTypeTest() { } @Test - void UnknownRunTypeTest() throws Exception { - int status = catchSystemExit( - () -> HyperAlloc.main(new String[]{"-u", "unknown", "-a", "5"})); - assertThat(status, is(1)); + void UnknownRunTypeTest() { + Exception e = assertThrows(IllegalArgumentException.class, () -> { + HyperAlloc.runner(new String[]{"-u", "unknown", "-a", "5"}); + }); + String expected = "Unknown run type (-u unknown), supported type is -u simple."; + assertEquals(expected,e.getMessage()); } } diff --git a/HyperAlloc/src/test/java/com/amazon/corretto/benchmark/hyperalloc/SimpleRunConfigTest.java b/HyperAlloc/src/test/java/com/amazon/corretto/benchmark/hyperalloc/SimpleRunConfigTest.java index 7b9af82..ea0792b 100644 --- a/HyperAlloc/src/test/java/com/amazon/corretto/benchmark/hyperalloc/SimpleRunConfigTest.java +++ b/HyperAlloc/src/test/java/com/amazon/corretto/benchmark/hyperalloc/SimpleRunConfigTest.java @@ -3,13 +3,10 @@ package com.amazon.corretto.benchmark.hyperalloc; import com.sun.management.HotSpotDiagnosticMXBean; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.lang.management.ManagementFactory; -import static com.github.stefanbirkner.systemlambda.SystemLambda.catchSystemExit; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.CoreMatchers.is; import static org.junit.jupiter.api.Assertions.*; @@ -84,10 +81,14 @@ void StringArgsTest() { } @Test - void UnknownParameterShouldExitTest() throws Exception { - int status = catchSystemExit( - () -> new SimpleRunConfig(new String[]{"-w", "who"})); - assertThat(status, is(1)); + void UnknownParameterShouldExitTest() { + Exception e = assertThrows(IllegalArgumentException.class, () -> { + HyperAlloc.runner(new String[]{"-w", "who"}); + }); + + String expected = "Unrecognized argument: -w\n" + + "Usage: java -jar HyperAlloc.jar [-u run type] [-a allocRateInMb] [-s longLivedObjectsInMb] [-m midAgedObjectsInMb] [-d runDurationInSeconds ] [-t numOfThreads] [-n minObjectSize] [-x maxObjectSize] [-r pruneRatio] [-f reshuffleRatio] [-l outputFile] [-b|-allocation-log logFile] [-z allocationSmoothness (0 to 1.0)] [-p rampUpSeconds ]"; + assertEquals(expected,e.getMessage()); } class MySecurityManager extends SecurityManager {}