mirror of
https://github.com/QwenLM/qwen-code.git
synced 2026-04-28 11:41:04 +00:00
feat(cli): add startup performance profiler (#3219) Add a lightweight startup profiler activated via QWEN_CODE_PROFILE_STARTUP=1. When enabled, collects performance.now() timestamps at 7 key phases in main() and writes a JSON report to ~/.qwen/startup-perf/. Also records process.uptime() at T0 to capture module loading time not covered by checkpoint-based measurement. Key design decisions: - Only profiles inside sandbox child process to avoid duplicate reports - initStartupProfiler() is idempotent (resets state on each call) - Filename uses report.sessionId for consistency with JSON content - Zero overhead when disabled (single env var check) Initial measurement: module loading ~1342ms (94%), main() ~85ms (6%), confirming barrel exports and eager dependency loading as primary optimization targets for #3011. Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
94 lines
2.4 KiB
JavaScript
94 lines
2.4 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
/**
|
|
* @license
|
|
* Copyright 2025 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
import { initStartupProfiler } from './src/utils/startupProfiler.js';
|
|
|
|
// Must run before any other imports to capture the earliest possible T0.
|
|
initStartupProfiler();
|
|
|
|
import './src/gemini.js';
|
|
import { main } from './src/gemini.js';
|
|
import { FatalError } from '@qwen-code/qwen-code-core';
|
|
import { writeStderrLine } from './src/utils/stdioHelpers.js';
|
|
|
|
// --- Global Entry Point ---
|
|
|
|
// Suppress known race conditions in @lydell/node-pty.
|
|
//
|
|
// PTY errors that are expected due to timing races between process exit
|
|
// and I/O operations. These should not crash the app.
|
|
//
|
|
// References:
|
|
// - https://github.com/microsoft/node-pty/issues/178 (EIO on macOS/Linux)
|
|
// - https://github.com/microsoft/node-pty/issues/827 (resize on Windows)
|
|
const getErrnoCode = (error: unknown): string | undefined => {
|
|
if (!error || typeof error !== 'object') {
|
|
return undefined;
|
|
}
|
|
const code = (error as { code?: unknown }).code;
|
|
return typeof code === 'string' ? code : undefined;
|
|
};
|
|
|
|
const isExpectedPtyRaceError = (error: unknown): boolean => {
|
|
if (!(error instanceof Error)) {
|
|
return false;
|
|
}
|
|
|
|
const message = error.message;
|
|
const code = getErrnoCode(error);
|
|
|
|
// EIO: PTY read race on macOS/Linux - code + PTY context required
|
|
// https://github.com/microsoft/node-pty/issues/178
|
|
if (
|
|
(code === 'EIO' && message.includes('read')) ||
|
|
message.includes('read EIO')
|
|
) {
|
|
return true;
|
|
}
|
|
|
|
// PTY-specific resize/exit race errors - require PTY context in message
|
|
if (
|
|
message.includes('ioctl(2) failed, EBADF') ||
|
|
message.includes('Cannot resize a pty that has already exited')
|
|
) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
};
|
|
|
|
process.on('uncaughtException', (error) => {
|
|
if (isExpectedPtyRaceError(error)) {
|
|
return;
|
|
}
|
|
|
|
if (error instanceof Error) {
|
|
writeStderrLine(error.stack ?? error.message);
|
|
} else {
|
|
writeStderrLine(String(error));
|
|
}
|
|
process.exit(1);
|
|
});
|
|
|
|
main().catch((error) => {
|
|
if (error instanceof FatalError) {
|
|
let errorMessage = error.message;
|
|
if (!process.env['NO_COLOR']) {
|
|
errorMessage = `\x1b[31m${errorMessage}\x1b[0m`;
|
|
}
|
|
console.error(errorMessage);
|
|
process.exit(error.exitCode);
|
|
}
|
|
console.error('An unexpected critical error occurred:');
|
|
if (error instanceof Error) {
|
|
console.error(error.stack);
|
|
} else {
|
|
console.error(String(error));
|
|
}
|
|
process.exit(1);
|
|
});
|