Path: blob/main/extensions/copilot/src/util/common/test/performance.spec.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 { afterEach, describe, expect, it } from 'vitest';6import { ChatExtPerfMark, clearChatExtMarks, getChatExtMarks, markChatExt } from '../performance';78describe('performance', () => {910const TEST_PREFIX = 'code/chat/ext/';11let testCounter = 0;12const createdSessionIds: string[] = [];1314afterEach(() => {15for (const id of createdSessionIds) {16clearChatExtMarks(id);17}18createdSessionIds.length = 0;19});2021function uniqueSessionId(): string {22const id = `test-session-${testCounter++}-${Date.now()}`;23createdSessionIds.push(id);24return id;25}2627function getMarksForSession(sessionId: string) {28return getChatExtMarks().filter(m => m.name.startsWith(`${TEST_PREFIX}${sessionId}/`));29}3031describe('markChatExt', () => {32it('emits a mark with the expected prefix', () => {33const sessionId = uniqueSessionId();34markChatExt(sessionId, ChatExtPerfMark.WillHandleParticipant);3536const marks = getMarksForSession(sessionId);37expect(marks).toHaveLength(1);38expect(marks[0].name).toBe(`${TEST_PREFIX}${sessionId}/${ChatExtPerfMark.WillHandleParticipant}`);39});4041it('emits multiple marks for the same session', () => {42const sessionId = uniqueSessionId();43markChatExt(sessionId, ChatExtPerfMark.WillBuildPrompt);44markChatExt(sessionId, ChatExtPerfMark.DidBuildPrompt);4546const marks = getMarksForSession(sessionId);47expect(marks).toHaveLength(2);48});4950it('no-ops when sessionId is undefined', () => {51const before = getChatExtMarks().length;52markChatExt(undefined, ChatExtPerfMark.WillFetch);53expect(getChatExtMarks().length).toBe(before);54});55});5657describe('ChatExtPerfMark', () => {58it('contains all expected mark names', () => {59expect(ChatExtPerfMark.WillHandleParticipant).toBe('willHandleParticipant');60expect(ChatExtPerfMark.DidHandleParticipant).toBe('didHandleParticipant');61expect(ChatExtPerfMark.WillBuildPrompt).toBe('willBuildPrompt');62expect(ChatExtPerfMark.DidBuildPrompt).toBe('didBuildPrompt');63expect(ChatExtPerfMark.WillFetch).toBe('willFetch');64expect(ChatExtPerfMark.DidFetch).toBe('didFetch');65});66});6768describe('clearChatExtMarks', () => {69it('removes marks for the given session', () => {70const sessionId1 = uniqueSessionId();71const sessionId2 = uniqueSessionId();72markChatExt(sessionId1, ChatExtPerfMark.WillFetch);73markChatExt(sessionId2, ChatExtPerfMark.DidFetch);7475clearChatExtMarks(sessionId1);7677const marks = getChatExtMarks().filter(m => m.name.startsWith(`${TEST_PREFIX}${sessionId1}/`));78expect(marks).toHaveLength(0);79const remaining = getChatExtMarks().filter(m => m.name.startsWith(`${TEST_PREFIX}${sessionId2}/`));80expect(remaining).toHaveLength(1);81});82});83});848586