Skip to content

Commit d84558f

Browse files
ofrobotsnolanmar511
authored andcommitted
feat: ability to get heap profile in V8 format (#42)
Expose API to get V8 heap profile unserialized as well. This can be useful for clients that want to serialize in a different foramt.
1 parent 656ea1d commit d84558f

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

ts/src/heap-profiler.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,27 @@ let heapStackDepth = 0;
2828
/*
2929
* Collects a heap profile when heapProfiler is enabled. Otherwise throws
3030
* an error.
31+
*
32+
* Data is returned in V8 allocation profile format.
3133
*/
32-
export function profile(ignoreSamplePath?: string, sourceMapper?: SourceMapper):
33-
perftools.profiles.IProfile {
34+
export function v8Profile(): AllocationProfileNode {
3435
if (!enabled) {
3536
throw new Error('Heap profiler is not enabled.');
3637
}
38+
return getAllocationProfile();
39+
}
40+
41+
/**
42+
* Collects a profile and returns it serialized in pprof format.
43+
* Throws if heap profiler is not enabled.
44+
*
45+
* @param ignoreSamplePath
46+
* @param sourceMapper
47+
*/
48+
export function profile(ignoreSamplePath?: string, sourceMapper?: SourceMapper):
49+
perftools.profiles.IProfile {
3750
const startTimeNanos = Date.now() * 1000 * 1000;
38-
const result = getAllocationProfile();
51+
const result = v8Profile();
3952
// Add node for external memory usage.
4053
// Current type definitions do not have external.
4154
// TODO: remove any once type definition is updated to include external.

ts/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const heap = {
3434
start: heapProfiler.start,
3535
stop: heapProfiler.stop,
3636
profile: heapProfiler.profile,
37+
v8Profile: heapProfiler.v8Profile,
3738
};
3839

3940
// If loaded with --require, start profiling.

0 commit comments

Comments
 (0)