Skip to content

Commit 4ac04d6

Browse files
committed
fix(profile): handle empty profiles and fix percentage calculation
- Make shared.profiles optional in schema (may be absent when no data) - Handle null values for sample_durations_ns and sample_counts - Fix hot path percentage calculation to use total self time
1 parent cddef24 commit 4ac04d6

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

src/lib/profile/analyzer.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,13 @@ export function hasProfileData(flamegraph: Flamegraph): boolean {
6060
}
6161

6262
/**
63-
* Get the total weight (time) across all samples.
63+
* Get the total self time across all frames.
64+
* This gives the total CPU time spent in all functions.
6465
*/
65-
function getTotalWeight(flamegraph: Flamegraph): number {
66+
function getTotalSelfTime(flamegraph: Flamegraph): number {
6667
let total = 0;
67-
for (const profile of flamegraph.profiles) {
68-
for (const weight of profile.weights) {
69-
total += weight;
70-
}
68+
for (const info of flamegraph.shared.frame_infos) {
69+
total += info.sumSelfTime;
7170
}
7271
return total;
7372
}
@@ -87,9 +86,9 @@ export function analyzeHotPaths(
8786
userCodeOnly: boolean
8887
): HotPath[] {
8988
const { frames, frame_infos } = flamegraph.shared;
90-
const totalWeight = getTotalWeight(flamegraph);
89+
const totalSelfTime = getTotalSelfTime(flamegraph);
9190

92-
if (totalWeight === 0 || frames.length === 0) {
91+
if (totalSelfTime === 0 || frames.length === 0) {
9392
return [];
9493
}
9594

@@ -125,7 +124,7 @@ export function analyzeHotPaths(
125124
return topFrames.map(({ frame, info }) => ({
126125
frames: [frame], // Single frame for now (could expand to full call stack)
127126
frameInfo: info,
128-
percentage: (info.sumSelfTime / totalWeight) * 100,
127+
percentage: (info.sumSelfTime / totalSelfTime) * 100,
129128
}));
130129
}
131130

src/types/profile.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ export const FlamegraphProfileSchema = z
102102
/** Time weights for each sample */
103103
weights: z.array(z.number()),
104104
/** Sample durations in nanoseconds */
105-
sample_durations_ns: z.array(z.number()).optional(),
105+
sample_durations_ns: z.array(z.number()).nullish(),
106106
/** Sample counts */
107-
sample_counts: z.array(z.number()).optional(),
107+
sample_counts: z.array(z.number()).nullish(),
108108
})
109109
.passthrough();
110110

@@ -132,8 +132,8 @@ export const FlamegraphSchema = z
132132
frames: z.array(FlamegraphFrameSchema),
133133
/** Statistics for each frame (parallel array to frames) */
134134
frame_infos: z.array(FlamegraphFrameInfoSchema),
135-
/** Profile metadata */
136-
profiles: z.array(FlamegraphProfileMetadataSchema),
135+
/** Profile metadata (may be absent when no profiles exist) */
136+
profiles: z.array(FlamegraphProfileMetadataSchema).optional(),
137137
}),
138138
/** Transaction name this flamegraph represents */
139139
transactionName: z.string().optional(),

0 commit comments

Comments
 (0)