Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
chore(scripts): improve performance analysis script by timing out earlier
Elian Doranc834c01c8ec9
diff --git a/scripts/analyze-perf.ts b/scripts/analyze-perf.tsindex e76d9ca8ad..96305f88c4 100644--- a/scripts/analyze-perf.ts+++ b/scripts/analyze-perf.ts@@ -235,6 +235,45 @@ function analyzePerf(entries: PerfEntry[]) {console.log('\n' + '='.repeat(80) + '\n');}+// Helper to make HTTP request to trigger Vite rendering+async function makeHttpRequest(): Promise<void> {+ return new Promise((resolve) => {+ console.log('\nπ Making request to http://localhost:8080 to trigger Vite...');++ const http = require('http');+ const req = http.get('http://localhost:8080', (res: any) => {+ console.log(` β Response status: ${res.statusCode}`);+ console.log(` Response headers:`, res.headers);++ let body = '';+ res.on('data', (chunk: any) => {+ body += chunk;+ });++ res.on('end', () => {+ console.log(` Response body length: ${body.length} bytes`);+ if (body.length < 1000) {+ console.log(` Response body: ${body}`);+ } else {+ console.log(` Response body preview (first 500 chars):\n${body.substring(0, 500)}`);+ }+ resolve();+ });+ });++ req.on('error', (err: any) => {+ console.log(` β Request failed: ${err.message}`);+ resolve(); // Continue anyway+ });++ req.setTimeout(5000, () => {+ console.log(` β±οΈ Request timed out after 5 seconds`);+ req.destroy();+ resolve();+ });+ });+}+// Main - runs pnpm server:start and analyzes output automaticallyasync function runPerfAnalysis() {console.log('π Starting pnpm server:start with DEBUG=vite:*...\n');@@ -245,7 +284,11 @@ async function runPerfAnalysis() {return new Promise<void>((resolve, reject) => {const child = spawn('pnpm', ['server:start'], {- env: { ...process.env, DEBUG: 'vite:*' },+ env: {+ ...process.env,+ DEBUG: 'vite:*',+ TRILIUM_GENERAL_NOAUTHENTICATION: '1'+ },shell: true,cwd: path.join(__dirname, '..'),stdio: ['ignore', 'pipe', 'pipe']@@ -254,28 +297,22 @@ async function runPerfAnalysis() {child.stdout?.setEncoding('utf8');child.stderr?.setEncoding('utf8');- child.stdout?.on('data', (data) => {- dataCount++;- const text = String(data);- lines.push(...text.split('\n'));- process.stdout.write('.');- });-- child.stderr?.on('data', (data) => {- dataCount++;- const text = String(data);- lines.push(...text.split('\n'));- process.stdout.write('.');- });+ let lastActivityTime = Date.now();+ let maxTimeoutHandle: NodeJS.Timeout;+ let inactivityCheckInterval: NodeJS.Interval;+ let serverStarted = false;- // Wait 60 seconds then kill the process- setTimeout(() => {+ const finishCollection = () => {+ clearTimeout(maxTimeoutHandle);+ clearInterval(inactivityCheckInterval);child.kill('SIGTERM');// Give it a moment to clean upsetTimeout(() => {+ const elapsedSeconds = ((Date.now() - lastActivityTime) / 1000).toFixed(1);console.log('\n\nβ Collected output, analyzing...\n');console.log(` Received ${dataCount} data chunks, ${lines.length} lines`);+ console.log(` Last activity was ${elapsedSeconds}s ago`);const entries = parsePerfFromLines(lines);console.log(`π Found ${entries.length} performance entries`);@@ -294,8 +331,48 @@ async function runPerfAnalysis() {analyzePerf(entries);resolve();}, 1000);+ };++ child.stdout?.on('data', (data) => {+ dataCount++;+ lastActivityTime = Date.now();+ const text = String(data);+ lines.push(...text.split('\n'));+ process.stdout.write('.');++ // Check if server has started+ if (!serverStarted && text.includes('Listening on')) {+ serverStarted = true;+ // Wait 2 seconds for Vite to initialize, then make a request+ setTimeout(async () => {+ await makeHttpRequest();+ }, 2000);+ }+ });++ child.stderr?.on('data', (data) => {+ dataCount++;+ lastActivityTime = Date.now();+ const text = String(data);+ lines.push(...text.split('\n'));+ process.stdout.write('.');+ });++ // Maximum timeout of 60 seconds+ maxTimeoutHandle = setTimeout(() => {+ console.log('\nβ±οΈ Reached 60 second maximum timeout');+ finishCollection();}, 60000);+ // Check every second for 10 seconds of inactivity+ inactivityCheckInterval = setInterval(() => {+ const inactiveSeconds = (Date.now() - lastActivityTime) / 1000;+ if (inactiveSeconds >= 10) {+ console.log('\nβ±οΈ No activity for 10 seconds, finishing...');+ finishCollection();+ }+ }, 1000);+child.on('error', (error) => {console.error(`β Failed to start process: ${error.message}`);reject(error);