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