-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenchmarkCommand.java
More file actions
299 lines (259 loc) Β· 13.1 KB
/
BenchmarkCommand.java
File metadata and controls
299 lines (259 loc) Β· 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package io.argus.cli.command;
import io.argus.cli.config.CliConfig;
import io.argus.cli.config.Messages;
import io.argus.cli.provider.ProviderRegistry;
import io.argus.cli.provider.jdk.JcmdExecutor;
import io.argus.cli.render.AnsiStyle;
import io.argus.cli.render.RichRenderer;
import io.argus.core.command.CommandGroup;
/**
* Lightweight sampling-based benchmark for a specific method in a running JVM.
*
* <p>Usage:
* <pre>
* argus benchmark <pid> com.example.Serializer.serialize --iterations=1000 --warmup=100
* </pre>
*
* <p>Implementation: starts a JFR recording, takes periodic thread dump samples to estimate
* how often the target method appears in stack traces, and computes throughput, GC overhead,
* and allocation rate from JFR events.
*/
public final class BenchmarkCommand implements Command {
private static final int WIDTH = RichRenderer.DEFAULT_WIDTH;
private static final int DEFAULT_DURATION_SEC = 30;
private static final int DEFAULT_SAMPLE_INTERVAL_MS = 100;
@Override public String name() { return "benchmark"; }
@Override public CommandGroup group() { return CommandGroup.PROFILING; }
@Override public CommandMode mode() { return CommandMode.WRITE; }
@Override public boolean supportsTui() { return false; }
@Override
public String description(Messages messages) {
return messages.get("cmd.benchmark.desc");
}
@Override
public void execute(String[] args, CliConfig config, ProviderRegistry registry, Messages messages) {
if (args.length < 2) {
printHelp(config.color(), messages);
return;
}
long pid;
try { pid = Long.parseLong(args[0]); }
catch (NumberFormatException e) { System.err.println(messages.get("error.pid.invalid", args[0])); return; }
String target = args[1];
if (!target.contains(".")) {
System.err.println(messages.get("error.benchmark.target.invalid", target));
return;
}
// Parse options
int durationSec = DEFAULT_DURATION_SEC;
int warmupSec = 5;
for (int i = 2; i < args.length; i++) {
String arg = args[i];
if (arg.startsWith("--iterations=")) {
// Iterations maps to duration: estimate 1 sample/100ms
try {
int iters = Integer.parseInt(arg.substring(13));
durationSec = Math.max(1, iters / 10);
} catch (NumberFormatException ignored) {}
} else if (arg.startsWith("--warmup=")) {
try {
int warmupIters = Integer.parseInt(arg.substring(9));
warmupSec = Math.max(1, warmupIters / 10);
} catch (NumberFormatException ignored) {}
} else if (arg.startsWith("--duration=")) {
try { durationSec = Integer.parseInt(arg.substring(11)); } catch (NumberFormatException ignored) {}
}
}
boolean useColor = config.color();
int totalSec = warmupSec + durationSec;
String recordingName = "argus-bench-" + pid;
System.out.print(RichRenderer.brandedHeader(useColor, "benchmark", messages.get("desc.benchmark")));
// Start JFR recording
System.out.println(messages.get("status.benchmark.starting", pid, target));
String jfrStartCmd = "JFR.start name=" + recordingName
+ " settings=profile"
+ " duration=" + totalSec + "s";
try {
JcmdExecutor.execute(pid, jfrStartCmd);
} catch (RuntimeException e) {
System.err.println(messages.get("error.benchmark.jfr.failed", e.getMessage()));
return;
}
// Warmup phase
if (warmupSec > 0) {
System.out.println(messages.get("status.benchmark.warmup", warmupSec));
sleepSec(warmupSec);
}
// Sampling phase: collect thread dumps and count target method hits
long startMs = System.currentTimeMillis();
long endMs = startMs + (long) durationSec * 1000;
int totalSamples = 0;
int targetHits = 0;
System.out.println(messages.get("status.benchmark.sampling", durationSec));
while (System.currentTimeMillis() < endMs) {
try {
String dump = JcmdExecutor.execute(pid, "Thread.print");
totalSamples++;
if (dump.contains(target)) {
targetHits++;
}
} catch (RuntimeException e) {
break;
}
sleepMs(DEFAULT_SAMPLE_INTERVAL_MS);
}
long actualMs = System.currentTimeMillis() - startMs;
double actualSec = actualMs / 1000.0;
// Stop JFR and get GC/allocation stats from recording output
String jfrDumpFile = recordingName + ".jfr";
GcStats gcStats = new GcStats();
AllocStats allocStats = new AllocStats();
try {
JcmdExecutor.execute(pid, "JFR.stop name=" + recordingName + " filename=" + jfrDumpFile);
// Parse JFR check output for basic stats (filename is best-effort)
String checkOut = JcmdExecutor.execute(pid, "JFR.check");
parseJfrSummary(checkOut, gcStats, allocStats);
} catch (RuntimeException ignored) {}
// Compute throughput estimate
double targetPct = totalSamples > 0 ? (double) targetHits / totalSamples * 100.0 : 0;
// Rough throughput: if method appears in X% of samples and each sample is 100ms window,
// estimate ops/s as inverse of estimated method duration
double estimatedOpsPerSec = totalSamples > 0 && actualSec > 0
? (double) targetHits / actualSec * 10.0
: 0;
renderResult(pid, target, durationSec, totalSamples, targetHits, targetPct,
estimatedOpsPerSec, gcStats, allocStats, actualSec, useColor, messages);
}
// -------------------------------------------------------------------------
// Rendering
// -------------------------------------------------------------------------
private void renderResult(long pid, String target, int durationSec, int totalSamples,
int targetHits, double targetPct, double estimatedOpsPerSec,
GcStats gcStats, AllocStats allocStats, double actualSec,
boolean c, Messages messages) {
System.out.println(RichRenderer.boxHeader(c, messages.get("header.benchmark"), WIDTH,
"pid:" + pid, (int) actualSec + "s"));
System.out.println(RichRenderer.emptyLine(WIDTH));
System.out.println(RichRenderer.boxLine(
" " + RichRenderer.padRight(messages.get("benchmark.label.target"), 20) + target, WIDTH));
System.out.println(RichRenderer.boxLine(
" " + RichRenderer.padRight(messages.get("benchmark.label.duration"), 20)
+ (int) actualSec + "s, "
+ messages.get("benchmark.label.samples") + ": " + totalSamples, WIDTH));
System.out.println(RichRenderer.emptyLine(WIDTH));
// Throughput
String opsStr = estimatedOpsPerSec > 0
? "~" + formatWithCommas((long) estimatedOpsPerSec) + " ops/s"
: messages.get("benchmark.insufficient.samples");
System.out.println(RichRenderer.boxLine(
" " + RichRenderer.padRight(messages.get("benchmark.label.throughput"), 24)
+ AnsiStyle.style(c, AnsiStyle.BOLD) + opsStr
+ AnsiStyle.style(c, AnsiStyle.RESET), WIDTH));
System.out.println(RichRenderer.boxLine(
" " + AnsiStyle.style(c, AnsiStyle.DIM)
+ messages.get("benchmark.label.sample.pct", String.format("%.0f", targetPct))
+ AnsiStyle.style(c, AnsiStyle.RESET), WIDTH));
// GC section
System.out.println(RichRenderer.emptyLine(WIDTH));
System.out.println(RichRenderer.boxLine(
" " + AnsiStyle.style(c, AnsiStyle.BOLD) + messages.get("benchmark.section.gc")
+ AnsiStyle.style(c, AnsiStyle.RESET), WIDTH));
if (gcStats.events >= 0) {
double gcOverheadPct = actualSec > 0 ? gcStats.pauseMs / (actualSec * 10.0) : 0;
System.out.println(RichRenderer.boxLine(
" " + RichRenderer.padRight(messages.get("benchmark.label.gc.events"), 10)
+ gcStats.events
+ " " + RichRenderer.padRight(messages.get("benchmark.label.gc.pause"), 8)
+ gcStats.pauseMs + "ms total"
+ " " + messages.get("benchmark.label.gc.overhead")
+ String.format("%.2f%%", gcOverheadPct), WIDTH));
} else {
System.out.println(RichRenderer.boxLine(
" " + AnsiStyle.style(c, AnsiStyle.DIM)
+ messages.get("benchmark.gc.unavailable")
+ AnsiStyle.style(c, AnsiStyle.RESET), WIDTH));
}
// Allocation section
System.out.println(RichRenderer.emptyLine(WIDTH));
System.out.println(RichRenderer.boxLine(
" " + AnsiStyle.style(c, AnsiStyle.BOLD) + messages.get("benchmark.section.alloc")
+ AnsiStyle.style(c, AnsiStyle.RESET), WIDTH));
if (allocStats.rateMBps >= 0) {
System.out.println(RichRenderer.boxLine(
" " + RichRenderer.padRight(messages.get("benchmark.label.alloc.rate"), 10)
+ String.format("%.0f MB/s", allocStats.rateMBps), WIDTH));
} else {
System.out.println(RichRenderer.boxLine(
" " + AnsiStyle.style(c, AnsiStyle.DIM)
+ messages.get("benchmark.alloc.unavailable")
+ AnsiStyle.style(c, AnsiStyle.RESET), WIDTH));
}
// Disclaimer
System.out.println(RichRenderer.emptyLine(WIDTH));
System.out.println(RichRenderer.boxLine(
" " + AnsiStyle.style(c, AnsiStyle.DIM)
+ messages.get("benchmark.note.sampling")
+ AnsiStyle.style(c, AnsiStyle.RESET), WIDTH));
System.out.println(RichRenderer.boxLine(
" " + AnsiStyle.style(c, AnsiStyle.DIM)
+ messages.get("benchmark.note.jmh")
+ AnsiStyle.style(c, AnsiStyle.RESET), WIDTH));
System.out.println(RichRenderer.boxFooter(c, null, WIDTH));
}
private void printHelp(boolean c, Messages messages) {
System.out.print(RichRenderer.brandedHeader(c, "benchmark", messages.get("cmd.benchmark.desc")));
System.out.println(RichRenderer.boxHeader(c, "Usage", WIDTH));
System.out.println(RichRenderer.boxLine("argus benchmark <pid> <class.method> [options]", WIDTH));
System.out.println(RichRenderer.emptyLine(WIDTH));
System.out.println(RichRenderer.boxLine(
AnsiStyle.style(c, AnsiStyle.BOLD) + "Options:"
+ AnsiStyle.style(c, AnsiStyle.RESET), WIDTH));
System.out.println(RichRenderer.boxLine(
RichRenderer.padRight(" --iterations=N", 30) + "Approximate iteration count (maps to duration)", WIDTH));
System.out.println(RichRenderer.boxLine(
RichRenderer.padRight(" --warmup=N", 30) + "Warmup iterations before measurement", WIDTH));
System.out.println(RichRenderer.boxLine(
RichRenderer.padRight(" --duration=N", 30) + "Measurement duration in seconds (default: 30)", WIDTH));
System.out.println(RichRenderer.boxFooter(c, null, WIDTH));
}
// -------------------------------------------------------------------------
// JFR parsing (best-effort from jcmd output)
// -------------------------------------------------------------------------
private void parseJfrSummary(String checkOutput, GcStats gcStats, AllocStats allocStats) {
// JFR check output doesn't provide GC stats directly; set unavailable
gcStats.events = -1;
gcStats.pauseMs = 0;
allocStats.rateMBps = -1;
}
// -------------------------------------------------------------------------
// Utilities
// -------------------------------------------------------------------------
private static void sleepSec(int seconds) {
sleepMs((long) seconds * 1000);
}
private static void sleepMs(long ms) {
try { Thread.sleep(ms); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
}
private static String formatWithCommas(long n) {
String s = String.valueOf(n);
if (s.length() <= 3) return s;
StringBuilder sb = new StringBuilder();
int rem = s.length() % 3;
if (rem > 0) sb.append(s, 0, rem);
for (int i = rem; i < s.length(); i += 3) {
if (sb.length() > 0) sb.append(',');
sb.append(s, i, i + 3);
}
return sb.toString();
}
// -------------------------------------------------------------------------
// Data holders
// -------------------------------------------------------------------------
private static final class GcStats {
int events = -1;
long pauseMs = 0;
}
private static final class AllocStats {
double rateMBps = -1;
}
}