Path: blob/main/src/vs/workbench/contrib/chat/common/chatPerf.ts
13401 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { mark, clearMarks } from '../../../../base/common/performance.js';6import { URI } from '../../../../base/common/uri.js';7import { chatSessionResourceToId } from './model/chatUri.js';89const chatPerfPrefix = 'code/chat/';1011/** Tracks all mark names emitted per session so they can be cleared individually. */12const chatMarksBySession = new Map<string, Set<string>>();1314/**15* Well-defined perf scenarios for chat request lifecycle.16* Each mark is a boundary of a measurable scenario — don't add marks17* without defining what scenario they belong to.18*19* ## Scenarios20*21* **Time to UI Feedback** (perceived input lag):22* `request/start` → `request/uiUpdated`23*24* **Instruction Collection Overhead**:25* `request/willCollectInstructions` → `request/didCollectInstructions`26*27* **Extension Activation Wait** (first-request cold start):28* `code/chat/willWaitForActivation` → `code/chat/didWaitForActivation`29* (global marks, not session-scoped — emitted via {@link markChatGlobal})30*31* **Time to First Token** (the headline metric):32* `request/start` → `request/firstToken`33*34* **Total Request Duration**:35* `request/start` → `request/complete`36*37* **Agent Invocation Time** (LLM round-trip):38* `agent/willInvoke` → `agent/didInvoke`39*/40export const ChatPerfMark = {41/** User pressed Enter / request initiated */42RequestStart: 'request/start',43/** Request added to model → UI shows the message */44RequestUiUpdated: 'request/uiUpdated',45/** Begin collecting .instructions.md / skills / hooks */46WillCollectInstructions: 'request/willCollectInstructions',47/** Done collecting instructions */48DidCollectInstructions: 'request/didCollectInstructions',49/** First streamed response content received */50FirstToken: 'request/firstToken',51/** Response fully complete */52RequestComplete: 'request/complete',53/** Agent invoke begins (LLM round-trip start) */54AgentWillInvoke: 'agent/willInvoke',55/** Agent invoke returns (LLM round-trip end) */56AgentDidInvoke: 'agent/didInvoke',57} as const;5859/**60* Emits a performance mark scoped to a chat session:61* `code/chat/<sessionResource>/<name>`62*63* Marks are automatically cleaned up when the corresponding chat model is64* disposed — see {@link clearChatMarks}.65*/66export function markChat(sessionResource: URI, name: string): void {67const sessionId = chatSessionResourceToId(sessionResource);68const fullName = `${chatPerfPrefix}${sessionId}/${name}`;69let names = chatMarksBySession.get(sessionId);70if (!names) {71names = new Set();72chatMarksBySession.set(sessionId, names);73}74names.add(fullName);75mark(fullName);76}7778/**79* Clears all performance marks for the given chat session.80* Called when the chat model is disposed.81*/82export function clearChatMarks(sessionResource: URI): void {83const sessionId = chatSessionResourceToId(sessionResource);84const names = chatMarksBySession.get(sessionId);85if (names) {86for (const name of names) {87clearMarks(name);88}89chatMarksBySession.delete(sessionId);90}91}9293/**94* Well-defined one-time global perf marks (not scoped to a session).95* These are emitted via {@link markChatGlobal} and are never cleared.96*/97export const ChatGlobalPerfMark = {98/** Begin waiting for chat extension activation (SetupAgent) */99WillWaitForActivation: 'willWaitForActivation',100/** Extension activation + readiness complete (SetupAgent) */101DidWaitForActivation: 'didWaitForActivation',102} as const;103104/**105* Emits a global (non-session-scoped) performance mark:106* `code/chat/<name>`107*108* Used for one-time marks like activation that should persist across requests.109*/110export function markChatGlobal(name: string): void {111mark(`${chatPerfPrefix}${name}`);112}113114115