GithubHelp home page GithubHelp logo

isabella232 / pprof-nodejs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from google/pprof-nodejs

0.0 0.0 0.0 2.97 MB

pprof support for Node.js

License: Apache License 2.0

Shell 3.49% Python 0.27% C++ 5.77% JavaScript 62.70% TypeScript 27.77%

pprof-nodejs's Introduction

pprof support for Node.js

NPM Version Build Status Known Vulnerabilities

pprof support for Node.js.

Prerequisites

  1. Your application will need to be using Node.js 10.4.1 or greater, or any version of Node 12 or 14.

The profiler should not be enabled when using earlier versions of Node 10, since versions of Node.js 10 prior to 10.4.1 are impacted by this issue, which can cause garbage collection to take several minutes when heap profiling is enabled.

  1. The pprof module has a native component that is used to collect profiles with v8's CPU and Heap profilers. You may need to install additional dependencies to build this module.

    • For Linux: pprof has prebuilt binaries available for Linux and Alpine Linux for Node 10, 12 and 14. No additional dependencies are required.
    • For other environments: when using @google-cloud/profiler on environments that pprof does not have prebuilt binaries for, the module node-gyp will be used to build binaries. See node-gyp's documentation for information on dependencies required to build binaries with node-gyp.
  2. The pprof CLI can be used to view profiles collected with this module. Instructions for installing the pprof CLI can be found here.

Basic Set-up

Install pprof with npm or add to your package.json.

# Install through npm while saving to the local 'package.json'
npm install --save pprof

Using the Profiler

Collect a Wall Time Profile

In code:

  1. Update code to collect and save a profile:

    const profile = await pprof.time.profile({
      durationMillis: 10000,    // time in milliseconds for which to 
                                // collect profile.
    });
    const buf = await pprof.encode(profile);
    fs.writeFile('wall.pb.gz', buf, (err) => {
      if (err) throw err;
    });
  2. View the profile with command line pprof:

    pprof -http=: wall.pb.gz

Requiring from the command line

  1. Start program from the command line:

    node --require pprof app.js
  2. A wall time profile for the job will be saved in pprof-profile-${process.pid}.pb.gz. View the profile with command line pprof:

    pprof -http=: pprof-profile-${process.pid}.pb.gz

Collect a Heap Profile

  1. Enable heap profiling at the start of the application:

    // The average number of bytes between samples.
    const intervalBytes = 512 * 1024;
    
    // The maximum stack depth for samples collected.
    const stackDepth = 64;
    
    heap.start(intervalBytes, stackDepth); 
  2. Collect heap profiles:

    • Collecting and saving a profile in profile.proto format:

      const profile = await pprof.heap.profile();
      const buf = await pprof.encode(profile);
      fs.writeFile('heap.pb.gz', buf, (err) => {
        if (err) throw err;
      })
    • View the profile with command line pprof.

      pprof -http=: heap.pb.gz
    • Collecting a heap profile with V8 allocation profile format:

        const profile = await pprof.heap.v8Profile();

pprof-nodejs's People

Contributors

aalexand avatar barrytam20 avatar fhinkel avatar greenkeeper[bot] avatar jinwoo avatar jkwlui avatar jqll avatar justbeingjustin avatar justinbeckwith avatar kjin avatar macieksmuga avatar nolanmar511 avatar ofrobots avatar renovate-bot avatar renovate[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.