diff --git a/desktop/src/shared/lib/channelSwitchPerf.test.mjs b/desktop/src/shared/lib/channelSwitchPerf.test.mjs index 957c86efed..a83471a129 100644 --- a/desktop/src/shared/lib/channelSwitchPerf.test.mjs +++ b/desktop/src/shared/lib/channelSwitchPerf.test.mjs @@ -248,6 +248,27 @@ test("an undisturbed settle records exactly one measure", async () => { }); }); +test("abandoned switches never accumulate start marks", async () => { + await withSettleHarness(async ({ abandon, begin }) => { + const { CHANNEL_SWITCH_START_MARK } = await import( + "./channelSwitchPerf.ts" + ); + performance.clearMarks?.(CHANNEL_SWITCH_START_MARK); + // Traces that die without recording (forum visits, route exits, drops) + // never reach record()'s buffer clearing — begin() must bound the + // buffer itself or weeks-long sessions accumulate a mark per abandon. + for (const channelId of ["aaaa", "bbbb", "cccc", "dddd"]) { + begin(channelId); + abandon(channelId); + } + assert.equal( + performance.getEntriesByName(CHANNEL_SWITCH_START_MARK).length, + 1, + ); + performance.clearMarks?.(CHANNEL_SWITCH_START_MARK); + }); +}); + test("a settle in a hidden window drops the trace instead of recording", async () => { await withSettleHarness( async ({ begin, settle, flush, measures }) => { diff --git a/desktop/src/shared/lib/channelSwitchPerf.ts b/desktop/src/shared/lib/channelSwitchPerf.ts index 163658a05e..bbe09bd09a 100644 --- a/desktop/src/shared/lib/channelSwitchPerf.ts +++ b/desktop/src/shared/lib/channelSwitchPerf.ts @@ -201,6 +201,12 @@ export function beginChannelSwitchTrace(channelId: string): void { windowFetch: null, membersFetch: null, }; + // Clear the previous start mark here, not only in record(): traces that + // die without recording (forum visits, route exits, drops) never reach + // record()'s buffer clearing, and weeks-long sessions would accumulate a + // stray mark per abandon. Clearing at begin bounds the buffer to one + // start mark no matter how the previous trace ended. + performance.clearMarks(CHANNEL_SWITCH_START_MARK); performance.mark(CHANNEL_SWITCH_START_MARK, { detail: { channelId } }); }