Skip to content

Commit 85e8a51

Browse files
HeshamHM28claude
authored andcommitted
docs: update Java tracer documentation to match verified behavior
Add mvn/gradle test suite examples, fix replay test description, document current limitations (void methods, mvnw search, --add-opens). Remove unverified claims. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c9c5f9d commit 85e8a51

1 file changed

Lines changed: 32 additions & 13 deletions

File tree

docs/optimizing-with-codeflash/trace-and-optimize.mdx

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ codeflash optimize --language javascript script.js
5757
```
5858
</Tab>
5959
<Tab title="Java">
60-
To trace and optimize a running Java program, replace your `java` command with `codeflash optimize java`:
60+
Pass your Java command after `codeflash optimize`:
6161

6262
```bash
6363
# JAR application
@@ -66,11 +66,14 @@ codeflash optimize java -jar target/my-app.jar --app-args
6666
# Class with classpath
6767
codeflash optimize java -cp target/classes com.example.Main
6868

69-
# Maven exec
70-
codeflash optimize mvn exec:java -Dexec.mainClass="com.example.Main"
69+
# Maven test suite
70+
codeflash optimize mvn test
71+
72+
# Gradle test suite
73+
codeflash optimize ./gradlew test
7174
```
7275

73-
For long-running programs (servers, benchmarks), use `--timeout` to limit each tracing stage:
76+
For long-running programs, use `--timeout` to limit each tracing stage:
7477

7578
```bash
7679
codeflash optimize --timeout 30 java -jar target/my-app.jar
@@ -221,21 +224,27 @@ The JavaScript tracer uses Babel instrumentation to capture function calls durin
221224
</Tab>
222225
<Tab title="Java">
223226

224-
The Java tracer uses a **two-stage approach**: JFR (Java Flight Recorder) for accurate profiling, then a bytecode instrumentation agent for argument capture.
227+
The Java tracer uses a **two-stage approach**: JFR (Java Flight Recorder) for profiling, then a bytecode instrumentation agent for argument capture.
225228

226229
1. **Trace and optimize a Java program**
227230

228-
Replace your `java` command with `codeflash optimize java`:
231+
Pass your Java command after `codeflash optimize`:
229232

230233
```bash
231234
# JAR application
232235
codeflash optimize java -jar target/my-app.jar --app-args
233236

234237
# Class with classpath
235238
codeflash optimize java -cp target/classes com.example.Main
239+
240+
# Maven test suite
241+
codeflash optimize mvn test
242+
243+
# Gradle test suite
244+
codeflash optimize ./gradlew test
236245
```
237246

238-
Codeflash will run your program twice (once for profiling, once for argument capture), generate JUnit replay tests, then optimize the most impactful functions.
247+
Codeflash runs your command twice (once for profiling, once for argument capture), generates JUnit replay tests from captured inputs, then optimizes the highest-impact functions.
239248

240249
2. **Long-running programs**
241250

@@ -245,30 +254,40 @@ The Java tracer uses a **two-stage approach**: JFR (Java Flight Recorder) for ac
245254
codeflash optimize --timeout 30 java -jar target/my-benchmark.jar
246255
```
247256

248-
Each stage runs for at most 30 seconds, then the program is terminated and captured data is processed.
257+
Each stage runs for at most the specified seconds, then the program receives SIGTERM (allowing JFR dump and shutdown hooks to run), followed by SIGKILL if it doesn't exit within 5 seconds.
249258

250259
3. **Trace only (no optimization)**
251260

252261
```bash
253262
codeflash optimize --trace-only java -jar target/my-app.jar
254263
```
255264

256-
This generates replay tests in `src/test/java/codeflash/replay/` without running the optimizer.
265+
This generates replay tests in `src/test/java/codeflash/replay/` without running the optimizer. You can inspect the generated tests and run the optimizer later with `codeflash --replay-test path/to/ReplayTest.java`.
257266

258-
More Options:
267+
Options:
259268

260269
- `--timeout`: Maximum time (seconds) for each tracing stage.
261270
- `--max-function-count`: Maximum captures per method (default: 100).
262271

263272
<Info>
264273
**How the Java tracer works:**
265274

266-
- **Stage 1 (JFR)**: Runs your program with Java Flight Recorder enabled. JFR is built into the JVM (Java 11+), has ~1% overhead, and doesn't interfere with JIT compilation. This produces accurate method-level CPU profiling data.
275+
- **Stage 1 (JFR profiling)**: Runs your command with Java Flight Recorder enabled via `JAVA_TOOL_OPTIONS`. JFR is built into the JVM (Java 11+) and produces method-level CPU sampling data used to rank functions by impact.
267276

268-
- **Stage 2 (Agent)**: Runs your program with a bytecode instrumentation agent injected via `JAVA_TOOL_OPTIONS`. The agent intercepts method entry points, serializes arguments using Kryo, and writes them to an SQLite database. A 500ms timeout per serialization prevents hangs on complex object graphs.
277+
- **Stage 2 (argument capture)**: Runs your command again with a bytecode instrumentation agent (`codeflash-runtime`) injected via `JAVA_TOOL_OPTIONS`. The agent uses ASM to intercept method entries, serializes arguments with Kryo, and writes them to an SQLite database. A per-serialization timeout prevents hangs on complex object graphs. Constructors and synthetic methods are skipped.
269278

270-
- **Replay Tests**: Generated JUnit 5 test classes that deserialize captured arguments and invoke the original methods via reflection. These tests exercise your code with real-world inputs.
279+
- **Replay tests**: Generated JUnit test classes (JUnit 5 by default, JUnit 4 if detected in the project) that use `ReplayHelper` to deserialize captured arguments from the trace database and invoke the original methods. Each captured invocation becomes a separate test method. These tests exercise your code with the exact inputs observed during tracing.
280+
281+
- **Optimization**: Functions are ranked by JFR addressable time. For each function, Codeflash generates optimization candidates, verifies correctness against both replay tests and AI-generated regression tests, then benchmarks performance to select the best candidate.
271282
</Info>
272283

284+
<Warning>
285+
**Current limitations:**
286+
287+
- The tracer captures arguments for methods that return values. Void methods are traced for profiling (Stage 1) but argument capture may not produce usable replay tests for them.
288+
- The `--add-opens` JVM flags are automatically injected for Java 16+ compatibility with Kryo serialization.
289+
- For Maven projects, the Maven wrapper (`mvnw`) is preferred over system Maven. Codeflash searches parent directories to find it in multi-module projects.
290+
</Warning>
291+
273292
</Tab>
274293
</Tabs>

0 commit comments

Comments
 (0)