-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathHeapSnapshotFormatter.ts
More file actions
66 lines (56 loc) · 1.67 KB
/
HeapSnapshotFormatter.ts
File metadata and controls
66 lines (56 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type {AggregatedInfoWithUid} from '../HeapSnapshotManager.js';
import type {DevTools} from '../third_party/index.js';
import {stableIdSymbol} from '../utils/id.js';
export interface FormattedSnapshotEntry {
className: string;
classUid?: number;
count: number;
selfSize: number;
retainedSize: number;
}
export class HeapSnapshotFormatter {
#aggregates: Record<string, AggregatedInfoWithUid>;
constructor(aggregates: Record<string, AggregatedInfoWithUid>) {
this.#aggregates = aggregates;
}
#getSortedAggregates(): AggregatedInfoWithUid[] {
return Object.values(this.#aggregates).sort((a, b) => b.self - a.self);
}
toString(): string {
const sorted = this.#getSortedAggregates();
const lines: string[] = [];
lines.push('uid,className,count,selfSize,maxRetainedSize');
for (const info of sorted) {
const uid = info[stableIdSymbol] ?? '';
lines.push(
`${uid},"${info.name}",${info.count},${info.self},${info.maxRet}`,
);
}
return lines.join('\n');
}
toJSON(): FormattedSnapshotEntry[] {
const sorted = this.#getSortedAggregates();
return sorted.map(info => ({
uid: info[stableIdSymbol],
className: info.name,
count: info.count,
selfSize: info.self,
retainedSize: info.maxRet,
}));
}
static sort(
aggregates: Record<
string,
DevTools.HeapSnapshotModel.HeapSnapshotModel.AggregatedInfo
>,
): Array<
[string, DevTools.HeapSnapshotModel.HeapSnapshotModel.AggregatedInfo]
> {
return Object.entries(aggregates).sort((a, b) => b[1].self - a[1].self);
}
}