rolldown_devtools_metrics 1.2.19

In-memory aggregation of Rolldown devtools events into an agent-readable metrics report
Documentation
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! Unit tests: fold synthetic devtools events (the same JSON shapes the writer receives) with
//! fixed emit-time instants, then assert on the report model, the delta, the history line, and
//! snapshot the rendered views. Everything is deterministic — no wall clock reaches a snapshot.

use std::time::{Duration, Instant};

use serde_json::json;

use crate::{MetricsAggregator, MetricsConfig, render, report::MetricValue};

fn agg() -> MetricsAggregator {
  MetricsAggregator::new(MetricsConfig { dir: "ignored".to_string(), top_n: 20, delta: true })
}

fn at(base: Instant, ms: u64) -> Instant {
  base + Duration::from_millis(ms)
}

fn session_meta() -> serde_json::Value {
  json!({
    "action": "SessionMeta",
    "inputs": [{"name": "a"}, {"name": "b"}],
    "plugins": [{"name": "test-plugin"}],
    "cwd": "/app",
    "platform": "node",
    "format": "esm",
  })
}

fn module_graph() -> serde_json::Value {
  json!({
    "action": "ModuleGraphReady",
    "modules": [
      {
        "id": "/app/src/a.ts",
        "imports": [
          {"kind": "import-statement", "module_id": "/app/src/shared.ts"},
          {"kind": "import-statement", "module_id": "/app/node_modules/foo/index.js"},
        ],
      },
      {
        "id": "/app/src/b.ts",
        "imports": [
          {"kind": "import-statement", "module_id": "/app/src/shared.ts"},
          {"kind": "dynamic-import", "module_id": "/app/src/lazy.ts"},
        ],
      },
      {
        "id": "/app/src/shared.ts",
        "importers": ["/app/src/a.ts", "/app/src/b.ts"],
        "imports": [{"kind": "import-statement", "module_id": "ext"}],
      },
      {"id": "/app/node_modules/foo/index.js", "importers": ["/app/src/a.ts"]},
      {"id": "/app/src/lazy.ts", "importers": ["/app/src/b.ts"]},
      {"id": "ext", "is_external": true, "importers": ["/app/src/shared.ts"]},
    ],
  })
}

fn chunk_graph() -> serde_json::Value {
  json!({
    "action": "ChunkGraphReady",
    "chunks": [
      {
        "chunk_id": 0,
        "name": "a",
        "is_user_defined_entry": true,
        "entry_module": "/app/src/a.ts",
        "modules": ["/app/src/a.ts"],
        "reason": "entry",
        "imports": [{"chunk_id": 2, "kind": "import-statement"}],
      },
      {
        "chunk_id": 1,
        "name": "b",
        "is_user_defined_entry": true,
        "entry_module": "/app/src/b.ts",
        "modules": ["/app/src/b.ts"],
        "reason": "entry",
        "imports": [
          {"chunk_id": 2, "kind": "import-statement"},
          {"chunk_id": 3, "kind": "dynamic-import"},
        ],
      },
      {
        "chunk_id": 2,
        "name": "shared",
        "modules": ["/app/src/shared.ts", "/app/node_modules/foo/index.js"],
        "reason": "common",
        "imports": [],
      },
      {
        "chunk_id": 3,
        "name": "lazy",
        "is_async_entry": true,
        "entry_module": "/app/src/lazy.ts",
        "modules": ["/app/src/lazy.ts"],
        "reason": "dynamic-entry",
        "imports": [],
      },
    ],
  })
}

fn package_graph() -> serde_json::Value {
  json!({
    "action": "PackageGraphReady",
    "packages": [
      {"name": "foo", "version": "1.0.0", "is_used": true, "dependency_type": "direct", "size": 1200, "modules": ["/app/node_modules/foo/index.js"]},
      {"name": "foo", "version": "2.0.0", "is_used": true, "dependency_type": "transitive", "size": 300, "modules": []},
      {"name": "bar", "version": "1.0.0", "is_used": false, "dependency_type": "transitive", "size": 50, "modules": []},
    ],
  })
}

