oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.13k stars 2.68k forks source link

Checking bun heap with Safari inspector fails #13345

Open bjesuiter opened 1 month ago

bjesuiter commented 1 month ago

What is the type of issue?

Example code is not working

What is the issue?

When I use this example code:

import { generateHeapSnapshot } from "bun";

const snapshot = generateHeapSnapshot();
await Bun.write("heap.json", JSON.stringify(snapshot, null, 2));

the generated json file cannot be imported into the Safari Timeline view, it produces this error:

Importfehler bei Timeline-Aufnahme: Ungültige JSON

=> Apparently the JSON is incorrect.

My Code:

bufferedWatch.subscribe(async (allChangedFiles) => {
  const dateString = format(new Date(), 'yyyy-MM-dd HH:mm:ss:SSS');
  const dateFileString = format(new Date(), 'yyyy-MM-dd HH_mm_ss_SSS');
  console.log(`${dateString} Changed files:`, allChangedFiles.length);

  const snapshot = generateHeapSnapshot();
  await Bun.write(
    `heap-${dateFileString}.json`,
    JSON.stringify(snapshot, null, 2),
  );
  console.log('Wrote heap snapshot for ${dateFileString}');

 // some other code
  }

My Heap Files

heap-2024-08-16 16_24_26_551.json

heap-2024-08-16 16_24_26_560.json

Where did you find it?

https://bun.sh/docs/project/benchmarking#javascript-heap-stats

Jarred-Sumner commented 1 month ago

Try bun --inspect

mstuercke commented 2 weeks ago

Looks like some wrapper object is missing. This makes it valid and shows some information, but it's still not what it should be:

await Bun.write(
  `heap-${dateFileString}.json`,
  JSON.stringify({
    version: 1,
    recording: {
      displayName: "Recording",
      discontinuities: [],
      instrumentTypes: [
        "timeline-record-type-heap-allocations"
      ],
      records: [{
        "type": "timeline-record-type-heap-allocations",
        "title": "Snapshot",
        "snapshotStringData": JSON.stringify(snapshot)
      }],
      markers: [],
      memoryPressureEvents: [],
      sampleStackTraces: [],
      sampleDurations: []
    },
    overview: {}
  }, undefined, 2)
)