Skip to content

Commit 9687fcf

Browse files
luoxuanzaoQoder-AI
andauthored
perf: add first-turn timing diagnostics for new-tab sessions (#53)
Instrument the new-tab first-session chain with [qoderian perf] logs so latency regressions can be attributed to a concrete stage: tab create/switch, turn build/title-gen/service-init/prepare, persistent-query spawn, CLI cold-start to first message, enqueue to first chunk, and end-to-end first-chunk/turn totals. Pure instrumentation: labels are fixed stage names without user data. Co-authored-by: QoderAI (Qwen 3.8 Max) <qoder_ai@qoder.com>
1 parent c181c74 commit 9687fcf

9 files changed

Lines changed: 267 additions & 15 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ version with its date and start a fresh empty `[Unreleased]` above it.
3838
startup instead of repeatedly blocking the interface, so vaults with
3939
hundreds of sessions no longer stall while Qoderian loads.
4040

41+
- First-message latency diagnostics: sending the first message of a new tab
42+
now logs per-stage timings to the developer console (filter by
43+
`qoderian perf`), covering tab creation, title generation, runtime startup,
44+
CLI cold-start, and the wait for the first response chunk — safe to paste
45+
into bug reports about slow first replies.
46+
4147
- The composer permission picker now mirrors New Qoder's three tiers —
4248
Ask approval, Auto approval, and Full access — and its labels and
4349
descriptions are localized across all ten supported locales instead of
Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
/**
2-
* Load-path timing diagnostics.
2+
* Load-path and turn-path timing diagnostics.
33
*
44
* Startup work (session metadata reads, edition migration, index building,
5-
* tab restore, history hydration) grows with the number of stored sessions,
6-
* and regressions were invisible until the plugin felt slow. Each stage wraps
7-
* itself in `measureAsync` and logs its elapsed time; labels are stable phase
5+
* tab restore, history hydration) and first-turn work (turn preparation,
6+
* persistent-query spawn, CLI cold-start, first response chunk) grow with the
7+
* number of stored sessions and external context, and regressions were
8+
* invisible until the plugin felt slow. Each stage wraps itself in
9+
* `measureAsync`/`measure` or logs via `logElapsed`; labels are stable phase
810
* names without user data, so the lines are safe to share in bug reports.
911
*/
1012

@@ -13,7 +15,22 @@ export async function measureAsync<T>(label: string, fn: () => Promise<T>): Prom
1315
try {
1416
return await fn();
1517
} finally {
16-
const elapsedMs = Math.round((performance.now() - startedAt) * 10) / 10;
17-
console.info(`[qoderian perf] ${label}: ${elapsedMs}ms`);
18+
logElapsed(label, startedAt);
1819
}
1920
}
21+
22+
/** Synchronous counterpart of `measureAsync` for CPU-bound stages. */
23+
export function measure<T>(label: string, fn: () => T): T {
24+
const startedAt = performance.now();
25+
try {
26+
return fn();
27+
} finally {
28+
logElapsed(label, startedAt);
29+
}
30+
}
31+
32+
/** Logs the elapsed time since `startedAt` under a stable perf label. */
33+
export function logElapsed(label: string, startedAt: number): void {
34+
const elapsedMs = Math.round((performance.now() - startedAt) * 10) / 10;
35+
console.info(`[qoderian perf] ${label}: ${elapsedMs}ms`);
36+
}

‎src/features/chat/controllers/input-controller.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { Notice } from 'obsidian';
33
import { hasErrorContentBlock } from '../../../core/chat/error-blocks';
44
import { detectBuiltInCommand } from '../../../core/commands/built-in-commands';
55
import type { BrowserSelectionContext, CanvasSelectionContext } from '../../../core/context/types';
6+
import {
7+
logElapsed,
8+
measure,
9+
measureAsync,
10+
} from '../../../core/diagnostics/performance';
611
import type { EditorSelectionContext } from '../../../core/editor/editor-context';
712
import type { ChatRuntime } from '../../../core/runtime/chat-runtime';
813
import type { ApprovalCallbackOptions, ChatTurnRequest } from '../../../core/runtime/types';
@@ -277,13 +282,13 @@ export class InputController {
277282
displayContent: content,
278283
turnRequest: cloneChatTurnRequest(options.turnRequestOverride),
279284
}
280-
: await this.buildTurnSubmission({
285+
: await measureAsync('turn.buildSubmission', () => this.buildTurnSubmission({
281286
content,
282287
images: imagesForMessage,
283288
editorContextOverride: options?.editorContextOverride,
284289
browserContextOverride: options?.browserContextOverride,
285290
canvasContextOverride: options?.canvasContextOverride,
286-
});
291+
}));
287292
const { displayContent, turnRequest } = turnSubmission;
288293