fn module_rendered() -> serde_json::Value {
  json!({
    "action": "ModuleRenderedReady",
    "modules": [
      {"id": "/app/src/a.ts", "bytes": 100},
      {"id": "/app/src/b.ts", "bytes": 150},
      {"id": "/app/src/shared.ts", "bytes": 700},
      {"id": "/app/node_modules/foo/index.js", "bytes": 1200},
      {"id": "/app/src/lazy.ts", "bytes": 500},
    ],
  })
}

fn assets(a_size: u32, b_size: u32) -> serde_json::Value {
  json!({
    "action": "AssetsReady",
    "assets": [
      {"chunk_id": 0, "size": a_size, "filename": "dist/a.js"},
      {"chunk_id": 1, "size": b_size, "filename": "dist/b.js"},
      {"chunk_id": 2, "size": 3000, "filename": "dist/shared.js"},
      {"chunk_id": 3, "size": 500, "filename": "dist/lazy.js"},
      {"size": 400, "filename": "dist/style.css"},
      {"size": 99999, "filename": "dist/a.js.map"},
    ],
  })
}

/// Fold one complete build (the real event order, including the double `BuildStart` the bundle
/// emits) spanning `[start_ms, start_ms + 100]`.
fn fold_build(agg: &mut MetricsAggregator, base: Instant, start_ms: u64, a_size: u32, b_size: u32) {
  let t = |offset: u64| at(base, start_ms + offset);
  agg.fold(&session_meta(), t(0));
  agg.fold(&json!({"action": "BuildStart"}), t(0));
  agg.fold(&json!({"action": "BuildStart"}), t(0));
  // c1: resolveId that resolved (did work).
  agg.fold(
    &json!({"action": "HookResolveIdCallStart", "call_id": "c1", "plugin_name": "test-plugin"}),
    t(5),
  );
  agg.fold(
    &json!({"action": "HookResolveIdCallEnd", "call_id": "c1", "resolved_id": "/app/src/shared.ts"}),
    t(7),
  );
  // c2: transform that changed the code (did work).
  agg.fold(
    &json!({
      "action": "HookTransformCallStart",
      "call_id": "c2",
      "plugin_name": "test-plugin",
      "module_id": "/app/src/a.ts",
      "content": "let x = 1;",
    }),
    t(10),
  );
  agg.fold(
    &json!({
      "action": "HookTransformCallEnd",
      "call_id": "c2",
      "content": "let x = 1; export {};",
    }),
    t(15),
  );
  // c3: resolveId early return (no-op).
  agg.fold(
    &json!({"action": "HookResolveIdCallStart", "call_id": "c3", "plugin_name": "test-plugin"}),
    t(20),
  );
  agg.fold(&json!({"action": "HookResolveIdCallEnd", "call_id": "c3", "resolved_id": null}), t(21));
  // c4: transform that returned the code unchanged (no-op) — End always carries Some(content).
  agg.fold(
    &json!({
      "action": "HookTransformCallStart",
      "call_id": "c4",
      "plugin_name": "test-plugin",
      "module_id": "/app/src/b.ts",
      "content": "export const b = 2;",
    }),
    t(25),
  );
  agg.fold(
    &json!({
      "action": "HookTransformCallEnd",
      "call_id": "c4",
      "content": "export const b = 2;",
    }),
    t(27),
  );
  // c5: load early return (no-op).
  agg.fold(
    &json!({"action": "HookLoadCallStart", "call_id": "c5", "plugin_name": "test-plugin", "module_id": "/app/src/lazy.ts"}),
    t(30),
  );
  agg.fold(&json!({"action": "HookLoadCallEnd", "call_id": "c5", "content": null}), t(31));
  agg.fold(&module_graph(), t(55));
  agg.fold(&json!({"action": "BuildEnd"}), t(60));
  agg.fold(&chunk_graph(), t(70));
  agg.fold(&module_rendered(), t(72));
  agg.fold(&package_graph(), t(75));
  agg.fold(&assets(a_size, b_size), t(95));
  agg.fold(&json!({"action": "BuildEnd"}), t(100));
}

