Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/common/test/performance.spec.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 { afterEach, describe, expect, it } from 'vitest';
7
import { ChatExtPerfMark, clearChatExtMarks, getChatExtMarks, markChatExt } from '../performance';
8
9
describe('performance', () => {
10
11
const TEST_PREFIX = 'code/chat/ext/';
12
let testCounter = 0;
13
const createdSessionIds: string[] = [];
14
15
afterEach(() => {
16
for (const id of createdSessionIds) {
17
clearChatExtMarks(id);
18
}
19
createdSessionIds.length = 0;
20
});
21
22
function uniqueSessionId(): string {
23
const id = `test-session-${testCounter++}-${Date.now()}`;
24
createdSessionIds.push(id);
25
return id;
26
}
27
28
function getMarksForSession(sessionId: string) {
29
return getChatExtMarks().filter(m => m.name.startsWith(`${TEST_PREFIX}${sessionId}/`));
30
}
31
32
describe('markChatExt', () => {
33
it('emits a mark with the expected prefix', () => {
34
const sessionId = uniqueSessionId();
35
markChatExt(sessionId, ChatExtPerfMark.WillHandleParticipant);
36
37
const marks = getMarksForSession(sessionId);
38
expect(marks).toHaveLength(1);
39
expect(marks[0].name).toBe(`${TEST_PREFIX}${sessionId}/${ChatExtPerfMark.WillHandleParticipant}`);
40
});
41
42
it('emits multiple marks for the same session', () => {
43
const sessionId = uniqueSessionId();
44
markChatExt(sessionId, ChatExtPerfMark.WillBuildPrompt);
45
markChatExt(sessionId, ChatExtPerfMark.DidBuildPrompt);
46
47
const marks = getMarksForSession(sessionId);
48
expect(marks).toHaveLength(2);
49
});
50
51
it('no-ops when sessionId is undefined', () => {
52
const before = getChatExtMarks().length;
53
markChatExt(undefined, ChatExtPerfMark.WillFetch);
54
expect(getChatExtMarks().length).toBe(before);
55
});
56
});
57
58
describe('ChatExtPerfMark', () => {
59
it('contains all expected mark names', () => {
60
expect(ChatExtPerfMark.WillHandleParticipant).toBe('willHandleParticipant');
61
expect(ChatExtPerfMark.DidHandleParticipant).toBe('didHandleParticipant');
62
expect(ChatExtPerfMark.WillBuildPrompt).toBe('willBuildPrompt');
63
expect(ChatExtPerfMark.DidBuildPrompt).toBe('didBuildPrompt');
64
expect(ChatExtPerfMark.WillFetch).toBe('willFetch');
65
expect(ChatExtPerfMark.DidFetch).toBe('didFetch');
66
});
67
});
68
69
describe('clearChatExtMarks', () => {
70
it('removes marks for the given session', () => {
71
const sessionId1 = uniqueSessionId();
72
const sessionId2 = uniqueSessionId();
73
markChatExt(sessionId1, ChatExtPerfMark.WillFetch);
74
markChatExt(sessionId2, ChatExtPerfMark.DidFetch);
75
76
clearChatExtMarks(sessionId1);
77
78
const marks = getChatExtMarks().filter(m => m.name.startsWith(`${TEST_PREFIX}${sessionId1}/`));
79
expect(marks).toHaveLength(0);
80
const remaining = getChatExtMarks().filter(m => m.name.startsWith(`${TEST_PREFIX}${sessionId2}/`));
81
expect(remaining).toHaveLength(1);
82
});
83
});
84
});
85
86