@@ -42,6 +42,7 @@ import path from 'path';
42
42
import { promisify } from 'util' ;
43
43
import { getOsInfo } from './get-os-info' ;
44
44
import { UpdateNotificationManager } from './update-notification-manager' ;
45
+ import { markTime } from './startup-timing' ;
45
46
46
47
/**
47
48
* Connecting text key.
@@ -226,9 +227,11 @@ export class CliRepl implements MongoshIOProvider {
226
227
) : Promise < void > {
227
228
const { version } = require ( '../package.json' ) ;
228
229
await this . verifyNodeVersion ( ) ;
230
+ markTime ( 'verified node version' ) ;
229
231
230
232
this . isContainerizedEnvironment =
231
233
await this . getIsContainerizedEnvironment ( ) ;
234
+ markTime ( 'checked containerized environment' ) ;
232
235
233
236
if ( ! this . cliOptions . nodb ) {
234
237
const cs = new ConnectionString ( driverUri ) ;
@@ -249,19 +252,23 @@ export class CliRepl implements MongoshIOProvider {
249
252
} catch ( err : any ) {
250
253
this . warnAboutInaccessibleFile ( err ) ;
251
254
}
255
+ markTime ( 'ensured shell homedir' ) ;
252
256
253
257
await this . logManager . cleanupOldLogfiles ( ) ;
258
+ markTime ( 'cleaned up log files' ) ;
254
259
const logger = await this . logManager . createLogWriter ( ) ;
255
260
if ( ! this . cliOptions . quiet ) {
256
261
this . output . write ( `Current Mongosh Log ID:\t${ logger . logId } \n` ) ;
257
262
}
258
263
this . logWriter = logger ;
264
+ markTime ( 'instantiated log writer' ) ;
259
265
260
266
logger . info ( 'MONGOSH' , mongoLogId ( 1_000_000_000 ) , 'log' , 'Starting log' , {
261
267
execPath : process . execPath ,
262
268
envInfo : redactSensitiveData ( this . getLoggedEnvironmentVariables ( ) ) ,
263
269
...( await buildInfo ( ) ) ,
264
270
} ) ;
271
+ markTime ( 'logged initial message' ) ;
265
272
266
273
let analyticsSetupError : Error | null = null ;
267
274
try {
@@ -272,6 +279,7 @@ export class CliRepl implements MongoshIOProvider {
272
279
analyticsSetupError = err ;
273
280
}
274
281
282
+ markTime ( 'created analytics instance' ) ;
275
283
setupLoggerAndTelemetry (
276
284
this . bus ,
277
285
logger ,
@@ -284,6 +292,7 @@ export class CliRepl implements MongoshIOProvider {
284
292
} ,
285
293
version
286
294
) ;
295
+ markTime ( 'completed telemetry setup' ) ;
287
296
288
297
if ( analyticsSetupError ) {
289
298
this . bus . emit ( 'mongosh:error' , analyticsSetupError , 'analytics' ) ;
@@ -298,6 +307,7 @@ export class CliRepl implements MongoshIOProvider {
298
307
}
299
308
300
309
this . globalConfig = await this . loadGlobalConfigFile ( ) ;
310
+ markTime ( 'read global config files' ) ;
301
311
302
312
// Needs to happen after loading the mongosh config file(s)
303
313
void this . fetchMongoshUpdateUrl ( ) ;
@@ -331,6 +341,7 @@ export class CliRepl implements MongoshIOProvider {
331
341
}
332
342
333
343
driverOptions = await this . prepareOIDCOptions ( driverOptions ) ;
344
+ markTime ( 'prepared OIDC options' ) ;
334
345
335
346
let initialServiceProvider ;
336
347
try {
@@ -346,10 +357,12 @@ export class CliRepl implements MongoshIOProvider {
346
357
}
347
358
throw err ;
348
359
}
360
+ markTime ( 'completed SP setup' ) ;
349
361
const initialized = await this . mongoshRepl . initialize (
350
362
initialServiceProvider ,
351
363
await this . getMoreRecentMongoshVersion ( )
352
364
) ;
365
+ markTime ( 'initialized mongosh repl' ) ;
353
366
this . injectReplFunctions ( ) ;
354
367
355
368
const commandLineLoadFiles = this . cliOptions . fileNames ?? [ ] ;
@@ -388,6 +401,7 @@ export class CliRepl implements MongoshIOProvider {
388
401
this . mongoshRepl
389
402
) ,
390
403
} ) ;
404
+ markTime ( 'set up editor' ) ;
391
405
392
406
if ( willExecuteCommandLineScripts ) {
393
407
this . mongoshRepl . setIsInteractive ( willEnterInteractiveMode ) ;
@@ -425,16 +439,20 @@ export class CliRepl implements MongoshIOProvider {
425
439
* - Programmatic users who really want a dependency on a snippet can use
426
440
* snippet('load-all') to explicitly load snippets
427
441
*/
442
+ markTime ( 'start load snippets' ) ;
428
443
await snippetManager ?. loadAllSnippets ( ) ;
444
+ markTime ( 'done load snippets' ) ;
429
445
}
430
446
await this . loadRcFiles ( ) ;
447
+ markTime ( 'loaded rc files' ) ;
431
448
432
449
this . verifyPlatformSupport ( ) ;
433
450
434
451
// We only enable/disable here, since the rc file/command line scripts
435
452
// can disable the telemetry setting.
436
453
this . setTelemetryEnabled ( await this . getConfig ( 'enableTelemetry' ) ) ;
437
454
this . bus . emit ( 'mongosh:start-mongosh-repl' , { version } ) ;
455
+ markTime ( 'starting repl' ) ;
438
456
await this . mongoshRepl . startRepl ( initialized ) ;
439
457
}
440
458
@@ -514,13 +532,15 @@ export class CliRepl implements MongoshIOProvider {
514
532
let lastEvalResult : any ;
515
533
let exitCode = 0 ;
516
534
try {
535
+ markTime ( 'start eval scripts' ) ;
517
536
for ( const script of evalScripts ) {
518
537
this . bus . emit ( 'mongosh:eval-cli-script' ) ;
519
538
lastEvalResult = await this . mongoshRepl . loadExternalCode (
520
539
script ,
521
540
'@(shell eval)'
522
541
) ;
523
542
}
543
+ markTime ( 'finished eval scripts' ) ;
524
544
} catch ( err ) {
525
545
// We have two distinct flows of control in the exception case;
526
546
// if we are running in --json mode, we treat the error as a
@@ -559,6 +579,7 @@ export class CliRepl implements MongoshIOProvider {
559
579
this . output . write ( formattedResult + '\n' ) ;
560
580
}
561
581
582
+ markTime ( 'wrote eval output, start loading external files' ) ;
562
583
for ( const file of files ) {
563
584
if ( ! this . cliOptions . quiet ) {
564
585
this . output . write (
@@ -567,6 +588,7 @@ export class CliRepl implements MongoshIOProvider {
567
588
}
568
589
await this . mongoshRepl . loadExternalFile ( file ) ;
569
590
}
591
+ markTime ( 'finished external files' ) ;
570
592
return exitCode ;
571
593
}
572
594
@@ -951,6 +973,7 @@ export class CliRepl implements MongoshIOProvider {
951
973
* Close all open resources held by this REPL instance.
952
974
*/
953
975
async close ( ) : Promise < void > {
976
+ markTime ( 'start closing' ) ;
954
977
if ( this . closing ) {
955
978
return ;
956
979
}
@@ -970,6 +993,7 @@ export class CliRepl implements MongoshIOProvider {
970
993
await new Promise ( ( resolve ) => this . output . write ( '' , resolve ) ) ;
971
994
}
972
995
}
996
+ markTime ( 'output flushed' ) ;
973
997
this . closing = true ;
974
998
const analytics = this . toggleableAnalytics ;
975
999
let flushError : string | null = null ;
@@ -978,6 +1002,7 @@ export class CliRepl implements MongoshIOProvider {
978
1002
const flushStart = Date . now ( ) ;
979
1003
try {
980
1004
await promisify ( analytics . flush . bind ( analytics ) ) ( ) ;
1005
+ markTime ( 'flushed analytics' ) ;
981
1006
} catch ( err : any ) {
982
1007
flushError = err . message ;
983
1008
} finally {
@@ -995,6 +1020,7 @@ export class CliRepl implements MongoshIOProvider {
995
1020
}
996
1021
) ;
997
1022
await this . logWriter ?. flush ( ) ;
1023
+ markTime ( 'flushed log writer' ) ;
998
1024
this . bus . emit ( 'mongosh:closed' ) ;
999
1025
}
1000
1026
0 commit comments