fn int(v: u64) -> MetricValue {
  MetricValue::Int(v)
}

#[test]
fn report_metrics_and_sections() {
  let base = Instant::now();
  let mut agg = agg();
  fold_build(&mut agg, base, 0, 1000, 800);
  let report = agg.build_report(None, None);

  let m = |id: &str| report.metrics.get(id).copied().unwrap();
  assert_eq!(m("build.total_ms"), MetricValue::Float(100.0));
  assert_eq!(m("build.scan_ms"), MetricValue::Float(60.0));
  assert_eq!(m("build.generate_ms"), MetricValue::Float(40.0));
  // 1000 + 800 + 3000 + 500 + 400; the .map asset is excluded.
  assert_eq!(m("output.total_bytes"), int(5700));
  assert_eq!(m("output.js_bytes"), int(5300));
  assert_eq!(m("output.css_bytes"), int(400));
  assert_eq!(m("output.chunk_count"), int(4));
  assert_eq!(m("output.asset_count"), int(5));
  // Entries: a, b (user-defined) + lazy (async entry).
  assert_eq!(m("output.entry_count"), int(3));
  // a: 1000 + shared 3000 = 4000 (dynamic import excluded from b's 800 + 3000).
  assert_eq!(m("output.max_initial_load_bytes"), int(4000));
  assert_eq!(m("modules.count"), int(6));
  assert_eq!(m("modules.external_count"), int(1));
  // shared.ts + ext (via shared) reachable from entries a and b; lazy.ts from entry b's
  // dynamic import and from its own async entry.
  assert_eq!(m("modules.shared_across_entries_count"), int(3));
  assert_eq!(m("chunks.duplicated_module_count"), int(0));
  assert_eq!(m("packages.count"), int(3));
  assert_eq!(m("packages.duplicate_version_count"), int(1));
  assert_eq!(m("plugins.hook_call_count"), int(5));
  // c3 (resolveId returned nothing) + c4 (transform returned unchanged code) + c5 (load
  // returned nothing); c1/c2 did work.
  assert_eq!(m("plugins.hook_noop_call_count"), int(3));
  assert_eq!(m("plugins.hook_time_ms"), MetricValue::Float(11.0));

  let plugin = &report.plugins[0];
  assert_eq!(plugin.name, "test-plugin");
  assert_eq!(plugin.noop_calls, 3);
  assert_eq!(plugin.hooks["resolveId"].calls, 2);
  assert_eq!(plugin.hooks["resolveId"].noop_calls, 1);
  assert_eq!(plugin.hooks["transform"].calls, 2);
  assert_eq!(plugin.hooks["transform"].noop_calls, 1);
  assert_eq!(plugin.hooks["load"].calls, 1);
  assert_eq!(plugin.hooks["load"].noop_calls, 1);

  assert_eq!(report.session.build_index, 1);
  let entry_a = &report.entries[0];
  assert_eq!(entry_a.entry, "src/a.ts");
  assert_eq!(entry_a.chunk, "dist/a.js");
  assert_eq!(entry_a.chunk_bytes, 1000);
  assert_eq!(entry_a.initial_load_bytes, 4000);
  assert_eq!(entry_a.static_imports, vec!["dist/shared.js"]);
  assert_eq!(report.transform_hotspots.len(), 2);
  assert_eq!(report.transform_hotspots[0].module, "src/a.ts");
  assert_eq!(report.transform_hotspots[0].ms, 5.0);
  assert_eq!(report.transform_hotspots[1].module, "src/b.ts");
  assert!(report.delta.is_none());

  // Dominator retained sizes: foo is only reachable through a (retained under a); shared is a
  // join of both entries (hangs off the virtual root); lazy is dynamic-only; ext weighs 0.
  let graph = report.graph.as_ref().expect("module graph events were folded");
  assert_eq!(graph.entry_modules, vec!["src/a.ts", "src/b.ts"]);
  assert_eq!(graph.static_module_count, 5); // a, b, shared, foo, ext
  assert_eq!(graph.static_bytes, 2150); // 100 + 150 + 700 + 1200 + 0
  assert_eq!(graph.dynamic_only_module_count, 1); // lazy.ts
  let rows: Vec<(&str, u64, Option<&str>)> = graph
    .retained_top
    .iter()
    .map(|r| (r.module.as_str(), r.retained_bytes, r.via.as_deref()))
    .collect();
  assert_eq!(rows, vec![("foo/index.js", 1200, Some("src/a.ts")), ("src/shared.ts", 700, None)]);
}