289294
fileContextManager?.markCurrentNoteSent();
@@ -300,7 +305,7 @@ export class InputController {
300305
state.hasPendingConversationSave = true;
301306
renderer.addMessage(userMsg);
302307

303-
await this.triggerTitleGeneration();
308+
await measureAsync('turn.titleGeneration', () => this.triggerTitleGeneration());
304309

305310
const assistantMsg: ChatMessage = {
306311
id: this.deps.generateId(),
@@ -325,6 +330,9 @@ export class InputController {
325330
isCompact ? 'qoderian-thinking--compact' : undefined,
326331
);
327332
state.responseStartTime = performance.now();
333+
// Turn-level timing origin for the [qoderian perf] turn.* diagnostics.
334+
const turnStartedAt = state.responseStartTime;
335+
let sawFirstRuntimeChunk = false;
328336

329337
let wasInterrupted = false;
330338
let wasInvalidated = false;
@@ -333,7 +341,7 @@ export class InputController {
333341

334342
// Lazy initialization: ensure service is ready before first query
335343
if (this.deps.ensureServiceInitialized) {
336-
const ready = await this.deps.ensureServiceInitialized();
344+
const ready = await measureAsync('turn.serviceInit', () => this.deps.ensureServiceInitialized!());
337345
if (!ready) {
338346
new Notice('Failed to initialize agent service. Please try again.');
339347
streamController.hideThinkingIndicator();
@@ -376,7 +384,7 @@ export class InputController {
376384
}
377385

378386
try {
379-
const preparedTurn = agentService.prepareTurn(turnRequest);
387+
const preparedTurn = measure('turn.prepareTurn', () => agentService.prepareTurn(turnRequest));
380388
userMsg.content = preparedTurn.persistedContent;
381389
userMsg.currentNote = preparedTurn.isCompact
382390
? undefined
@@ -386,6 +394,10 @@ export class InputController {
386394
// This prevents duplication when rebuilding context for new sessions
387395
const previousMessages = state.messages.slice(0, -2);
388396
for await (const chunk of agentService.query(preparedTurn, previousMessages)) {
397+
if (!sawFirstRuntimeChunk) {
398+
sawFirstRuntimeChunk = true;
399+
logElapsed('turn.firstChunk', turnStartedAt);
400+
}
389401
if (state.streamGeneration !== streamGeneration) {
390402
wasInvalidated = true;
391403
break;
@@ -416,6 +428,7 @@ export class InputController {
416428
this.activeStreamingAssistantMessage ?? assistantMsg,
417429
);
418430
} finally {
431+
logElapsed('turn.total', turnStartedAt);
419432
const finalAssistantMsg = this.activeStreamingAssistantMessage ?? assistantMsg;
420433
const turnMetadata = agentService.consumeTurnMetadata();
421434
userMsg.userMessageId = turnMetadata.userMessageId ?? userMsg.userMessageId;

‎src/features/chat/tabs/tab-manager.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ export class TabManager implements TabManagerInterface {
9494
conversationId?: string | null,
9595
tabId?: TabId,
9696
options: CreateTabOptions = {},
97+
): Promise<TabData | null> {
98+
return measureAsync(
99+
this.isRestoringState ? 'tab.restore' : 'tab.create',
100+
() => this.createTabInner(conversationId, tabId, options),
101+
);
102+
}
103+
104+
private async createTabInner(
105+
conversationId?: string | null,
106+
tabId?: TabId,
107+
options: CreateTabOptions = {},
97108
): Promise<TabData | null> {
98109
const maxTabs = this.getMaxTabs();
99110
if (this.tabs.size >= maxTabs) {
@@ -160,6 +171,10 @@ export class TabManager implements TabManagerInterface {
160171
* @param tabId The tab to switch to.
161172
*/
162173
async switchToTab(tabId: TabId): Promise<void> {
174+
return measureAsync('tab.switchTo', () => this.switchToTabInner(tabId));
175+
}
176+
177+
private async switchToTabInner(tabId: TabId): Promise<void> {
163178
const tab = this.tabs.get(tabId);
164179
if (!tab) {
165180
return;

‎src/qoder/runtime/persistent-turn-stream.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { SDKUserMessage } from '@qoder-ai/qoder-agent-sdk';
22

3+
import { logElapsed } from '../../core/diagnostics/performance';
34
import type { StreamChunk } from '../../core/types';
45
import type { QoderMessageChannel } from './qoder-message-channel';
56
import { isSessionExpiredError } from './session-context';
@@ -26,9 +27,16 @@ export async function* streamPersistentTurn(
2627
error: null as Error | null,
2728
};
2829
const handlerId = `handler-${Date.now()}-${Math.random().toString(36).slice(2)}`;
30+
const enqueuedAt = performance.now();
31+
let sawFirstChunk = false;
2932
const handler = createResponseHandler({
3033
id: handlerId,
3134
onChunk: chunk => {
35+
if (!sawFirstChunk) {
36+
sawFirstChunk = true;
37+
// Covers CLI cold-start when the persistent Query was just spawned.
38+
logElapsed('turn.enqueueToFirstChunk', enqueuedAt);
39+
}
3240
handler.markChunkSeen();
3341
if (state.resolveChunk) {
3442
state.resolveChunk(chunk);

‎src/qoder/runtime/qoder-chat-runtime.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import type {
2323
import { query as agentQuery } from '@qoder-ai/qoder-agent-sdk';
2424
import { Notice } from 'obsidian';
2525

26+
import {
27+
logElapsed,
28+
measureAsync,
29+
} from '../../core/diagnostics/performance';
2630
import { getEnhancedPath, getMissingNodeError } from '../../core/env/environment';
2731
import { getVaultPath } from '../../core/fs/path';
2832
import type { ChatRuntime } from '../../core/runtime/chat-runtime';
@@ -145,6 +149,8 @@ export class QoderChatRuntime implements ChatRuntime {
145149
private persistentQuery: Query | null = null;
146150
private messageChannel: QoderMessageChannel | null = null;
147151
private queryAbortController: AbortController | null = null;
152+
/** Set when the persistent Query is created; used to time CLI cold-start. */
153+
private persistentQueryCreatedAt: number | null = null;
148154
private readonly responseRouter: QoderResponseRouter;
149155
private responseConsumerRunning = false;
150156
private responseConsumerPromise: Promise<void> | null = null;
@@ -419,10 +425,13 @@ export class QoderChatRuntime implements ChatRuntime {
419425
externalContextPaths
420426
);
421427

428+
const spawnStartedAt = performance.now();
422429
this.persistentQuery = agentQuery({
423430
prompt: this.messageChannel,
424431
options,
425432
});
433+
logElapsed('runtime.spawnPersistentQuery', spawnStartedAt);
434+
this.persistentQueryCreatedAt = performance.now();
426435

427436
if (this.pendingResumeAt === resumeAtMessageId) {
428437
this.pendingResumeAt = undefined;
@@ -481,6 +490,7 @@ export class QoderChatRuntime implements ChatRuntime {
481490
this.persistentQuery = null;
482491
this.messageChannel = null;
483492
this.queryAbortController = null;
493+
this.persistentQueryCreatedAt = null;
484494
this.responseConsumerRunning = false;
485495
this.responseConsumerPromise = null;
486496
this.currentConfig = null;
@@ -628,7 +638,15 @@ export class QoderChatRuntime implements ChatRuntime {
628638
if (!this.persistentQuery) return;
629639

630640
try {
641+
let sawFirstMessage = false;
631642
for await (const message of this.persistentQuery) {
643+
if (!sawFirstMessage) {
644+
sawFirstMessage = true;
645+
if (this.persistentQueryCreatedAt !== null) {
646+
// CLI cold-start: from Query creation to the first runtime message.
647+
logElapsed('runtime.cliFirstMessage', this.persistentQueryCreatedAt);
648+
}
649+
}
632650
if (this.shuttingDown) break;
633651

634652
await this.responseRouter.route(message);
@@ -946,7 +964,7 @@ export class QoderChatRuntime implements ChatRuntime {
946964
const savedPreapprovedTools = this.currentPreapprovedTools;
947965

948966
// Apply dynamic updates before sending (Phase 1.6)
949-
await this.applyDynamicUpdates(queryOptions);
967+
await measureAsync('runtime.applyDynamicUpdates', () => this.applyDynamicUpdates(queryOptions));
950968

951969
// Restore turn pre-approvals in case a dynamic update restarted the Query.
952970
this.currentPreapprovedTools = savedPreapprovedTools;

‎src/qoder/services/qoder-title-generation-service.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { measureAsync } from '../../core/diagnostics/performance';
12
import type {
23
TitleGenerationCallback,
34
TitleGenerationResult,
@@ -36,15 +37,15 @@ export class QoderTitleGenerationService {
3637
const prompt = `User's request:\n"""\n${truncatedUser}\n"""\n\nGenerate a title for this conversation:`;
3738

3839
try {
39-
const result = await runColdStartQuery({
40+
const result = await measureAsync('title.coldStartQuery', () => runColdStartQuery({
4041
plugin: this.plugin,
4142
systemPrompt: TITLE_GENERATION_SYSTEM_PROMPT,
4243
tools: [],
4344
model: this.resolveTitleModel(),
4445
thinking: { disabled: true },
4546
persistSession: false,
4647
abortController,
47-
}, prompt);
48+
}, prompt));
4849

4950
const title = this.parseTitle(result.text);
5051
if (title) {

‎tests/unit/core/diagnostics/performance.test.ts‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { measureAsync } from '@/core/diagnostics/performance';
1+
import {
2+
logElapsed,
3+
measure,
4+
measureAsync,
5+
} from '@/core/diagnostics/performance';
26

37
describe('measureAsync', () => {
48
let infoSpy: jest.SpyInstance;
@@ -37,3 +41,60 @@ describe('measureAsync', () => {
3741
expect(infoSpy.mock.calls[0][0]).toMatch(/^\[qoderian perf\] stage\.failing: /);
3842
});
3943
});
44+
45+
describe('measure', () => {
46+
let infoSpy: jest.SpyInstance;
47+
48+
beforeEach(() => {
49+
infoSpy = jest.spyOn(console, 'info').mockImplementation(() => {});
50+
});
51+
52+
afterEach(() => {
53+
infoSpy.mockRestore();
54+
});
55+
56+
it('returns the wrapped result unchanged', () => {
57+
const result = measure('stage.sync', () => 42);
58+
expect(result).toBe(42);
59+
});
60+
61+
it('logs the label with an elapsed-time suffix', () => {
62+
measure('stage.sync', () => undefined);
63+
64+
expect(infoSpy).toHaveBeenCalledTimes(1);
65+
expect(infoSpy.mock.calls[0][0]).toMatch(/^\[qoderian perf\] stage\.sync: \d+(\.\d+)?ms$/);
66+
});
67+
68+
it('logs timing and rethrows when the wrapped operation fails', () => {
69+
const boom = new Error('boom');
70+
71+
expect(() =>
72+
measure('stage.failing', () => {
73+
throw boom;
74+
})).toThrow(boom);
75+
76+
expect(infoSpy).toHaveBeenCalledTimes(1);
77+
expect(infoSpy.mock.calls[0][0]).toMatch(/^\[qoderian perf\] stage\.failing: /);
78+
});
79+
});
80+
81+
describe('logElapsed', () => {
82+
let infoSpy: jest.SpyInstance;
83+
84+
beforeEach(() => {
85+
infoSpy = jest.spyOn(console, 'info').mockImplementation(() => {});
86+
});
87+
88+
afterEach(() => {
89+
infoSpy.mockRestore();
90+
});
91+
92+
it('logs the elapsed time since the given origin', () => {
93+
const startedAt = performance.now();
94+
95+
logElapsed('turn.firstChunk', startedAt);
96+
97+
expect(infoSpy).toHaveBeenCalledTimes(1);
98+
expect(infoSpy.mock.calls[0][0]).toMatch(/^\[qoderian perf\] turn\.firstChunk: \d+(\.\d+)?ms$/);
99+
});
100+
});

0 commit comments

Comments
 (0)