Skip to content

Commit 27535c1

Browse files
author
Ubuntu
committed
Revert "docs: update Java tracer documentation to match verified behavior"
This reverts commit 85e8a51.
1 parent 20f76a3 commit 27535c1

1 file changed

Lines changed: 13 additions & 32 deletions

File tree

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

Lines changed: 13 additions & 32 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-
Pass your Java command after `codeflash optimize`:
60+
To trace and optimize a running Java program, replace your `java` command with `codeflash optimize java`:
6161

6262
```bash
6363
# JAR application
@@ -66,14 +66,11 @@ 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 test suite
70-
codeflash optimize mvn test
71-
72-
# Gradle test suite
73-
codeflash optimize ./gradlew test
69+
# Maven exec
70+
codeflash optimize mvn exec:java -Dexec.mainClass="com.example.Main"
7471
```
7572

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

7875
```bash
7976
codeflash optimize --timeout 30 java -jar target/my-app.jar
@@ -224,27 +221,21 @@ The JavaScript tracer uses Babel instrumentation to capture function calls durin
224221
</Tab>
225222
<Tab title="Java">
226223

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

229226
1. **Trace and optimize a Java program**
230227

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

233230
```bash
234231
# JAR application
235232
codeflash optimize java -jar target/my-app.jar --app-args
236233

237234
# Class with classpath
238235
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
245236
```
246237

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.
238+
Codeflash will run your program twice (once for profiling, once for argument capture), generate JUnit replay tests, then optimize the most impactful functions.
248239

249240
2. **Long-running programs**
250241

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

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.
248+
Each stage runs for at most 30 seconds, then the program is terminated and captured data is processed.
258249

259250
3. **Trace only (no optimization)**
260251

261252
```bash
262253
codeflash optimize --trace-only java -jar target/my-app.jar
263254
```
264255

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`.
256+
This generates replay tests in `src/test/java/codeflash/replay/` without running the optimizer.
266257

267-
Options:
258+
More Options:
268259

269260
- `--timeout`: Maximum time (seconds) for each tracing stage.
270261
- `--max-function-count`: Maximum captures per method (default: 100).
271262

272263
<Info>
273264
**How the Java tracer works:**
274265

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.
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.
276267

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.
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.
278269

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.
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.
282271
</Info>
283272

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-
292273
</Tab>
293274
</Tabs>

0 commit comments

Comments
 (0)