perf(server): avoid quadratic resource history replay (#18825)
Co-authored-by: SAPHID <[email protected]> Co-authored-by: GPT-6.1 Sol <[email protected]>
This commit is contained in:
3 files changed
+92
-13
No files matched your search
@@ -52,6 +52,8 @@ export interface MergeProcessesInput {
|
||||
readonly previous: ReadonlyMap<string, ProcessState>;
|
||||
readonly counters: TelemetryCounters;
|
||||
readonly updatePrevious: boolean;
|
||||
/** History replay needs process deltas, but does not publish lifetime group counters. */
|
||||
readonly accumulateCounters?: boolean;
|
||||
}
|
||||
|
||||
export interface MergeProcessesResult {
|
||||
@@ -574,14 +576,15 @@ export function mergeProcesses(input: MergeProcessesInput): MergeProcessesResult
|
||||
});
|
||||
const ordered = orderProcessTree(normalized, rootPids);
|
||||
|
||||
const counters = input.updatePrevious
|
||||
? applyLifecycleCounters({
|
||||
counters: input.counters,
|
||||
deltas: processDeltas,
|
||||
current: nextPrevious,
|
||||
previous: input.previous,
|
||||
})
|
||||
: input.counters;
|
||||
const counters =
|
||||
input.updatePrevious && input.accumulateCounters !== false
|
||||
? applyLifecycleCounters({
|
||||
counters: input.counters,
|
||||
deltas: processDeltas,
|
||||
current: nextPrevious,
|
||||
previous: input.previous,
|
||||
})
|
||||
: input.counters;
|
||||
const backendProcesses = ordered.filter(
|
||||
(process) => categoryGroup(process.category) === "backend",
|
||||
);
|
||||
|
||||
@@ -142,6 +142,80 @@ function desktopSnapshot(sampledAtUnixMs: number): DesktopHostTelemetrySnapshot
|
||||
}
|
||||
|
||||
describe("ResourceTelemetry", () => {
|
||||
it.effect("replays churning history and retains baselines for returning processes", () =>
|
||||
Effect.gen(function* () {
|
||||
const readAtMs = DateTime.toEpochMillis(yield* DateTime.now);
|
||||
const sampleCount = 100;
|
||||
const samples = Array.from({ length: sampleCount }, (_, index) => {
|
||||
const sample = nativeSnapshot({
|
||||
sequence: index + 1,
|
||||
sampledAtUnixMs: readAtMs - (sampleCount - index) * 1_000,
|
||||
childCpuTimeMs: index * 10,
|
||||
childWriteBytes: index * 100,
|
||||
});
|
||||
const processes = sample.processes
|
||||
.filter((entry) => entry.pid === process.pid || entry.pid === 4_242)
|
||||
.map((entry) =>
|
||||
entry.pid === 4_242
|
||||
? { ...entry, startTimeMs: index }
|
||||
: { ...entry, ioWriteBytes: index * 100 },
|
||||
);
|
||||
if (index === 0 || index === 2) {
|
||||
processes.push(
|
||||
processSample({
|
||||
pid: 4_243,
|
||||
ppid: process.pid,
|
||||
startTimeMs: 500,
|
||||
cpuTimeMs: index === 0 ? 100 : 350,
|
||||
ioWriteBytes: index === 0 ? 1_000 : 5_000,
|
||||
}),
|
||||
);
|
||||
}
|
||||
return { ...sample, processes };
|
||||
});
|
||||
const layerTelemetry = ResourceTelemetry.layer.pipe(
|
||||
Layer.provide(
|
||||
Layer.mergeAll(
|
||||
NativeTelemetryClient.layerTest({ readHistory: () => Effect.succeed(samples) }),
|
||||
DesktopTelemetryReceiver.layerTest(),
|
||||
ResourceAttribution.layer,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
yield* Effect.gen(function* () {
|
||||
const telemetry = yield* ResourceTelemetry.ResourceTelemetry;
|
||||
const history = yield* telemetry.readHistory({
|
||||
windowMs: sampleCount * 1_000,
|
||||
bucketMs: 10_000,
|
||||
});
|
||||
|
||||
expect(history.topProcesses).toHaveLength(sampleCount + 2);
|
||||
expect(
|
||||
history.topProcesses.find((entry) => entry.identity.pid === process.pid),
|
||||
).toMatchObject({
|
||||
sampleCount,
|
||||
cpuTimeMs: (sampleCount - 1) * 10,
|
||||
ioWriteBytes: (sampleCount - 1) * 100,
|
||||
});
|
||||
const children = history.topProcesses.filter((entry) => entry.identity.pid === 4_242);
|
||||
expect(children).toHaveLength(sampleCount);
|
||||
for (const child of children) {
|
||||
expect(child).toMatchObject({ sampleCount: 1, cpuTimeMs: 0, ioWriteBytes: 0 });
|
||||
}
|
||||
expect(history.topProcesses.find((entry) => entry.identity.pid === 4_243)).toMatchObject({
|
||||
sampleCount: 2,
|
||||
cpuTimeMs: 250,
|
||||
ioWriteBytes: 4_000,
|
||||
});
|
||||
expect(Math.max(...history.buckets.map((bucket) => bucket.maxProcessCount))).toBe(3);
|
||||
expect(history.buckets.reduce((total, bucket) => total + bucket.ioWriteBytes, 0)).toBe(
|
||||
(sampleCount - 1) * 100 + 4_000,
|
||||
);
|
||||
}).pipe(Effect.provide(layerTelemetry));
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("enables live native and Electron collection only while changes are retained", () =>
|
||||
Effect.gen(function* () {
|
||||
const sampledAtUnixMs = DateTime.toEpochMillis(yield* DateTime.now);
|
||||
|
||||
@@ -15,7 +15,6 @@ import {
|
||||
mergeProcesses,
|
||||
processIdentityKey,
|
||||
type ProcessState,
|
||||
type TelemetryCounters,
|
||||
} from "./Model.ts";
|
||||
|
||||
const MAX_HISTORY_WINDOW_MS = 60 * 60_000;
|
||||
@@ -167,8 +166,8 @@ export function buildResourceTelemetryHistory(
|
||||
const aggregateSamples: AggregateSample[] = [];
|
||||
const legacyBackendAggregateSamples: AggregateSample[] = [];
|
||||
const processSamples: ProcessSample[] = [];
|
||||
let previous: ReadonlyMap<string, ProcessState> = new Map();
|
||||
let counters: TelemetryCounters = emptyTelemetryCounters();
|
||||
const previous = new Map<string, ProcessState>();
|
||||
const counters = emptyTelemetryCounters();
|
||||
let previousSnapshotAtMs: number | undefined;
|
||||
|
||||
for (const snapshot of snapshots) {
|
||||
@@ -216,9 +215,12 @@ export function buildResourceTelemetryHistory(
|
||||
previous,
|
||||
counters,
|
||||
updatePrevious: true,
|
||||
accumulateCounters: false,
|
||||
});
|
||||
previous = new Map([...previous, ...merged.previous]);
|
||||
counters = merged.counters;
|
||||
// Keep absent identities' baselines without copying every exited process on each sample.
|
||||
for (const [identityKey, state] of merged.previous) {
|
||||
previous.set(identityKey, state);
|
||||
}
|
||||
if (snapshot.sampledAtUnixMs < windowStartMs) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user