What is @datadog/pprof?
The @datadog/pprof package is a Node.js module provided by Datadog for profiling Node.js applications. It allows developers to generate and analyze CPU and heap profiles to understand the performance characteristics of their applications and identify bottlenecks or memory leaks.
What are @datadog/pprof's main functionalities?
CPU Profiling
This feature enables CPU profiling, allowing developers to understand where their application spends most of its execution time. The code sample demonstrates how to start CPU profiling with a specified duration and sampling rate.
const profiler = require('@datadog/pprof');
profiler.start({
service: 'my-service',
// 10 minutes profiling duration
duration: 600,
// Profile CPU every 1000 milliseconds
sampleRate: 1000
});
Heap Profiling
Heap profiling helps in identifying memory leaks and understanding memory allocation patterns. The code sample shows how to start heap profiling with a specified interval between snapshots.
const profiler = require('@datadog/pprof');
profiler.heap.start({
service: 'my-service',
// Take a snapshot every 600 seconds
snapshotInterval: 600
});
Other packages similar to @datadog/pprof
v8-profiler-next
v8-profiler-next is a Node.js package for profiling the V8 JavaScript engine. It provides similar functionalities for CPU and heap profiling. Compared to @datadog/pprof, it focuses more on the V8 engine specifically and might offer more detailed insights for applications heavily relying on V8's features.
node-memwatch
node-memwatch is another Node.js package designed for memory leak detection and heap diffing. While it provides valuable insights into memory usage and leaks, it does not offer CPU profiling, making it less comprehensive than @datadog/pprof for overall performance analysis.
pprof support for Node.js
pprof support for Node.js.
Prerequisites
-
Your application will need to be using Node.js 14 or greater.
-
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 arm64/x64,
Alpine Linux x64, macOS arm64/x64, windows x64 for Node 14/16/18/20.
No additional dependencies are required. - For other environments: 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
.
-
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
.
npm install --save @datadog/pprof
Using the Profiler
Collect a Wall Time Profile
In code:
-
Update code to collect and save a profile:
const profile = await pprof.time.profile({
durationMillis: 10000,
});
const buf = await pprof.encode(profile);
fs.writeFile('wall.pb.gz', buf, (err) => {
if (err) throw err;
});
-
View the profile with command line pprof
:
pprof -http=: wall.pb.gz
Requiring from the command line
-
Start program from the command line:
node --require @datadog/pprof app.js
-
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
-
Enable heap profiling at the start of the application:
const intervalBytes = 512 * 1024;
const stackDepth = 64;
heap.start(intervalBytes, stackDepth);
-
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();