build(test): 가용 메모리로 테스트 힙을 산정하고 워커 수명을 제한한다
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
plugins {
|
||||
java
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
java {
|
||||
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.2")
|
||||
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.2")
|
||||
}
|
||||
|
||||
tasks.test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
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,149 @@
|
||||
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";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user