Skip to content

Commit e0a9ca7

Browse files
committed
Update Java optimization instructions and add support for Maven/Gradle test suites
1 parent 94e1b02 commit e0a9ca7

1 file changed

Lines changed: 35 additions & 20 deletions

File tree

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

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,13 @@ codeflash optimize --language javascript script.js
6060
To trace and optimize a running Java program, replace your `java` command with `codeflash optimize java`:
6161

6262
```bash
63+
cd /path/to/your/java/project
64+
6365
# JAR application
64-
codeflash optimize java -jar target/my-app.jar --app-args
66+
codeflash optimize java -jar target/my-app.jar
6567

6668
# Class with classpath
6769
codeflash optimize java -cp target/classes com.example.Main
68-
69-
# Maven exec
70-
codeflash optimize mvn exec:java -Dexec.mainClass="com.example.Main"
7170
```
7271

7372
For long-running programs (servers, benchmarks), use `--timeout` to limit each tracing stage:
@@ -229,15 +228,36 @@ The Java tracer uses a **two-stage approach**: JFR (Java Flight Recorder) for ac
229228

230229
```bash
231230
# JAR application
232-
codeflash optimize java -jar target/my-app.jar --app-args
231+
codeflash optimize java -jar target/my-app.jar
233232

234233
# Class with classpath
235234
codeflash optimize java -cp target/classes com.example.Main
236235
```
237236

238237
Codeflash will run your program twice (once for profiling, once for argument capture), generate JUnit replay tests, then optimize the most impactful functions.
239238

240-
2. **Long-running programs**
239+
2. **With Maven / Gradle test suites**
240+
241+
```bash
242+
# Maven
243+
codeflash optimize mvn test
244+
245+
# Gradle
246+
codeflash optimize ./gradlew :module:cleanTest :module:test
247+
```
248+
249+
3. **Multi-module projects**
250+
251+
For multi-module builds, point Codeflash at the correct source and test roots:
252+
253+
```bash
254+
codeflash \
255+
--module-root src/main/java \
256+
--tests-root src/test/java \
257+
optimize ./gradlew :my-module:cleanTest :my-module:test
258+
```
259+
260+
4. **Long-running programs**
241261

242262
For servers, benchmarks, or programs that don't terminate on their own, use `--timeout` to limit each tracing stage:
243263

@@ -247,28 +267,23 @@ The Java tracer uses a **two-stage approach**: JFR (Java Flight Recorder) for ac
247267

248268
Each stage runs for at most 30 seconds, then the program is terminated and captured data is processed.
249269

250-
3. **Trace only (no optimization)**
270+
5. **Trace only (no optimization)**
251271

252272
```bash
253-
codeflash optimize --trace-only java -jar target/my-app.jar
273+
codeflash optimize --trace-only java -cp target/classes com.example.Main
254274
```
255275

256276
This generates replay tests in `src/test/java/codeflash/replay/` without running the optimizer.
257277

258-
More Options:
259-
260-
- `--timeout`: Maximum time (seconds) for each tracing stage.
261-
- `--max-function-count`: Maximum captures per method (default: 100).
262-
263-
<Info>
264-
**How the Java tracer works:**
265-
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.
278+
Options:
267279

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.
280+
| Option | Description | Default |
281+
|--------|-------------|---------|
282+
| `--timeout` | Maximum time (seconds) for each tracing stage | Indefinite |
283+
| `--max-function-count` | Maximum captures per method | 100 |
284+
| `--trace-only` | Generate replay tests without running the optimizer | Off |
285+
| `--no-pr` | Skip PR creation and keep changes local | Off |
269286

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.
271-
</Info>
272287

273288
</Tab>
274289
</Tabs>

0 commit comments

Comments
 (0)