refactor: remove performance monitoring and diagnostics infrastructure
This commit is contained in:
@@ -1,80 +0,0 @@
|
||||
import { getNowMs, isPerfLoggingEnabled, perfLog } from "@/utils/perf";
|
||||
|
||||
type NavigationTimingDetails = {
|
||||
from: string;
|
||||
to: string;
|
||||
params?: Record<string, unknown>;
|
||||
targetMs?: number;
|
||||
};
|
||||
|
||||
type NavigationTimingEntry = NavigationTimingDetails & {
|
||||
startedAt: number;
|
||||
};
|
||||
|
||||
const NAVIGATION_TAG = "[NavigationTiming]";
|
||||
const pendingNavigations = new Map<string, NavigationTimingEntry>();
|
||||
|
||||
export const HOME_NAVIGATION_KEY = "home";
|
||||
|
||||
export const buildAgentNavigationKey = (serverId: string, agentId: string) =>
|
||||
`agent:${serverId}:${agentId}`;
|
||||
|
||||
export const startNavigationTiming = (key: string, details: NavigationTimingDetails): void => {
|
||||
if (!isPerfLoggingEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
pendingNavigations.set(key, {
|
||||
...details,
|
||||
startedAt: getNowMs(),
|
||||
});
|
||||
|
||||
perfLog(NAVIGATION_TAG, {
|
||||
phase: "start",
|
||||
key,
|
||||
from: details.from,
|
||||
to: details.to,
|
||||
targetMs: details.targetMs ?? null,
|
||||
params: details.params ?? null,
|
||||
});
|
||||
};
|
||||
|
||||
export const endNavigationTiming = (key: string, extra?: Record<string, unknown>): void => {
|
||||
if (!isPerfLoggingEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const entry = pendingNavigations.get(key);
|
||||
if (!entry) {
|
||||
return;
|
||||
}
|
||||
pendingNavigations.delete(key);
|
||||
|
||||
const durationMs = getNowMs() - entry.startedAt;
|
||||
perfLog(NAVIGATION_TAG, {
|
||||
phase: "complete",
|
||||
key,
|
||||
from: entry.from,
|
||||
to: entry.to,
|
||||
durationMs: Number(durationMs.toFixed(2)),
|
||||
targetMs: entry.targetMs ?? null,
|
||||
params: entry.params ?? null,
|
||||
extra: extra ?? null,
|
||||
});
|
||||
};
|
||||
|
||||
export const cancelNavigationTiming = (key: string, reason?: string): void => {
|
||||
if (!isPerfLoggingEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!pendingNavigations.has(key)) {
|
||||
return;
|
||||
}
|
||||
pendingNavigations.delete(key);
|
||||
perfLog(NAVIGATION_TAG, {
|
||||
phase: "cancelled",
|
||||
key,
|
||||
reason: reason ?? null,
|
||||
});
|
||||
};
|
||||
@@ -1,20 +0,0 @@
|
||||
// Compatibility shim while callers move to `@/runtime/perf-diagnostics`.
|
||||
// Keep this file side-effect free.
|
||||
export {
|
||||
consumePersistedPerfDiagnosticReports,
|
||||
getPerfDiagnosticBreadcrumbs,
|
||||
getPerfDiagnosticsSnapshot,
|
||||
installPerfDiagnosticsMonitor,
|
||||
isPerfDiagnosticsEnabled,
|
||||
peekPersistedPerfDiagnosticReports,
|
||||
recordPerfDiagnosticMark,
|
||||
} from "@/runtime/perf-diagnostics";
|
||||
|
||||
export type {
|
||||
PerfDiagnosticBreadcrumb,
|
||||
PerfDiagnosticFields,
|
||||
PerfDiagnosticsReport,
|
||||
PerfDiagnosticsSnapshot,
|
||||
PerfDiagnosticsReporter,
|
||||
RecordPerfDiagnosticMarkOptions,
|
||||
} from "@/runtime/perf-diagnostics";
|
||||
@@ -1,113 +0,0 @@
|
||||
import { getNowMs, isPerfLoggingEnabled, perfLog } from "@/utils/perf";
|
||||
|
||||
const PERF_MONITOR_LOG_TAG = "[PerfMonitor]";
|
||||
const FRAME_GAP_THRESHOLD_MS = 100;
|
||||
const EVENT_LOOP_STALL_THRESHOLD_MS = 100;
|
||||
const EVENT_LOOP_TICK_MS = 100;
|
||||
const LONG_TASK_THRESHOLD_MS = 100;
|
||||
|
||||
type StopPerfMonitor = () => void;
|
||||
|
||||
type GlobalScope = typeof globalThis & {
|
||||
requestAnimationFrame?: (callback: (timestamp: number) => void) => number;
|
||||
cancelAnimationFrame?: (handle: number) => void;
|
||||
PerformanceObserver?: unknown;
|
||||
};
|
||||
|
||||
type PerfObserverEntry = {
|
||||
duration: number;
|
||||
startTime: number;
|
||||
};
|
||||
|
||||
type PerfObserverLike = {
|
||||
observe: (options: { entryTypes: string[] }) => void;
|
||||
disconnect: () => void;
|
||||
};
|
||||
|
||||
type PerfObserverConstructor = new (
|
||||
callback: (list: { getEntries(): PerfObserverEntry[] }) => void
|
||||
) => PerfObserverLike;
|
||||
|
||||
export function startPerfMonitor(scope: string): StopPerfMonitor {
|
||||
if (!isPerfLoggingEnabled()) {
|
||||
return () => {};
|
||||
}
|
||||
|
||||
const globalScope = globalThis as GlobalScope;
|
||||
if (!globalScope) {
|
||||
return () => {};
|
||||
}
|
||||
|
||||
const startMs = getNowMs();
|
||||
perfLog(PERF_MONITOR_LOG_TAG, { event: "monitor_start", scope });
|
||||
|
||||
let rafHandle: number | null = null;
|
||||
let lastFrameMs = getNowMs();
|
||||
|
||||
if (typeof globalScope.requestAnimationFrame === "function") {
|
||||
const onFrame = (now: number) => {
|
||||
const delta = now - lastFrameMs;
|
||||
if (delta >= FRAME_GAP_THRESHOLD_MS) {
|
||||
perfLog(PERF_MONITOR_LOG_TAG, {
|
||||
event: "frame_gap",
|
||||
scope,
|
||||
deltaMs: Math.round(delta),
|
||||
sinceStartMs: Math.round(now - startMs),
|
||||
});
|
||||
}
|
||||
lastFrameMs = now;
|
||||
rafHandle = globalScope.requestAnimationFrame?.(onFrame) ?? null;
|
||||
};
|
||||
rafHandle = globalScope.requestAnimationFrame(onFrame);
|
||||
}
|
||||
|
||||
let lastTickMs = getNowMs();
|
||||
const intervalHandle = setInterval(() => {
|
||||
const now = getNowMs();
|
||||
const drift = now - lastTickMs - EVENT_LOOP_TICK_MS;
|
||||
if (drift >= EVENT_LOOP_STALL_THRESHOLD_MS) {
|
||||
perfLog(PERF_MONITOR_LOG_TAG, {
|
||||
event: "event_loop_stall",
|
||||
scope,
|
||||
driftMs: Math.round(drift),
|
||||
sinceStartMs: Math.round(now - startMs),
|
||||
});
|
||||
}
|
||||
lastTickMs = now;
|
||||
}, EVENT_LOOP_TICK_MS);
|
||||
|
||||
let observer: PerfObserverLike | null = null;
|
||||
const ObserverCtor = globalScope.PerformanceObserver as PerfObserverConstructor | undefined;
|
||||
if (typeof ObserverCtor === "function") {
|
||||
try {
|
||||
observer = new ObserverCtor((list) => {
|
||||
for (const entry of list.getEntries()) {
|
||||
if (entry.duration >= LONG_TASK_THRESHOLD_MS) {
|
||||
perfLog(PERF_MONITOR_LOG_TAG, {
|
||||
event: "longtask",
|
||||
scope,
|
||||
durationMs: Math.round(entry.duration),
|
||||
startMs: Math.round(entry.startTime),
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
observer.observe({ entryTypes: ["longtask"] });
|
||||
} catch (error) {
|
||||
perfLog(PERF_MONITOR_LOG_TAG, {
|
||||
event: "longtask_observer_error",
|
||||
scope,
|
||||
message: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return () => {
|
||||
if (rafHandle !== null && typeof globalScope.cancelAnimationFrame === "function") {
|
||||
globalScope.cancelAnimationFrame(rafHandle);
|
||||
}
|
||||
clearInterval(intervalHandle);
|
||||
observer?.disconnect();
|
||||
perfLog(PERF_MONITOR_LOG_TAG, { event: "monitor_stop", scope });
|
||||
};
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
const PERF_LOGGING_ENABLED = false;
|
||||
|
||||
export const isPerfLoggingEnabled = (): boolean => PERF_LOGGING_ENABLED;
|
||||
|
||||
export const perfLog = (tag: string, details: Record<string, unknown>): void => {
|
||||
if (!PERF_LOGGING_ENABLED) {
|
||||
return;
|
||||
}
|
||||
console.info(tag, details);
|
||||
};
|
||||
|
||||
export const getNowMs = (): number => {
|
||||
if (typeof performance !== "undefined" && typeof performance.now === "function") {
|
||||
return performance.now();
|
||||
}
|
||||
return Date.now();
|
||||
};
|
||||
|
||||
export interface PayloadMetrics {
|
||||
approxBytes: number;
|
||||
fieldCount: number;
|
||||
}
|
||||
|
||||
const MEASUREMENT_DEPTH = 2;
|
||||
|
||||
export const measurePayload = (payload: unknown): PayloadMetrics => {
|
||||
if (!payload || typeof payload !== "object") {
|
||||
return {
|
||||
approxBytes: 0,
|
||||
fieldCount: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const fieldCount = Object.keys(payload as Record<string, unknown>).length;
|
||||
const approxBytes = estimateSize(payload, MEASUREMENT_DEPTH, new WeakSet());
|
||||
|
||||
return {
|
||||
approxBytes,
|
||||
fieldCount,
|
||||
};
|
||||
};
|
||||
|
||||
const estimateSize = (value: unknown, depth: number, seen: WeakSet<object>): number => {
|
||||
if (depth <= 0 || value === null || value === undefined) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (typeof value === "string") {
|
||||
return value.length;
|
||||
}
|
||||
|
||||
if (typeof value === "number") {
|
||||
return 8;
|
||||
}
|
||||
|
||||
if (typeof value === "boolean") {
|
||||
return 4;
|
||||
}
|
||||
|
||||
if (typeof value === "bigint") {
|
||||
return value.toString().length;
|
||||
}
|
||||
|
||||
if (typeof value === "symbol" || typeof value === "function") {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (typeof value !== "object") {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const objectValue = value as object;
|
||||
if (seen.has(objectValue)) {
|
||||
return 0;
|
||||
}
|
||||
seen.add(objectValue);
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
return value.reduce((total, item) => total + estimateSize(item, depth - 1, seen), 0);
|
||||
}
|
||||
|
||||
if (value instanceof Map) {
|
||||
let total = 0;
|
||||
for (const [mapKey, mapValue] of value.entries()) {
|
||||
total += estimateSize(mapKey, depth - 1, seen);
|
||||
total += estimateSize(mapValue, depth - 1, seen);
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
if (value instanceof Set) {
|
||||
let total = 0;
|
||||
for (const setValue of value.values()) {
|
||||
total += estimateSize(setValue, depth - 1, seen);
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
let total = 0;
|
||||
for (const [key, child] of Object.entries(value as Record<string, unknown>)) {
|
||||
total += key.length;
|
||||
total += estimateSize(child, depth - 1, seen);
|
||||
}
|
||||
return total;
|
||||
};
|
||||
@@ -56,6 +56,7 @@ export async function buildClientConfig(
|
||||
clientId,
|
||||
clientType: "mobile" as const,
|
||||
suppressSendErrors: true,
|
||||
reconnect: { enabled: false },
|
||||
...(connection.type === "directSocket" || connection.type === "directPipe"
|
||||
? localTransportFactory
|
||||
? { transportFactory: localTransportFactory }
|
||||
|
||||
@@ -1,11 +1,4 @@
|
||||
import { z } from "zod";
|
||||
import { getNowMs, isPerfLoggingEnabled, perfLog } from "./perf";
|
||||
|
||||
const TOOL_CALL_DIFF_LOG_TAG = "[ToolCallDiff]";
|
||||
const LINE_DIFF_DURATION_THRESHOLD_MS = 16;
|
||||
const WORD_DIFF_DURATION_THRESHOLD_MS = 8;
|
||||
const LINE_DIFF_MATRIX_THRESHOLD = 200000;
|
||||
const WORD_DIFF_MATRIX_THRESHOLD = 50000;
|
||||
|
||||
export type DiffSegment = {
|
||||
text: string;
|
||||
@@ -56,14 +49,11 @@ function splitIntoWords(text: string): string[] {
|
||||
}
|
||||
|
||||
function computeWordLevelDiff(oldLine: string, newLine: string): { oldSegments: DiffSegment[]; newSegments: DiffSegment[] } {
|
||||
const shouldLog = isPerfLoggingEnabled();
|
||||
const startMs = shouldLog ? getNowMs() : 0;
|
||||
const oldWords = splitIntoWords(oldLine);
|
||||
const newWords = splitIntoWords(newLine);
|
||||
|
||||
const m = oldWords.length;
|
||||
const n = newWords.length;
|
||||
const matrixSize = m * n;
|
||||
|
||||
// LCS to find common words
|
||||
const dp: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
|
||||
@@ -131,22 +121,6 @@ function computeWordLevelDiff(oldLine: string, newLine: string): { oldSegments:
|
||||
const oldSegments = buildSegments(oldWords, oldInLCS);
|
||||
const newSegments = buildSegments(newWords, newInLCS);
|
||||
|
||||
if (shouldLog) {
|
||||
const durationMs = getNowMs() - startMs;
|
||||
if (
|
||||
durationMs >= WORD_DIFF_DURATION_THRESHOLD_MS ||
|
||||
matrixSize >= WORD_DIFF_MATRIX_THRESHOLD
|
||||
) {
|
||||
perfLog(TOOL_CALL_DIFF_LOG_TAG, {
|
||||
event: "word_diff",
|
||||
durationMs: Math.round(durationMs),
|
||||
oldWordCount: m,
|
||||
newWordCount: n,
|
||||
matrixSize,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
oldSegments,
|
||||
newSegments,
|
||||
@@ -154,8 +128,6 @@ function computeWordLevelDiff(oldLine: string, newLine: string): { oldSegments:
|
||||
}
|
||||
|
||||
export function buildLineDiff(originalText: string, updatedText: string): DiffLine[] {
|
||||
const shouldLog = isPerfLoggingEnabled();
|
||||
const startMs = shouldLog ? getNowMs() : 0;
|
||||
const originalLines = splitIntoLines(originalText);
|
||||
const updatedLines = splitIntoLines(updatedText);
|
||||
|
||||
@@ -166,7 +138,6 @@ export function buildLineDiff(originalText: string, updatedText: string): DiffLi
|
||||
|
||||
const m = originalLines.length;
|
||||
const n = updatedLines.length;
|
||||
const matrixSize = m * n;
|
||||
const dp: number[][] = Array.from({ length: m + 1 }, () =>
|
||||
Array(n + 1).fill(0)
|
||||
);
|
||||
@@ -225,23 +196,6 @@ export function buildLineDiff(originalText: string, updatedText: string): DiffLi
|
||||
}
|
||||
}
|
||||
|
||||
if (shouldLog) {
|
||||
const durationMs = getNowMs() - startMs;
|
||||
if (
|
||||
durationMs >= LINE_DIFF_DURATION_THRESHOLD_MS ||
|
||||
matrixSize >= LINE_DIFF_MATRIX_THRESHOLD
|
||||
) {
|
||||
perfLog(TOOL_CALL_DIFF_LOG_TAG, {
|
||||
event: "line_diff",
|
||||
durationMs: Math.round(durationMs),
|
||||
originalLineCount: m,
|
||||
updatedLineCount: n,
|
||||
diffLineCount: diff.length,
|
||||
matrixSize,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return diff;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user