Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ Install [`pprof`][npm-url] with `npm` or add to your `package.json`.
const profile = await pprof.heap.v8Profile();
```

### Collect without building a `Profile` object

Use `time.stopEncoded()` or `heap.profileEncoded()` when you only need profile
bytes. They accept the same arguments as `time.stop()` and `heap.profile()`.
The result includes `hasMissingSourceMaps` and an `encodeAsync()` method that
returns uncompressed pprof protobuf bytes. Compress the bytes separately if
your output requires gzip.

[build-image]: https://github.com/Datadog/pprof-nodejs/actions/workflows/build.yml/badge.svg?branch=main
[build-url]: https://github.com/Datadog/pprof-nodejs/actions/workflows/build.yml
[npm-image]: https://badge.fury.io/js/@datadog%2Fpprof.svg
Expand Down
133 changes: 133 additions & 0 deletions benchmark/direct-profile-writer.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
'use strict';

const assert = require('node:assert/strict');
const {performance} = require('node:perf_hooks');
const {visitTimeProfile} = require('../out/src/profile-normalizer');
const {
serializeEncodedProfile,
serializeTimeProfile,
} = require('../out/src/profile-serializer');

const count = Number(process.env.SAMPLES ?? 6500);
const depth = Number(process.env.DEPTH ?? 16);
const stackCount = Number(process.env.STACKS ?? 64);
const iterations = Number(process.env.ITERATIONS ?? 80);
const contexts = Array.from({length: count}, (_, index) => ({
timestamp: 1700000000000000n + BigInt(index) * 10000n,
cpuTime: 1000,
}));
let leaf = {
name: 'dispatcher',
scriptName: '/service.js',
hitCount: 0,
children: Array.from({length: stackCount}, (_, index) => {
const from = Math.floor((index * count) / stackCount);
const to = Math.floor(((index + 1) * count) / stackCount);
return {
name: `handler${index}`,
scriptName: '/service.js',
hitCount: to - from,
children: [],
contexts: contexts.slice(from, to),
};
}),
};
for (let index = 0; index < depth; index++) {
leaf = {
name: `frame${index}`,
scriptName: '/service.js',
hitCount: 0,
children: [leaf],
};
}
const profile = {
startTime: 0,
endTime: count * 10000,
epochOffset: 1700000000000000,
hasCpuTime: true,
topDownRoot: {name: '(root)', scriptName: '', hitCount: 0, children: [leaf]},
};
const intervalMicros = 10000;
const generateLabels = ({context}) => ({
'thread name': 'Main',
'thread id': 1,
'span id': (context.timestamp << 12n) | 13n,
end_timestamp_ns: context.timestamp * 1000n,
});
const lowCardinality = ['thread name', 'thread id'];
const paths = {
objects: () =>
serializeTimeProfile(
profile,
intervalMicros,
undefined,
true,
generateLabels,
lowCardinality,
),
writer: () =>
serializeEncodedProfile(
visitor =>
visitTimeProfile(profile, visitor, {
intervalMicros,
generateLabels,
recomputeSamplingInterval: true,
}),
lowCardinality,
'drop',
),
};

function summarize(values) {
values.sort((left, right) => left - right);
return [0.5, 0.95].map(percentile =>
Number(values[Math.floor(values.length * percentile)].toFixed(2)),
);
}

async function main() {
const now = Date.now;
Date.now = () => 1700000000000;
const writer = paths.writer();
const objects = paths.objects();
Date.now = now;
assert.deepEqual(await writer.encodeAsync(), await objects.encodeAsync());
const results = {objects: [], writer: []};
for (let index = -20; index < iterations; index++) {
const order =
index % 2 === 0 ? ['objects', 'writer'] : ['writer', 'objects'];
for (const name of order) {
const start = performance.now();
const collected = paths[name]();
const pause = performance.now() - start;
await collected.encodeAsync();
if (index >= 0)
results[name].push({pause, total: performance.now() - start});
}
}
for (const [name, samples] of Object.entries(results)) {
let collected;
global.gc();
const before = process.memoryUsage();
collected = paths[name]();
global.gc();
const retained = process.memoryUsage();
console.log(name, {
collectionMs: summarize(samples.map(sample => sample.pause)),
totalMs: summarize(samples.map(sample => sample.total)),
retainedKiB: Math.round(
(retained.heapUsed +
retained.arrayBuffers -
before.heapUsed -
before.arrayBuffers) /
1024,
),
});
await collected.encodeAsync();
}
}

main().catch(error => {
console.error(error);
process.exitCode = 1;
});
62 changes: 55 additions & 7 deletions ts/src/heap-profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ import {
ColumnNumbers,
DEFAULT_COLUMN_NUMBERS,
serializeHeapProfile,
serializeEncodedProfile,
EncodedProfile,
} from './profile-serializer';
import {NormalizedProfile, normalizeHeapProfile} from './profile-normalizer';
import {
NormalizedProfile,
normalizeHeapProfile,
visitHeapProfile,
} from './profile-normalizer';
import {SourceMapper} from './sourcemapper/sourcemapper';
import {
AllocationProfileNode,
Expand Down Expand Up @@ -160,17 +166,59 @@ export function convertProfile(
generateLabels?: GenerateAllocationLabelsFunction,
columnNumbers: ColumnNumbers = DEFAULT_COLUMN_NUMBERS,
): Profile {
return convertProfileWith(rootNode, (root, startTimeNanos, allocations) =>
serializeHeapProfile(
root,
startTimeNanos,
heapIntervalBytes,
ignoreSamplePath,
sourceMapper,
generateLabels,
allocations,
columnNumbers,
),
);
}

export function profileEncoded(
ignoreSamplePath?: string,
sourceMapper?: SourceMapper,
generateLabels?: GenerateAllocationLabelsFunction,
columnNumbers: ColumnNumbers = DEFAULT_COLUMN_NUMBERS,
): EncodedProfile {
return collectProfile(rootNode =>
convertProfileWith(rootNode, (root, startTimeNanos, allocations) =>
serializeEncodedProfile(
visitor =>
visitHeapProfile(root, visitor, {
startTimeNanos,
intervalBytes: heapIntervalBytes,
ignoreSamplesPath: ignoreSamplePath,
sourceMapper,
generateLabels,
allocations,
}),
[],
columnNumbers,
),
),
);
}

function convertProfileWith<Output>(
rootNode: AllocationProfileNode | AllocationProfileNodeWithStats,
convert: (
root: AllocationProfileNode | AllocationProfileNodeWithStats,
startTimeNanos: bigint,
allocations: boolean,
) => Output,
): Output {
const allocations = startedWithAllocations;
const startTimeNanos = BigInt(Date.now()) * 1_000_000n;
return serializeHeapProfile(
return convert(
addExternalMemoryNode(rootNode, allocations),
startTimeNanos,
heapIntervalBytes,
ignoreSamplePath,
sourceMapper,
generateLabels,
allocations,
columnNumbers,
);
}

Expand Down
2 changes: 2 additions & 0 deletions ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const time = {
profile: timeProfiler.profile,
start: timeProfiler.start,
stop: timeProfiler.stop,
stopEncoded: timeProfiler.stopEncoded,
stopNormalized: timeProfiler.stopNormalized,
profileV2: timeProfiler.profileV2,
getContext: timeProfiler.getContext,
Expand All @@ -73,6 +74,7 @@ export const heap = {
start: heapProfiler.start,
stop: heapProfiler.stop,
profile: heapProfiler.profile,
profileEncoded: heapProfiler.profileEncoded,
profileV2: heapProfiler.profileV2,
normalizedProfile: heapProfiler.normalizedProfile,
convertProfile: heapProfiler.convertProfile,
Expand Down
Loading
Loading