#[test]
fn report_json_snapshot() {
  let base = Instant::now();
  let mut agg = agg();
  fold_build(&mut agg, base, 0, 1000, 800);
  let report = agg.build_report(None, None);
  insta::assert_snapshot!(serde_json::to_string_pretty(&report).unwrap());
}

#[test]
fn markdown_snapshots() {
  let base = Instant::now();
  let mut agg = agg();
  fold_build(&mut agg, base, 0, 1000, 800);
  let report = agg.build_report(None, None);
  insta::assert_snapshot!("entry_md", render::render_entry(&report));
  insta::assert_snapshot!("timing_md", render::render_timing(&report));
  insta::assert_snapshot!("graph_md", render::render_graph(&report));
  insta::assert_snapshot!("packages_md", render::render_packages(&report));
}

#[test]
fn delta_between_two_builds() {
  let base = Instant::now();
  let mut agg = agg();
  fold_build(&mut agg, base, 0, 1000, 800);
  let state = agg.build_report(None, None).to_state();

  // Second build in the same session: entry chunk `a` grows by 500 bytes.
  fold_build(&mut agg, base, 1000, 1500, 800);
  let report = agg.build_report(Some(&state), None);

  assert_eq!(report.session.build_index, 2);
  let delta = report.delta.as_ref().unwrap();
  let total = delta.metrics.get("output.total_bytes").unwrap();
  assert_eq!(total.prev, int(5700));
  assert_eq!(total.curr, int(6200));
  assert_eq!(total.delta, 500.0);
  assert_eq!(total.pct, Some(8.8));
  // Unchanged metric: pct present and zero (prev is nonzero).
  let modules = delta.metrics.get("modules.count").unwrap();
  assert_eq!(modules.delta, 0.0);
  assert_eq!(modules.pct, Some(0.0));

  let entry_a = delta.entries.iter().find(|e| e.entry == "src/a.ts").unwrap();
  assert_eq!(entry_a.initial_load_bytes.prev, int(4000));
  assert_eq!(entry_a.initial_load_bytes.curr, int(4500));
  assert_eq!(entry_a.initial_load_bytes.pct, Some(12.5));
  assert!(delta.added_entries.is_empty());
  assert!(delta.removed_entries.is_empty());

  insta::assert_snapshot!("delta_md", render::render_delta(&report));
}

#[test]
fn baseline_delta_stays_pinned_across_builds() {
  let base = Instant::now();
  let mut agg = agg();
  fold_build(&mut agg, base, 0, 1000, 800);
  // Pin build 1 as the baseline (the file-system equivalent: copy .state.json -> baseline.json).
  let baseline = agg.build_report(None, None).to_state();

  // Experiment A.
  fold_build(&mut agg, base, 1000, 1500, 800);
  let prev = agg.build_report(None, None).to_state();

  // Experiment B: chain delta compares vs A, baseline delta still compares vs build 1.
  fold_build(&mut agg, base, 2000, 2000, 800);
  let report = agg.build_report(Some(&prev), Some(&baseline));

  let chain = report.delta.as_ref().unwrap().metrics.get("output.total_bytes").unwrap();
  assert_eq!(chain.prev, int(6200));
  assert_eq!(chain.curr, int(6700));
  let pinned = report.baseline_delta.as_ref().unwrap().metrics.get("output.total_bytes").unwrap();
  assert_eq!(pinned.prev, int(5700));
  assert_eq!(pinned.curr, int(6700));
  assert_eq!(pinned.pct, Some(17.5));

  insta::assert_snapshot!("delta_md_with_baseline", render::render_delta(&report));
}

