This commit is contained in:
Kevin Turcios 2026-04-10 05:07:53 -05:00
parent e81f25f825
commit 01e22152c7
3 changed files with 53 additions and 5 deletions

View file

@ -1,5 +1,8 @@
package com.example;
import java.util.ArrayList;
import java.util.List;
public class Workload {
public static int computeSum(int n) {
@ -10,15 +13,53 @@ public class Workload {
return sum;
}
public static String repeatString(String s, int count) {
String result = "";
for (int i = 0; i < count; i++) {
result = result + s;
}
return result;
}
public static List<Integer> filterEvens(List<Integer> numbers) {
List<Integer> result = new ArrayList<>();
for (int n : numbers) {
if (n % 2 == 0) {
result.add(n);
}
}
return result;
}
public int instanceMethod(int x, int y) {
return x * y + computeSum(x);
}
public static void main(String[] args) {
// Run with large inputs so JFR can capture CPU samples.
// Run methods with large inputs so JFR can capture CPU samples.
// Small inputs finish too fast (<1ms) for JFR's 10ms sampling interval.
for (int round = 0; round < 100; round++) {
for (int round = 0; round < 1000; round++) {
computeSum(100_000);
repeatString("hello world ", 1000);
List<Integer> nums = new ArrayList<>();
for (int i = 1; i <= 10_000; i++) nums.add(i);
filterEvens(nums);
Workload w = new Workload();
w.instanceMethod(100_000, 42);
}
// Also call with small inputs for variety in traced args
System.out.println("computeSum(100) = " + computeSum(100));
System.out.println("repeatString(\"ab\", 3) = " + repeatString("ab", 3));
List<Integer> small = new ArrayList<>();
for (int i = 1; i <= 10; i++) small.add(i);
System.out.println("filterEvens(1..10) = " + filterEvens(small));
Workload w = new Workload();
System.out.println("instanceMethod(5, 3) = " + w.instanceMethod(5, 3));
System.out.println("Workload complete.");
}

View file

@ -81,11 +81,14 @@ class TestTracingAgent:
conn = sqlite3.connect(str(trace_db))
try:
rows = conn.execute("SELECT function, classname, descriptor, length(args) FROM function_calls").fetchall()
assert len(rows) >= 2, f"Expected at least 2 captured invocations, got {len(rows)}"
assert len(rows) >= 5, f"Expected at least 5 captured invocations, got {len(rows)}"
# Check that specific methods were captured
functions = {row[0] for row in rows}
assert "computeSum" in functions
assert "repeatString" in functions
assert "filterEvens" in functions
assert "instanceMethod" in functions
# Verify all rows have non-empty args blobs
for row in rows:
@ -94,7 +97,7 @@ class TestTracingAgent:
# Verify metadata
metadata = dict(conn.execute("SELECT key, value FROM metadata").fetchall())
assert "totalCaptures" in metadata
assert int(metadata["totalCaptures"]) >= 2
assert int(metadata["totalCaptures"]) >= 5
finally:
conn.close()
@ -133,7 +136,7 @@ class TestTracingAgent:
conn = sqlite3.connect(str(trace_db))
try:
# computeSum is called 2 times (direct calls in main)
# computeSum is called 4 times (2 direct + 2 from instanceMethod)
compute_count = conn.execute(
"SELECT COUNT(*) FROM function_calls WHERE function = 'computeSum'"
).fetchone()[0]
@ -196,6 +199,7 @@ class TestReplayTestGeneration:
assert "import org.junit.jupiter.api.Test;" in content
assert "ReplayHelper" in content
assert "replay_computeSum_0" in content
assert "replay_repeatString_0" in content
def test_metadata_parsing(self, compiled_workload: Path, trace_db: Path, tmp_path: Path) -> None:
"""Test that metadata comments are correctly parsed from generated tests."""
@ -292,6 +296,7 @@ class TestJavaTracerOrchestration:
assert len(workload_files) == 1
content = workload_files[0].read_text(encoding="utf-8")
assert "replay_computeSum" in content
assert "replay_instanceMethod" in content
def test_package_detection(self) -> None:
"""Test that package detection finds Java packages from source files."""

View file

@ -88,6 +88,7 @@ class TestFunctionDiscoveryFromReplayTests:
assert func.file_path == file_path
assert "computeSum" in all_func_names
assert "repeatString" in all_func_names
def test_discover_tests_for_replay_tests(self, traced_workload: tuple) -> None:
"""Test that test discovery maps replay tests to source functions."""
@ -111,6 +112,7 @@ class TestFunctionDiscoveryFromReplayTests:
matched_func_names.add(func_name)
assert "computeSum" in matched_func_names, f"computeSum not found in: {result.keys()}"
assert "repeatString" in matched_func_names, f"repeatString not found in: {result.keys()}"
# Each function should have at least one test
for func_name, test_infos in result.items():