hyj1991 / v8-profiler-next

node bindings for the v8 profiler
MIT License
218 stars 21 forks source link

.cpuprofile looks empty in vscode #33

Closed slutske22 closed 2 years ago

slutske22 commented 2 years ago

Hi and thanks for this great tool!

I am able to use this library to generate .cpuprofile files programmatically. Here's some code on how I'm doing it

export const heavyCalc = async (req, res: Response) => {
    const title = new Date().toISOString();
    v8profiler.startProfiling(title, true);

    calc = new HeavyCalculator();
    await calc.initialize();

    res.on('finish', () => {
        const profile = v8profiler.stopProfiling(title);
        profile.export(function (error, result) {
            fs.writeFileSync(
                `${process.cwd()}/temp/profiles/${title}.cpuprofile`,
                result
            );
        });
    });

    res.send(calc.toJSON());
};

This code successfully creates a .cpuprofile file in the specified directory. If I open this file with chrome devtools, it reads as expected:

Screen Shot 2021-11-22 at 7 55 34 AM

However, I'd like to work with vscode instead of relying on chrome. When I open this file with vscode, its empty:

Screen Shot 2021-11-22 at 7 56 25 AM

I can create a .cpuprofile manually within the vscode debugger following instructions from Can I profile NodeJS Applications using Visual Studio Code?. That .cpuprofile file opens as expected in both chrome and vscode. Here's what it looks like in vscode (light theme)

Screen Shot 2021-11-22 at 8 15 47 AM

What might be the problem with the .cpuprofile files generated by this library that they don't open properly in vscode?

Is this an issue with this library, or is there some inconsistencies between vscode and chrome as far as reading .cpuprofile files?

Versions I'm working with:

Mac OS Catalina, 10.15.6 Chrome Version 95.0.4638.69 (Official Build) (x86_64) VSCode 1.61.1 Node 14.16.0

hyj1991 commented 2 years ago

Thanks for your feedback, I'll take a look at this issue.

hyj1991 commented 2 years ago

This is because chrome made changes to the serialization format of .cpuprofile, and this is not a bug.

The old format (which v8-profiler-next generate) is like:

{
  head: { ...call frame tree },
  startTime: xxxx,
  endTime: xxxx,
  samples: [],
  timestamps: []
}

And the new format is like:

{
  nodes: [ ... flattern call frame tree ],
  startTime: xxxx,
  endTime: xxxx,
  samples: [],
  timeDeltas: []
}

It seems that chrome devtools is compatible with both old and new formats, but the vscode is not. That is why .cpuprofile generated by v8-profiler-next looks empty in vscode.

I plan to add a config to decide which serialization format will be generated.

hyj1991 commented 2 years ago

Install the latest v8-profiler-next (>= v1.5.0), and set generateType before profiling :

const v8Profiler = require('v8-profiler-next');
// default value is 0
// 0: default old format, 1: new format
v8Profiler.setGenerateType(1); 

v8Profiler.startProfiling();
const profile = v8Profiler.stopProfiling(title);

If do not set generateType, the previous .cpuprofile format will be used by default.

slutske22 commented 2 years ago

It works! Amazing! Thank you so much for addressing this, and so fast!