-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add support for Java 9+ JVM statistics
- Loading branch information
Jean-Baptiste Catté
committed
Mar 21, 2024
1 parent
98b1638
commit 9d3028d
Showing
8 changed files
with
202 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
jvm-statistics/src/test/java/com/criteo/hadoop/garmadon/jvm/utils/JavaRuntime.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.criteo.hadoop.garmadon.jvm.utils; | ||
|
||
public final class JavaRuntime { | ||
|
||
private static final Version VERSION = parseVersion(System.getProperty("java.version")); | ||
|
||
private JavaRuntime() { | ||
} | ||
|
||
public static int getVersion() throws RuntimeException { | ||
if (VERSION.versionNumber == -1) { | ||
throw new RuntimeException("Could not parse Java version.", VERSION.parsingError); | ||
} | ||
return VERSION.versionNumber; | ||
} | ||
|
||
static Version parseVersion(String version) { | ||
try { | ||
int versionNumber; | ||
if (version.startsWith("1.")) { | ||
versionNumber = Integer.parseInt(version.substring(2, 3)); | ||
} else { | ||
int dot = version.indexOf("."); | ||
versionNumber = Integer.parseInt(version.substring(0, dot)); | ||
} | ||
return new Version(versionNumber); | ||
} catch (RuntimeException e) { | ||
return new Version(e); | ||
} | ||
} | ||
|
||
static final class Version { | ||
|
||
private final int versionNumber; | ||
private final RuntimeException parsingError; | ||
|
||
private Version(int versionNumber) { | ||
this.versionNumber = versionNumber; | ||
this.parsingError = null; | ||
} | ||
|
||
private Version(RuntimeException parsingError) { | ||
this.versionNumber = -1; | ||
this.parsingError = parsingError; | ||
} | ||
|
||
public int getVersionNumber() { | ||
return versionNumber; | ||
} | ||
|
||
public RuntimeException getParsingError() { | ||
return parsingError; | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
jvm-statistics/src/test/java/com/criteo/hadoop/garmadon/jvm/utils/JavaRuntimeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.criteo.hadoop.garmadon.jvm.utils; | ||
|
||
import junit.framework.TestCase; | ||
|
||
import static com.criteo.hadoop.garmadon.jvm.utils.JavaRuntime.parseVersion; | ||
import static org.assertj.core.api.Assertions.*; | ||
|
||
public class JavaRuntimeTest extends TestCase { | ||
public void test_parse_1_8_x_as_8() { | ||
JavaRuntime.Version version = parseVersion("1.8.0_362"); | ||
assertThat(version.getVersionNumber()).isEqualTo(8); | ||
assertThat(version.getParsingError()).isNull(); | ||
} | ||
|
||
public void test_parse_11_x_as_11() { | ||
JavaRuntime.Version version = parseVersion("11.0.16"); | ||
assertThat(version.getVersionNumber()).isEqualTo(11); | ||
assertThat(version.getParsingError()).isNull(); | ||
} | ||
|
||
public void test_parsing_error() { | ||
JavaRuntime.Version version = parseVersion("ABC"); | ||
assertThat(version.getVersionNumber()).isEqualTo(-1); | ||
assertThat(version.getParsingError()).isNotNull(); | ||
} | ||
|
||
public void test_getVersion() { | ||
assertThat(JavaRuntime.getVersion()).isGreaterThanOrEqualTo(8); | ||
} | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
jvm-statistics/src/test/java/com/criteo/hadoop/garmadon/jvm/utils/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Utilities | ||
*/ | ||
package com.criteo.hadoop.garmadon.jvm.utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters