chrome2calltree

repository·master·Indexed 19 days ago

https://github.com/jlfwong/chrome2calltree

A CLI utility and library that converts Chrome CPU profiles into callgrind formatted files. This allows developers to analyze browser performance using visualization tools such as KCacheGrind or QCacheGrind. It includes a CLI for file conversion and the chromeProfileToCallgrind function for programmatic conversion of profile objects to Callgrind-compatible text.

Tokens
1.8K
Snippets
8
Records
8
Agent score
62%

What's inside chrome2calltree

  1. Install chrome2calltree

    master

    Install the chrome2calltree CLI tool globally using npm.

    Prerequisite: This tool is designed to work with visualization tools like KCacheGrind or QCacheGrind. You should install one of these before using chrome2calltree to ensure you can view the converted profiles.

    • Linux (KCacheGrind): sudo apt-get install kcachegrind
    • macOS (QCacheGrind): brew install qcachegrind
    npm install -g chrome2calltree
  2. Save converted callgrind output to a file

    master

    By default, chrome2calltree writes the converted output to standard out. To save the converted callgrind stats to a specific file for later use, use the -o (or --outfile) flag.

    chrome2calltree -i ~/Downloads/CPU-20131213T155541.cpuprofile -o foobar.profile
  3. Open a converted profile in KCacheGrind/QCacheGrind

    master

    You can automatically open the converted callgrind data in your installed KCacheGrind or QCacheGrind viewer by using the -k flag.

    Note: The application might open in the background, so you may need to switch to it manually.

    chrome2calltree -k -i ~/Downloads/CPU-20131213T155541.cpuprofile
  4. Reference the chrome2calltree CLI flags

    master

    The chrome2calltree CLI accepts the following optional arguments:

    FlagLong FlagDescription
    -h--helpShow this help message and exit
    -v--versionShow program's version number and exit
    -o OUTFILE--outfile OUTFILESave calltree stats to OUTFILE. If omitted, writes to standard out
    -i INFILE--infile INFILERead chrome CPU profile from INFILE. If omitted, reads from standard in
    -k--kcachegrindRun the kcachegrind tool on the converted data. Unless combined with the -o flag, will write output to a temporary file and remove it after kcachegrind exits
    usage: chrome2calltree [-h] [-v] [-o OUTFILE] [-i INFILE] [-k]
    
    Convert CPU profiles exported from Chrome to callgrind format
    
    Optional arguments:
      -h, --help            Show this help message and exit.
      -v, --version         Show program's version number and exit.
      -o OUTFILE, --outfile OUTFILE
                            Save calltree stats to OUTFILE. If omitted, writes to
                            standard out.
      -i INFILE, --infile INFILE
                            Read chrome CPU profile from INFILE. If omitted,
                            reads from standard in.
      -k, --kcachegrind     Run the kcachegrind tool on the converted data.
                            Unless combined with the -o flag, will write output
                            to a temporary file and remove it after kcachegrind
                            exits.
  5. Convert Chrome profiles to Callgrind format with chromeProfileToCallgrind

    master

    The chromeProfileToCallgrind function converts a Chrome CPU profile object into a Callgrind-compatible text format. This is useful for visualizing Chrome performance data in tools like KCachegrind.

    Parameters

    • profile (Object): The Chrome CPU profile object containing nodes, startTime, and endTime.
    • outStream (Writable Stream): A Node.js writable stream where the Callgrind formatted text will be written.
    • copy (Boolean, optional): If true (default), the function performs a deep clone of the profile object to avoid mutating the original input. If false, the original profile object is modified to include calculated timing data.

    Output Format

    The function writes a Callgrind file structure to the outStream, including:

    • events: ms hits header.
    • A summary line with total time and total hit count.
    • Function entries (fl, fn) followed by their self-time and hit count.
    • Call frame information (cfi, cfn) for child calls.
    const { chromeProfileToCallgrind } = require('./index');
    const fs = require('fs');
    
    const profile = JSON.parse(fs.readFileSync('chrome_profile.json', 'utf8'));
    const outStream = fs.createWriteStream('output.callgrind');
    
    // Convert the profile and write to the stream
    chromeProfileToCallgrind(profile, outStream, true);
  6. Reference: chrome2calltree CLI flags

    master

    The following flags are available for the chrome2calltree command line interface:

    -i, --infile INFILE
        Read chrome CPU profile from INFILE. If omitted, reads from standard in.
    -o, --outfile OUTFILE
        Save calltree stats to OUTFILE. If omitted, writes to standard out.
    -k, --kcachegrind
        Run the kcachegrind tool on the converted data. Unless combined with the -o flag, will write output to a temporary file and remove it after kcachegrind exits.
  7. Use the chrome2calltree CLI

    master

    The chrome2calltree CLI converts Chrome CPU profiles (exported as JSON) into the callgrind format. It supports reading from files or standard input, and writing to files or standard output. You can also automatically launch a visualization tool like kcachegrind or qcachegrind to inspect the results.

    # Convert a file and save to an output file
    chrome2calltree -i profile.json -o output.log
    
    # Convert from stdin and pipe to stdout
    cat profile.json | chrome2calltree
    
    # Convert and immediately open in kcachegrind/qcachegrind
    chrome2calltree -i profile.json --kcachegrind