fix(test): 고정 힙 하한을 제거하고 정책과 테스트를 Kotlin으로 전환한다
This commit is contained in:
+1
-1
@@ -121,7 +121,7 @@ tasks.withType<Test>().configureEach {
|
|||||||
val heapMiB = TestHeapPolicy.calculateHeapMiB(availableBytes, maxParallelForks, maxWorkers)
|
val heapMiB = TestHeapPolicy.calculateHeapMiB(availableBytes, maxParallelForks, maxWorkers)
|
||||||
maxHeapSize = "${heapMiB}m"
|
maxHeapSize = "${heapMiB}m"
|
||||||
logger.lifecycle(
|
logger.lifecycle(
|
||||||
"Test heap: available estimate={} MiB, workers={}, maxHeap={} MiB (50% budget, 1 GiB native reserve per worker)",
|
"Test heap: available estimate={} MiB, workers={}, maxHeap={} MiB (50% worker budget, 25% native reserve)",
|
||||||
availableBytes / (1024 * 1024),
|
availableBytes / (1024 * 1024),
|
||||||
minOf(maxParallelForks, maxWorkers),
|
minOf(maxParallelForks, maxWorkers),
|
||||||
heapMiB
|
heapMiB
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
plugins {
|
plugins {
|
||||||
java
|
`kotlin-dsl`
|
||||||
|
id("org.jlleitschuh.gradle.ktlint") version "11.5.0"
|
||||||
}
|
}
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
|
|||||||
@@ -1,124 +0,0 @@
|
|||||||
package buildlogic;
|
|
||||||
|
|
||||||
import com.sun.management.OperatingSystemMXBean;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.lang.management.ManagementFactory;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
public final class TestHeapPolicy {
|
|
||||||
private static final long MIB = 1L << 20;
|
|
||||||
private static final long WORKER_RESERVE_BYTES = 1L << 30;
|
|
||||||
private static final long MINIMUM_HEAP_MIB = 4096;
|
|
||||||
private static final Pattern PAGE_SIZE = Pattern.compile(
|
|
||||||
"Mach Virtual Memory Statistics:\\s*\\(page size of ([0-9]+) bytes\\)"
|
|
||||||
);
|
|
||||||
private static final Pattern PAGE_COUNTER = Pattern.compile("Pages (free|inactive)\\s*:\\s*([0-9]+)\\.?");
|
|
||||||
|
|
||||||
private TestHeapPolicy() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public static long calculateHeapMiB(long availableBytes, int maxParallelForks, int maxWorkers) {
|
|
||||||
if (availableBytes <= 0 || maxParallelForks <= 0 || maxWorkers <= 0) {
|
|
||||||
throw new IllegalArgumentException("Available bytes, maxParallelForks and maxWorkers must be positive");
|
|
||||||
}
|
|
||||||
|
|
||||||
int effectiveWorkers = Math.min(maxParallelForks, maxWorkers);
|
|
||||||
long perWorkerBytes = availableBytes / 2 / effectiveWorkers;
|
|
||||||
long heapMiB = (perWorkerBytes - WORKER_RESERVE_BYTES) / MIB;
|
|
||||||
if (heapMiB < MINIMUM_HEAP_MIB) {
|
|
||||||
throw new IllegalStateException(
|
|
||||||
"Insufficient test memory: availableBytes=" + availableBytes + ", effectiveWorkers=" + effectiveWorkers
|
|
||||||
+ ", minimumHeapMiB=" + MINIMUM_HEAP_MIB + ". Close other applications or reduce workers."
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return heapMiB;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static long readAvailableBytes() {
|
|
||||||
long availableBytes;
|
|
||||||
if (System.getProperty("os.name").startsWith("Mac")) {
|
|
||||||
ProcessBuilder builder = new ProcessBuilder("/usr/bin/vm_stat").redirectErrorStream(true);
|
|
||||||
builder.environment().put("LC_ALL", "C");
|
|
||||||
try {
|
|
||||||
Process process = builder.start();
|
|
||||||
try (
|
|
||||||
InputStream output = process.getInputStream();
|
|
||||||
InputStream error = process.getErrorStream();
|
|
||||||
OutputStream input = process.getOutputStream()
|
|
||||||
) {
|
|
||||||
if (!process.waitFor(5, TimeUnit.SECONDS)) {
|
|
||||||
throw new IllegalStateException("vm_stat did not finish within 5 seconds");
|
|
||||||
}
|
|
||||||
if (process.exitValue() != 0) {
|
|
||||||
throw new IllegalStateException("vm_stat failed with exit code " + process.exitValue());
|
|
||||||
}
|
|
||||||
availableBytes = parseMacVmStat(new String(output.readAllBytes(), StandardCharsets.US_ASCII));
|
|
||||||
} finally {
|
|
||||||
process.destroyForcibly();
|
|
||||||
}
|
|
||||||
} catch (InterruptedException failure) {
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
throw new IllegalStateException("Interrupted while reading available memory with vm_stat", failure);
|
|
||||||
} catch (IOException failure) {
|
|
||||||
throw new IllegalStateException("Unable to read available memory with vm_stat", failure);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
OperatingSystemMXBean operatingSystem = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);
|
|
||||||
if (operatingSystem == null) {
|
|
||||||
throw new IllegalStateException("OperatingSystemMXBean free-memory observation is unavailable");
|
|
||||||
}
|
|
||||||
availableBytes = operatingSystem.getFreeMemorySize();
|
|
||||||
}
|
|
||||||
if (availableBytes <= 0) {
|
|
||||||
throw new IllegalStateException("Available-memory observation must be positive: availableBytes=" + availableBytes);
|
|
||||||
}
|
|
||||||
return availableBytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
static long parseMacVmStat(String output) {
|
|
||||||
long pageSize = -1;
|
|
||||||
long freePages = -1;
|
|
||||||
long inactivePages = -1;
|
|
||||||
try {
|
|
||||||
for (String rawLine : output.lines().toList()) {
|
|
||||||
String line = rawLine.strip();
|
|
||||||
if (line.startsWith("Mach Virtual Memory Statistics")) {
|
|
||||||
Matcher header = PAGE_SIZE.matcher(line);
|
|
||||||
if (pageSize >= 0 || !header.matches()) {
|
|
||||||
throw new IllegalArgumentException("Duplicate or malformed vm_stat page-size header: " + line);
|
|
||||||
}
|
|
||||||
pageSize = Long.parseLong(header.group(1));
|
|
||||||
} else if (line.startsWith("Pages free") || line.startsWith("Pages inactive")) {
|
|
||||||
Matcher counter = PAGE_COUNTER.matcher(line);
|
|
||||||
if (!counter.matches()) {
|
|
||||||
throw new IllegalArgumentException("Malformed vm_stat page counter: " + line);
|
|
||||||
}
|
|
||||||
long pages = Long.parseLong(counter.group(2));
|
|
||||||
if (counter.group(1).equals("free")) {
|
|
||||||
if (freePages >= 0) {
|
|
||||||
throw new IllegalArgumentException("Duplicate vm_stat Pages free counter");
|
|
||||||
}
|
|
||||||
freePages = pages;
|
|
||||||
} else {
|
|
||||||
if (inactivePages >= 0) {
|
|
||||||
throw new IllegalArgumentException("Duplicate vm_stat Pages inactive counter");
|
|
||||||
}
|
|
||||||
inactivePages = pages;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (pageSize <= 0 || freePages < 0 || inactivePages < 0) {
|
|
||||||
throw new IllegalArgumentException("vm_stat requires a positive page size and both free and inactive counters");
|
|
||||||
}
|
|
||||||
// Raw free already includes speculative pages; do not subtract or add them separately.
|
|
||||||
return Math.multiplyExact(Math.addExact(freePages, inactivePages), pageSize);
|
|
||||||
} catch (NumberFormatException | ArithmeticException failure) {
|
|
||||||
throw new IllegalArgumentException("vm_stat page size or counters exceed the supported long range", failure);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
package buildlogic
|
||||||
|
|
||||||
|
import com.sun.management.OperatingSystemMXBean
|
||||||
|
import java.io.IOException
|
||||||
|
import java.lang.management.ManagementFactory
|
||||||
|
import java.nio.charset.StandardCharsets
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
import java.util.regex.Pattern
|
||||||
|
|
||||||
|
object TestHeapPolicy {
|
||||||
|
private const val MIB = 1L shl 20
|
||||||
|
private const val JVM_MINIMUM_HEAP_MIB = 2L
|
||||||
|
private val PAGE_SIZE = Pattern.compile(
|
||||||
|
"Mach Virtual Memory Statistics:\\s*\\(page size of ([0-9]+) bytes\\)"
|
||||||
|
)
|
||||||
|
private val PAGE_COUNTER = Pattern.compile("Pages (free|inactive)\\s*:\\s*([0-9]+)\\.?")
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun calculateHeapMiB(availableBytes: Long, maxParallelForks: Int, maxWorkers: Int): Long {
|
||||||
|
require(availableBytes > 0 && maxParallelForks > 0 && maxWorkers > 0) {
|
||||||
|
"Available bytes, maxParallelForks and maxWorkers must be positive"
|
||||||
|
}
|
||||||
|
|
||||||
|
val effectiveWorkers = minOf(maxParallelForks, maxWorkers)
|
||||||
|
val perWorkerBytes = availableBytes / 2 / effectiveWorkers
|
||||||
|
val nativeReserveBytes = (perWorkerBytes + 3) / 4
|
||||||
|
val heapMiB = (perWorkerBytes - nativeReserveBytes) / MIB
|
||||||
|
check(heapMiB >= JVM_MINIMUM_HEAP_MIB) {
|
||||||
|
"Insufficient test memory: availableBytes=$availableBytes, effectiveWorkers=$effectiveWorkers" +
|
||||||
|
", heapMiB=$heapMiB. JVM requires at least 2 MiB. Close other applications or reduce workers."
|
||||||
|
}
|
||||||
|
return heapMiB
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun readAvailableBytes(): Long {
|
||||||
|
val availableBytes = if (System.getProperty("os.name").startsWith("Mac")) {
|
||||||
|
val builder = ProcessBuilder("/usr/bin/vm_stat").redirectErrorStream(true)
|
||||||
|
builder.environment()["LC_ALL"] = "C"
|
||||||
|
try {
|
||||||
|
val process = builder.start()
|
||||||
|
try {
|
||||||
|
process.inputStream.use { output ->
|
||||||
|
process.errorStream.use {
|
||||||
|
process.outputStream.use {
|
||||||
|
check(process.waitFor(5, TimeUnit.SECONDS)) {
|
||||||
|
"vm_stat did not finish within 5 seconds"
|
||||||
|
}
|
||||||
|
check(process.exitValue() == 0) {
|
||||||
|
"vm_stat failed with exit code ${process.exitValue()}"
|
||||||
|
}
|
||||||
|
parseMacVmStat(String(output.readAllBytes(), StandardCharsets.US_ASCII))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
process.destroyForcibly()
|
||||||
|
}
|
||||||
|
} catch (failure: InterruptedException) {
|
||||||
|
Thread.currentThread().interrupt()
|
||||||
|
throw IllegalStateException("Interrupted while reading available memory with vm_stat", failure)
|
||||||
|
} catch (failure: IOException) {
|
||||||
|
throw IllegalStateException("Unable to read available memory with vm_stat", failure)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
val operatingSystem = checkNotNull(ManagementFactory.getPlatformMXBean(OperatingSystemMXBean::class.java)) {
|
||||||
|
"OperatingSystemMXBean free-memory observation is unavailable"
|
||||||
|
}
|
||||||
|
operatingSystem.freeMemorySize
|
||||||
|
}
|
||||||
|
check(availableBytes > 0) {
|
||||||
|
"Available-memory observation must be positive: availableBytes=$availableBytes"
|
||||||
|
}
|
||||||
|
return availableBytes
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun parseMacVmStat(output: String): Long {
|
||||||
|
var pageSize = -1L
|
||||||
|
var freePages = -1L
|
||||||
|
var inactivePages = -1L
|
||||||
|
try {
|
||||||
|
for (rawLine in output.lineSequence()) {
|
||||||
|
val line = rawLine.trim { Character.isWhitespace(it) }
|
||||||
|
if (line.startsWith("Mach Virtual Memory Statistics")) {
|
||||||
|
val header = PAGE_SIZE.matcher(line)
|
||||||
|
require(pageSize < 0 && header.matches()) {
|
||||||
|
"Duplicate or malformed vm_stat page-size header: $line"
|
||||||
|
}
|
||||||
|
pageSize = header.group(1).toLong()
|
||||||
|
} else if (line.startsWith("Pages free") || line.startsWith("Pages inactive")) {
|
||||||
|
val counter = PAGE_COUNTER.matcher(line)
|
||||||
|
require(counter.matches()) { "Malformed vm_stat page counter: $line" }
|
||||||
|
val pages = counter.group(2).toLong()
|
||||||
|
if (counter.group(1) == "free") {
|
||||||
|
require(freePages < 0) { "Duplicate vm_stat Pages free counter" }
|
||||||
|
freePages = pages
|
||||||
|
} else {
|
||||||
|
require(inactivePages < 0) { "Duplicate vm_stat Pages inactive counter" }
|
||||||
|
inactivePages = pages
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
require(pageSize > 0 && freePages >= 0 && inactivePages >= 0) {
|
||||||
|
"vm_stat requires a positive page size and both free and inactive counters"
|
||||||
|
}
|
||||||
|
// Raw free already includes speculative pages; do not subtract or add them separately.
|
||||||
|
return Math.multiplyExact(Math.addExact(freePages, inactivePages), pageSize)
|
||||||
|
} catch (failure: NumberFormatException) {
|
||||||
|
throw IllegalArgumentException("vm_stat page size or counters exceed the supported long range", failure)
|
||||||
|
} catch (failure: ArithmeticException) {
|
||||||
|
throw IllegalArgumentException("vm_stat page size or counters exceed the supported long range", failure)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,149 +0,0 @@
|
|||||||
package buildlogic;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.stream.Stream;
|
|
||||||
import org.junit.jupiter.api.DynamicTest;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.junit.jupiter.api.TestFactory;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||||||
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
|
|
||||||
|
|
||||||
class TestHeapPolicyTest {
|
|
||||||
private static final long MIB = 1L << 20;
|
|
||||||
private static final long GIB = 1L << 30;
|
|
||||||
|
|
||||||
@TestFactory
|
|
||||||
Stream<DynamicTest> shouldCalculateHeapWithinTheAvailableWorkerBudget() {
|
|
||||||
return Stream.of(
|
|
||||||
dynamicTest("10 GiB reaches the 4096 MiB floor", () ->
|
|
||||||
assertEquals(4096, TestHeapPolicy.calculateHeapMiB(10 * GIB, 1, 1))),
|
|
||||||
dynamicTest("20 GiB supports two workers at the floor", () ->
|
|
||||||
assertEquals(4096, TestHeapPolicy.calculateHeapMiB(20 * GIB, 2, 2))),
|
|
||||||
dynamicTest("13 GiB leaves half and the per-worker reserve", () ->
|
|
||||||
assertEquals(5632, TestHeapPolicy.calculateHeapMiB(13 * GIB, 1, 1))),
|
|
||||||
dynamicTest("max workers caps four requested forks", () ->
|
|
||||||
assertEquals(7168, TestHeapPolicy.calculateHeapMiB(32 * GIB, 4, 2))),
|
|
||||||
dynamicTest("max forks caps four available workers", () ->
|
|
||||||
assertEquals(7168, TestHeapPolicy.calculateHeapMiB(32 * GIB, 2, 4))),
|
|
||||||
dynamicTest("fractional MiB rounds down", () ->
|
|
||||||
assertEquals(5632, TestHeapPolicy.calculateHeapMiB(13 * GIB + 2 * MIB - 1, 1, 1))),
|
|
||||||
dynamicTest("an exact additional MiB is retained", () ->
|
|
||||||
assertEquals(5633, TestHeapPolicy.calculateHeapMiB(13 * GIB + 2 * MIB, 1, 1))),
|
|
||||||
dynamicTest("the largest positive long does not overflow", () ->
|
|
||||||
assertEquals(4_398_046_510_079L, TestHeapPolicy.calculateHeapMiB(Long.MAX_VALUE, 1, 1)))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestFactory
|
|
||||||
Stream<DynamicTest> shouldRejectInsufficientBudgetsWithActionableDiagnostics() {
|
|
||||||
return Stream.of(
|
|
||||||
new long[] {10 * GIB - 1, 1, 1},
|
|
||||||
new long[] {20 * GIB - 1, 4, 2},
|
|
||||||
new long[] {1, 1, 1},
|
|
||||||
new long[] {GIB, 1, 1},
|
|
||||||
new long[] {2 * GIB, 1, 1},
|
|
||||||
new long[] {8 * GIB, 1, 1}
|
|
||||||
).map(input -> dynamicTest(Arrays.toString(input), () -> {
|
|
||||||
IllegalStateException failure = assertThrows(IllegalStateException.class, () ->
|
|
||||||
TestHeapPolicy.calculateHeapMiB(input[0], (int) input[1], (int) input[2]));
|
|
||||||
|
|
||||||
String message = failure.getMessage();
|
|
||||||
assertAll(
|
|
||||||
() -> assertTrue(message.contains("availableBytes=" + input[0]), message),
|
|
||||||
() -> assertTrue(message.contains("effectiveWorkers=" + Math.min(input[1], input[2])), message),
|
|
||||||
() -> assertTrue(message.contains("minimumHeapMiB=4096"), message),
|
|
||||||
() -> assertTrue(message.contains("Close other applications"), message),
|
|
||||||
() -> assertTrue(message.contains("reduce workers"), message)
|
|
||||||
);
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestFactory
|
|
||||||
Stream<DynamicTest> shouldRejectNonpositiveAvailabilityOrWorkerLimits() {
|
|
||||||
return Stream.of(
|
|
||||||
new long[] {0, 1, 1},
|
|
||||||
new long[] {-1, 1, 1},
|
|
||||||
new long[] {Long.MIN_VALUE, 1, 1},
|
|
||||||
new long[] {10 * GIB, 0, 1},
|
|
||||||
new long[] {10 * GIB, -1, 1},
|
|
||||||
new long[] {10 * GIB, Integer.MIN_VALUE, 1},
|
|
||||||
new long[] {10 * GIB, 1, 0},
|
|
||||||
new long[] {10 * GIB, 1, -1},
|
|
||||||
new long[] {10 * GIB, 1, Integer.MIN_VALUE}
|
|
||||||
).map(input -> dynamicTest(Arrays.toString(input), () ->
|
|
||||||
assertThrows(IllegalArgumentException.class, () ->
|
|
||||||
TestHeapPolicy.calculateHeapMiB(input[0], (int) input[1], (int) input[2]))));
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestFactory
|
|
||||||
Stream<DynamicTest> shouldParseOnlyRawFreeAndInactivePages() {
|
|
||||||
return Stream.of(
|
|
||||||
dynamicTest("4K pages ignore speculative and file-backed counts", () ->
|
|
||||||
assertEquals(2_371_584, TestHeapPolicy.parseMacVmStat("""
|
|
||||||
Mach Virtual Memory Statistics: (page size of 4096 bytes)
|
|
||||||
Pages free: 123.
|
|
||||||
Pages speculative: 100.
|
|
||||||
Pages inactive: 456.
|
|
||||||
File-backed pages: 999.
|
|
||||||
"""))),
|
|
||||||
dynamicTest("16K pages accept reordered counters and whitespace", () ->
|
|
||||||
assertEquals(81_920, TestHeapPolicy.parseMacVmStat("""
|
|
||||||
Mach Virtual Memory Statistics: (page size of 16384 bytes)
|
|
||||||
Pages inactive : 3.
|
|
||||||
Pages active: 999.
|
|
||||||
Pages free:\t2.
|
|
||||||
"""))),
|
|
||||||
dynamicTest("both raw counters may be zero", () ->
|
|
||||||
assertEquals(0, TestHeapPolicy.parseMacVmStat(vmStat("4096", "0", "0")))),
|
|
||||||
dynamicTest("counters need not have trailing periods", () ->
|
|
||||||
assertEquals(20_480, TestHeapPolicy.parseMacVmStat(
|
|
||||||
vmStat("4096", "2", "3").replace(".\n", "\n")))),
|
|
||||||
dynamicTest("the largest representable byte count remains valid", () ->
|
|
||||||
assertEquals(Long.MAX_VALUE, TestHeapPolicy.parseMacVmStat(vmStat("1", "9223372036854775806", "1"))))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestFactory
|
|
||||||
Stream<DynamicTest> shouldRejectMissingDuplicateMalformedNegativeOrOverflowingVmStatFields() {
|
|
||||||
String valid = vmStat("4096", "2", "3");
|
|
||||||
return Stream.of(
|
|
||||||
"",
|
|
||||||
"Pages free: 2.\nPages inactive: 3.\n",
|
|
||||||
"page size of 4096 bytes\nPages free: 2.\nPages inactive: 3.\n",
|
|
||||||
valid.replace("Pages free: 2.\n", ""),
|
|
||||||
valid.replace("Pages inactive: 3.\n", ""),
|
|
||||||
valid + "Pages free: 2.\n",
|
|
||||||
valid + "Pages inactive: 3.\n",
|
|
||||||
valid + "Mach Virtual Memory Statistics: (page size of 4096 bytes)\n",
|
|
||||||
valid + "Pages free: broken.\n",
|
|
||||||
vmStat("4096", "-1", "3"),
|
|
||||||
vmStat("4096", "2", "-1"),
|
|
||||||
vmStat("-4096", "2", "3"),
|
|
||||||
vmStat("0", "2", "3"),
|
|
||||||
vmStat("invalid", "2", "3"),
|
|
||||||
vmStat("4096", "2x", "3"),
|
|
||||||
vmStat("4096", "2", "3.5"),
|
|
||||||
valid.replace("Pages free:", "Pages free"),
|
|
||||||
valid.replace("bytes)", "bytes) trailing"),
|
|
||||||
vmStat("9223372036854775808", "2", "3"),
|
|
||||||
vmStat("4096", "9223372036854775808", "3"),
|
|
||||||
vmStat("1", "9223372036854775807", "1"),
|
|
||||||
vmStat("4096", "2251799813685248", "0")
|
|
||||||
).map(output -> dynamicTest("rejects " + output.replace('\n', ' '), () ->
|
|
||||||
assertThrows(IllegalArgumentException.class, () -> TestHeapPolicy.parseMacVmStat(output))));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void shouldReadPositiveAvailabilityFromTheCurrentOperatingSystem() {
|
|
||||||
assertTrue(TestHeapPolicy.readAvailableBytes() > 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String vmStat(String pageSize, String free, String inactive) {
|
|
||||||
return "Mach Virtual Memory Statistics: (page size of " + pageSize + " bytes)\n"
|
|
||||||
+ "Pages free: " + free + ".\nPages inactive: " + inactive + ".\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,187 @@
|
|||||||
|
package buildlogic
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions.assertAll
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Assertions.assertThrows
|
||||||
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
|
import org.junit.jupiter.api.DynamicTest
|
||||||
|
import org.junit.jupiter.api.DynamicTest.dynamicTest
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.api.TestFactory
|
||||||
|
|
||||||
|
class TestHeapPolicyTest {
|
||||||
|
@TestFactory
|
||||||
|
fun shouldCalculateHeapWithinTheAvailableWorkerBudget(): List<DynamicTest> = listOf(
|
||||||
|
dynamicTest("the reported Jenkins availability runs below the former fixed floor") {
|
||||||
|
assertEquals(879L, TestHeapPolicy.calculateHeapMiB(2_460_004_352L, 1, 1))
|
||||||
|
},
|
||||||
|
dynamicTest("1 GiB reserves native memory proportionally") {
|
||||||
|
assertEquals(384L, TestHeapPolicy.calculateHeapMiB(GIB, 1, 1))
|
||||||
|
},
|
||||||
|
dynamicTest("2 GiB does not lose the whole worker budget to a fixed reserve") {
|
||||||
|
assertEquals(768L, TestHeapPolicy.calculateHeapMiB(2 * GIB, 1, 1))
|
||||||
|
},
|
||||||
|
dynamicTest("8 GiB permits a heap below 4 GiB") {
|
||||||
|
assertEquals(3072L, TestHeapPolicy.calculateHeapMiB(8 * GIB, 1, 1))
|
||||||
|
},
|
||||||
|
dynamicTest("10 GiB scales without a policy floor") {
|
||||||
|
assertEquals(3840L, TestHeapPolicy.calculateHeapMiB(10 * GIB, 1, 1))
|
||||||
|
},
|
||||||
|
dynamicTest("20 GiB is shared between two workers") {
|
||||||
|
assertEquals(3840L, TestHeapPolicy.calculateHeapMiB(20 * GIB, 2, 2))
|
||||||
|
},
|
||||||
|
dynamicTest("13 GiB leaves half and a proportional native reserve") {
|
||||||
|
assertEquals(4992L, TestHeapPolicy.calculateHeapMiB(13 * GIB, 1, 1))
|
||||||
|
},
|
||||||
|
dynamicTest("max workers caps four requested forks") {
|
||||||
|
assertEquals(6144L, TestHeapPolicy.calculateHeapMiB(32 * GIB, 4, 2))
|
||||||
|
},
|
||||||
|
dynamicTest("max forks caps four available workers") {
|
||||||
|
assertEquals(6144L, TestHeapPolicy.calculateHeapMiB(32 * GIB, 2, 4))
|
||||||
|
},
|
||||||
|
dynamicTest("fractional MiB rounds down") {
|
||||||
|
assertEquals(4992L, TestHeapPolicy.calculateHeapMiB(13 * GIB + 2 * MIB, 1, 1))
|
||||||
|
},
|
||||||
|
dynamicTest("an exact proportional increase is retained") {
|
||||||
|
assertEquals(4995L, TestHeapPolicy.calculateHeapMiB(13 * GIB + 8 * MIB, 1, 1))
|
||||||
|
},
|
||||||
|
dynamicTest("one byte below the proportional increase rounds down") {
|
||||||
|
assertEquals(4994L, TestHeapPolicy.calculateHeapMiB(13 * GIB + 8 * MIB - 1, 1, 1))
|
||||||
|
},
|
||||||
|
dynamicTest("the JVM technical minimum is not replaced with an application floor") {
|
||||||
|
assertEquals(2L, TestHeapPolicy.calculateHeapMiB(5_592_406, 1, 1))
|
||||||
|
},
|
||||||
|
dynamicTest("the JVM technical minimum accounts for two workers") {
|
||||||
|
assertEquals(2L, TestHeapPolicy.calculateHeapMiB(11_184_812, 2, 2))
|
||||||
|
},
|
||||||
|
dynamicTest("the largest positive long does not overflow") {
|
||||||
|
assertEquals(3_298_534_883_327L, TestHeapPolicy.calculateHeapMiB(Long.MAX_VALUE, 1, 1))
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
@TestFactory
|
||||||
|
fun shouldRejectOnlyBudgetsTooSmallToStartTheJvm(): List<DynamicTest> = listOf(
|
||||||
|
Triple(5_592_405L, 1, 1),
|
||||||
|
Triple(11_184_811L, 4, 2),
|
||||||
|
Triple(1L, 1, 1),
|
||||||
|
Triple(MIB, 1, 1),
|
||||||
|
Triple(2 * MIB, 1, 1),
|
||||||
|
Triple(4 * MIB, 1, 1)
|
||||||
|
).map { (available, forks, workers) ->
|
||||||
|
dynamicTest("[$available, $forks, $workers]") {
|
||||||
|
val failure = assertThrows(IllegalStateException::class.java) {
|
||||||
|
TestHeapPolicy.calculateHeapMiB(available, forks, workers)
|
||||||
|
}
|
||||||
|
val message = requireNotNull(failure.message)
|
||||||
|
assertAll(
|
||||||
|
{ assertTrue(message.contains("availableBytes=$available"), message) },
|
||||||
|
{ assertTrue(message.contains("effectiveWorkers=${minOf(forks, workers)}"), message) },
|
||||||
|
{ assertTrue(message.contains("JVM requires at least 2 MiB"), message) },
|
||||||
|
{ assertTrue(message.contains("Close other applications"), message) },
|
||||||
|
{ assertTrue(message.contains("reduce workers"), message) }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestFactory
|
||||||
|
fun shouldRejectNonpositiveAvailabilityOrWorkerLimits(): List<DynamicTest> = listOf(
|
||||||
|
Triple(0L, 1, 1),
|
||||||
|
Triple(-1L, 1, 1),
|
||||||
|
Triple(Long.MIN_VALUE, 1, 1),
|
||||||
|
Triple(10 * GIB, 0, 1),
|
||||||
|
Triple(10 * GIB, -1, 1),
|
||||||
|
Triple(10 * GIB, Int.MIN_VALUE, 1),
|
||||||
|
Triple(10 * GIB, 1, 0),
|
||||||
|
Triple(10 * GIB, 1, -1),
|
||||||
|
Triple(10 * GIB, 1, Int.MIN_VALUE)
|
||||||
|
).map { (available, forks, workers) ->
|
||||||
|
dynamicTest("[$available, $forks, $workers]") {
|
||||||
|
assertThrows(IllegalArgumentException::class.java) {
|
||||||
|
TestHeapPolicy.calculateHeapMiB(available, forks, workers)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestFactory
|
||||||
|
fun shouldParseOnlyRawFreeAndInactivePages(): List<DynamicTest> = listOf(
|
||||||
|
dynamicTest("4K pages ignore speculative and file-backed counts") {
|
||||||
|
assertEquals(
|
||||||
|
2_371_584L,
|
||||||
|
TestHeapPolicy.parseMacVmStat(
|
||||||
|
"""
|
||||||
|
Mach Virtual Memory Statistics: (page size of 4096 bytes)
|
||||||
|
Pages free: 123.
|
||||||
|
Pages speculative: 100.
|
||||||
|
Pages inactive: 456.
|
||||||
|
File-backed pages: 999.
|
||||||
|
""".trimIndent()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
},
|
||||||
|
dynamicTest("16K pages accept reordered counters and whitespace") {
|
||||||
|
assertEquals(
|
||||||
|
81_920L,
|
||||||
|
TestHeapPolicy.parseMacVmStat(
|
||||||
|
" Mach Virtual Memory Statistics: (page size of 16384 bytes)\n" +
|
||||||
|
" Pages inactive : 3.\nPages active: 999.\nPages free:\t2.\n"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
},
|
||||||
|
dynamicTest("both raw counters may be zero") {
|
||||||
|
assertEquals(0L, TestHeapPolicy.parseMacVmStat(vmStat("4096", "0", "0")))
|
||||||
|
},
|
||||||
|
dynamicTest("counters need not have trailing periods") {
|
||||||
|
assertEquals(20_480L, TestHeapPolicy.parseMacVmStat(vmStat("4096", "2", "3").replace(".\n", "\n")))
|
||||||
|
},
|
||||||
|
dynamicTest("the largest representable byte count remains valid") {
|
||||||
|
assertEquals(Long.MAX_VALUE, TestHeapPolicy.parseMacVmStat(vmStat("1", "9223372036854775806", "1")))
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
@TestFactory
|
||||||
|
fun shouldRejectMissingDuplicateMalformedNegativeOrOverflowingVmStatFields(): List<DynamicTest> {
|
||||||
|
val valid = vmStat("4096", "2", "3")
|
||||||
|
return listOf(
|
||||||
|
"",
|
||||||
|
"Pages free: 2.\nPages inactive: 3.\n",
|
||||||
|
"page size of 4096 bytes\nPages free: 2.\nPages inactive: 3.\n",
|
||||||
|
valid.replace("Pages free: 2.\n", ""),
|
||||||
|
valid.replace("Pages inactive: 3.\n", ""),
|
||||||
|
valid + "Pages free: 2.\n",
|
||||||
|
valid + "Pages inactive: 3.\n",
|
||||||
|
valid + "Mach Virtual Memory Statistics: (page size of 4096 bytes)\n",
|
||||||
|
valid + "Pages free: broken.\n",
|
||||||
|
vmStat("4096", "-1", "3"),
|
||||||
|
vmStat("4096", "2", "-1"),
|
||||||
|
vmStat("-4096", "2", "3"),
|
||||||
|
vmStat("0", "2", "3"),
|
||||||
|
vmStat("invalid", "2", "3"),
|
||||||
|
vmStat("4096", "2x", "3"),
|
||||||
|
vmStat("4096", "2", "3.5"),
|
||||||
|
valid.replace("Pages free:", "Pages free"),
|
||||||
|
valid.replace("bytes)", "bytes) trailing"),
|
||||||
|
vmStat("9223372036854775808", "2", "3"),
|
||||||
|
vmStat("4096", "9223372036854775808", "3"),
|
||||||
|
vmStat("1", "9223372036854775807", "1"),
|
||||||
|
vmStat("4096", "2251799813685248", "0")
|
||||||
|
).map { output ->
|
||||||
|
dynamicTest("rejects ${output.replace('\n', ' ')}") {
|
||||||
|
assertThrows(IllegalArgumentException::class.java) { TestHeapPolicy.parseMacVmStat(output) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun shouldReadPositiveAvailabilityFromTheCurrentOperatingSystem() {
|
||||||
|
assertTrue(TestHeapPolicy.readAvailableBytes() > 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun vmStat(pageSize: String, free: String, inactive: String): String =
|
||||||
|
"Mach Virtual Memory Statistics: (page size of $pageSize bytes)\n" +
|
||||||
|
"Pages free: $free.\nPages inactive: $inactive.\n"
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val MIB = 1_048_576L
|
||||||
|
private const val GIB = 1_073_741_824L
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user