#[test]
fn double_build_start_counts_one_build() {
  let base = Instant::now();
  let mut agg = agg();
  agg.fold(&session_meta(), at(base, 0));
  agg.fold(&json!({"action": "BuildStart"}), at(base, 0));
  agg.fold(&json!({"action": "BuildStart"}), at(base, 0));
  let report = agg.build_report(None, None);
  assert_eq!(report.session.build_index, 1);
  // Incomplete build: no history line.
  assert!(agg.history_line(123).is_none());
}

#[test]
fn history_line_after_completed_build() {
  let base = Instant::now();
  let mut agg = agg();
  fold_build(&mut agg, base, 0, 1000, 800);
  let line = agg.history_line(1234).unwrap();
  assert_eq!(line.build, 1);
  assert_eq!(line.ts_ms, 1234);
  assert_eq!(line.metrics.get("output.total_bytes").copied().unwrap(), int(5700));
  assert_eq!(line.entries.len(), 3);
  // One line = one JSON object, safe to append to history.jsonl.
  let json = serde_json::to_string(&line).unwrap();
  assert!(!json.contains('\n'));
}

#[test]
fn state_survives_roundtrip_and_self_delta_is_zero() {
  let base = Instant::now();
  let mut agg = agg();
  fold_build(&mut agg, base, 0, 1000, 800);
  let state = agg.build_report(None, None).to_state();
  let raw = serde_json::to_string(&state).unwrap();
  let parsed: crate::report::MetricsState = serde_json::from_str(&raw).unwrap();
  let report = agg.build_report(Some(&parsed), None);
  let delta = report.delta.as_ref().unwrap();
  assert!(delta.metrics.values().all(|d| d.delta == 0.0));
}

#[test]
fn report_dir_is_one_folder_per_output() {
  use crate::report_dir_for;
  let cwd = std::env::current_dir().unwrap().join("app");
  let cwd = cwd.as_path();
  let absolute = cwd.join("build").join("server");
  // Rolldown's default output folder.
  assert_eq!(report_dir_for(cwd, None, None), "node_modules/.rolldown/report/dist");
  // One folder per output: a client and an SSR build stay apart.
  assert_eq!(
    report_dir_for(cwd, Some("dist/client"), None),
    "node_modules/.rolldown/report/dist/client"
  );
  assert_eq!(report_dir_for(cwd, Some("./dist/"), None), "node_modules/.rolldown/report/dist");
  // An absolute output folder under cwd is made relative (Vite passes absolute paths).
  assert_eq!(
    report_dir_for(cwd, Some(absolute.to_str().unwrap()), None),
    "node_modules/.rolldown/report/build/server"
  );
  // `file` wins over `dir`, and its folder is the output folder.
  assert_eq!(
    report_dir_for(cwd, Some("ignored"), Some("out/bundle.js")),
    "node_modules/.rolldown/report/out"
  );
  // Output in cwd itself: the report root.
  assert_eq!(report_dir_for(cwd, None, Some("bundle.js")), "node_modules/.rolldown/report");
  assert_eq!(report_dir_for(cwd, Some("."), None), "node_modules/.rolldown/report");
  // Outside cwd: `..` becomes `__`, so the report never leaves the report root.
  assert_eq!(
    report_dir_for(cwd, Some("../shared/dist"), None),
    "node_modules/.rolldown/report/__/shared/dist"
  );
}