Skip to content

Commit f7302c1

Browse files
committed
better formatting for performance results
1 parent f3cb823 commit f7302c1

File tree

3 files changed

+53
-41
lines changed

3 files changed

+53
-41
lines changed
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CommandModule } from 'yargs';
2+
23
import { SharedOptions } from '../options.js';
34

45
export const command: CommandModule<object, SharedOptions> = {
@@ -7,8 +8,8 @@ export const command: CommandModule<object, SharedOptions> = {
78
handler: async () => {
89
console.log('Profile test completed successfully');
910
// Profiling report will be automatically displayed by the main function
10-
11+
1112
// Force a delay to simulate some processing
12-
await new Promise(resolve => setTimeout(resolve, 100));
13+
await new Promise((resolve) => setTimeout(resolve, 100));
1314
},
14-
};
15+
};

packages/cli/src/index.ts

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import { hideBin } from 'yargs/helpers';
77

88
import { command as defaultCommand } from './commands/$default.js';
99
import { command as configCommand } from './commands/config.js';
10-
import { command as testSentryCommand } from './commands/test-sentry.js';
1110
import { command as testProfileCommand } from './commands/test-profile.js';
11+
import { command as testSentryCommand } from './commands/test-sentry.js';
1212
import { command as toolsCommand } from './commands/tools.js';
1313
import { sharedOptions } from './options.js';
14-
import { getConfig } from './settings/config.js';
1514
import { initSentry, captureException } from './sentry/index.js';
15+
import { getConfig } from './settings/config.js';
1616
import { enableProfiling, mark, reportTimings } from './utils/performance.js';
1717

1818
mark('After imports');
@@ -27,30 +27,35 @@ mark('After sourceMapSupport install');
2727

2828
const main = async () => {
2929
// Parse argv early to check for profiling flag
30-
const parsedArgv = await yargs(hideBin(process.argv)).options(sharedOptions).parse();
31-
30+
const parsedArgv = await yargs(hideBin(process.argv))
31+
.options(sharedOptions)
32+
.parse();
33+
3234
// Get config to check for profile setting
3335
const config = getConfig();
34-
36+
3537
// Enable profiling if --profile flag is set or if enabled in config
3638
enableProfiling(Boolean(parsedArgv.profile) || Boolean(config.profile));
37-
39+
3840
mark('Main function start');
39-
41+
4042
dotenv.config();
4143
mark('After dotenv config');
42-
44+
4345
// Only initialize Sentry if needed
44-
if (process.env.NODE_ENV !== 'development' || process.env.ENABLE_SENTRY === 'true') {
46+
if (
47+
process.env.NODE_ENV !== 'development' ||
48+
process.env.ENABLE_SENTRY === 'true'
49+
) {
4550
initSentry();
4651
mark('After Sentry init');
4752
}
48-
53+
4954
mark('Before package.json load');
5055
const require = createRequire(import.meta.url);
5156
const packageInfo = require('../package.json') as PackageJson;
5257
mark('After package.json load');
53-
58+
5459
// Set up yargs with the new CLI interface
5560
mark('Before yargs setup');
5661
await yargs(hideBin(process.argv))
@@ -69,17 +74,16 @@ const main = async () => {
6974
.strict()
7075
.showHelpOnFail(true)
7176
.help().argv;
72-
mark('After yargs setup');
73-
74-
// Report timings if profiling is enabled
75-
await reportTimings();
7677
};
7778

78-
await main().catch(async (error) => {
79-
console.error(error);
80-
// Report profiling data even if there's an error
81-
await reportTimings();
82-
// Capture the error with Sentry
83-
captureException(error);
84-
process.exit(1);
85-
});
79+
await main()
80+
.catch(async (error) => {
81+
console.error(error);
82+
// Capture the error with Sentry
83+
captureException(error);
84+
process.exit(1);
85+
})
86+
.finally(async () => {
87+
// Report timings if profiling is enabled
88+
await reportTimings();
89+
});

packages/cli/src/utils/performance.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,26 @@ export function mark(label: string): void {
2525
*/
2626
export async function reportTimings(): Promise<void> {
2727
if (!isEnabled) return;
28-
29-
console.log('\n📊 Performance Profile:');
28+
29+
console.log('\n📊 Performance Profile Results');
3030
console.log('=======================');
31-
31+
console.log(
32+
`${'Label'.padEnd(40, ' ')}${'Time'.padStart(10, ' ')}${'Duration'.padStart(10, ' ')}`,
33+
);
34+
3235
// Sort timings by time value
33-
const sortedTimings = Object.entries(timings)
34-
.sort((a, b) => a[1] - b[1]);
35-
36+
const sortedTimings = Object.entries(timings).sort((a, b) => a[1] - b[1]);
37+
3638
// Calculate durations between steps
3739
let previousTime = 0;
3840
for (const [label, time] of sortedTimings) {
3941
const duration = time - previousTime;
40-
console.log(`${label}: ${time.toFixed(2)}ms (${duration.toFixed(2)}ms)`);
42+
console.log(
43+
`${label.padEnd(40, ' ')}${`${time.toFixed(2)}ms`.padStart(10, ' ')}${`${duration.toFixed(2)}ms`.padStart(10, ' ')}`,
44+
);
4145
previousTime = time;
4246
}
43-
47+
4448
console.log(`Total startup time: ${previousTime.toFixed(2)}ms`);
4549
console.log('=======================\n');
4650

@@ -55,34 +59,37 @@ export async function reportTimings(): Promise<void> {
5559
*/
5660
async function reportPlatformInfo(): Promise<void> {
5761
if (!isEnabled) return;
58-
62+
5963
console.log('\n🖥️ Platform Information:');
6064
console.log('=======================');
6165
console.log(`Platform: ${process.platform}`);
6266
console.log(`Architecture: ${process.arch}`);
6367
console.log(`Node.js version: ${process.version}`);
64-
68+
6569
// Windows-specific information
6670
if (process.platform === 'win32') {
6771
console.log('Windows-specific details:');
6872
console.log(`- Current working directory: ${process.cwd()}`);
6973
console.log(`- Path length: ${process.cwd().length} characters`);
70-
74+
7175
// Check for antivirus markers by measuring file read time
7276
try {
7377
// Using dynamic import to avoid require
7478
const fs = await import('fs');
7579
const startTime = performance.now();
7680
fs.readFileSync(process.execPath);
77-
console.log(`- Time to read Node.js executable: ${(performance.now() - startTime).toFixed(2)}ms`);
81+
console.log(
82+
`- Time to read Node.js executable: ${(performance.now() - startTime).toFixed(2)}ms`,
83+
);
7884
} catch (error: unknown) {
79-
const errorMessage = error instanceof Error ? error.message : String(error);
85+
const errorMessage =
86+
error instanceof Error ? error.message : String(error);
8087
console.log(`- Error reading Node.js executable: ${errorMessage}`);
8188
}
8289
}
83-
90+
8491
console.log('=======================\n');
8592
}
8693

8794
// Initial mark for module load time
88-
mark('Module initialization');
95+
mark('Module initialization');

0 commit comments

Comments
 (0)