@@ -37,13 +37,17 @@ import {
37
37
SampledAnalytics ,
38
38
} from '@mongosh/logging' ;
39
39
import type { MongoshBus } from '@mongosh/types' ;
40
- import { CliUserConfig , CliUserConfigValidator } from '@mongosh/types' ;
40
+ import {
41
+ CliUserConfig ,
42
+ CliUserConfigValidator ,
43
+ TimingCategories ,
44
+ } from '@mongosh/types' ;
41
45
import { promises as fs } from 'fs' ;
42
46
import path from 'path' ;
43
47
import { promisify } from 'util' ;
44
48
import { getOsInfo } from './get-os-info' ;
45
49
import { UpdateNotificationManager } from './update-notification-manager' ;
46
- import { markTime } from './startup-timing' ;
50
+ import { getTimingData , markTime , summariseTimingData } from './startup-timing' ;
47
51
48
52
/**
49
53
* Connecting text key.
@@ -228,11 +232,14 @@ export class CliRepl implements MongoshIOProvider {
228
232
) : Promise < void > {
229
233
const { version } = require ( '../package.json' ) ;
230
234
await this . verifyNodeVersion ( ) ;
231
- markTime ( 'verified node version' ) ;
235
+ markTime ( TimingCategories . REPLInstantiation , 'verified node version' ) ;
232
236
233
237
this . isContainerizedEnvironment =
234
238
await this . getIsContainerizedEnvironment ( ) ;
235
- markTime ( 'checked containerized environment' ) ;
239
+ markTime (
240
+ TimingCategories . REPLInstantiation ,
241
+ 'checked containerized environment'
242
+ ) ;
236
243
237
244
if ( ! this . cliOptions . nodb ) {
238
245
const cs = new ConnectionString ( driverUri ) ;
@@ -253,23 +260,23 @@ export class CliRepl implements MongoshIOProvider {
253
260
} catch ( err : any ) {
254
261
this . warnAboutInaccessibleFile ( err ) ;
255
262
}
256
- markTime ( 'ensured shell homedir' ) ;
263
+ markTime ( TimingCategories . REPLInstantiation , 'ensured shell homedir' ) ;
257
264
258
265
await this . logManager . cleanupOldLogfiles ( ) ;
259
- markTime ( 'cleaned up log files' ) ;
266
+ markTime ( TimingCategories . Logging , 'cleaned up log files' ) ;
260
267
const logger = await this . logManager . createLogWriter ( ) ;
261
268
if ( ! this . cliOptions . quiet ) {
262
269
this . output . write ( `Current Mongosh Log ID:\t${ logger . logId } \n` ) ;
263
270
}
264
271
this . logWriter = logger ;
265
- markTime ( 'instantiated log writer' ) ;
272
+ markTime ( TimingCategories . Logging , 'instantiated log writer' ) ;
266
273
267
274
logger . info ( 'MONGOSH' , mongoLogId ( 1_000_000_000 ) , 'log' , 'Starting log' , {
268
275
execPath : process . execPath ,
269
276
envInfo : redactSensitiveData ( this . getLoggedEnvironmentVariables ( ) ) ,
270
277
...( await buildInfo ( ) ) ,
271
278
} ) ;
272
- markTime ( 'logged initial message' ) ;
279
+ markTime ( TimingCategories . Logging , 'logged initial message' ) ;
273
280
274
281
let analyticsSetupError : Error | null = null ;
275
282
try {
@@ -280,7 +287,7 @@ export class CliRepl implements MongoshIOProvider {
280
287
analyticsSetupError = err ;
281
288
}
282
289
283
- markTime ( 'created analytics instance' ) ;
290
+ markTime ( TimingCategories . Telemetry , 'created analytics instance' ) ;
284
291
setupLoggerAndTelemetry (
285
292
this . bus ,
286
293
logger ,
@@ -293,7 +300,7 @@ export class CliRepl implements MongoshIOProvider {
293
300
} ,
294
301
version
295
302
) ;
296
- markTime ( 'completed telemetry setup' ) ;
303
+ markTime ( TimingCategories . Telemetry , 'completed telemetry setup' ) ;
297
304
298
305
if ( analyticsSetupError ) {
299
306
this . bus . emit ( 'mongosh:error' , analyticsSetupError , 'analytics' ) ;
@@ -308,7 +315,7 @@ export class CliRepl implements MongoshIOProvider {
308
315
}
309
316
310
317
this . globalConfig = await this . loadGlobalConfigFile ( ) ;
311
- markTime ( 'read global config files' ) ;
318
+ markTime ( TimingCategories . UserConfigLoading , 'read global config files' ) ;
312
319
313
320
// Needs to happen after loading the mongosh config file(s)
314
321
void this . fetchMongoshUpdateUrl ( ) ;
@@ -342,7 +349,7 @@ export class CliRepl implements MongoshIOProvider {
342
349
}
343
350
344
351
driverOptions = await this . prepareOIDCOptions ( driverOptions ) ;
345
- markTime ( 'prepared OIDC options' ) ;
352
+ markTime ( TimingCategories . DriverSetup , 'prepared OIDC options' ) ;
346
353
347
354
let initialServiceProvider ;
348
355
try {
@@ -358,12 +365,12 @@ export class CliRepl implements MongoshIOProvider {
358
365
}
359
366
throw err ;
360
367
}
361
- markTime ( 'completed SP setup' ) ;
368
+ markTime ( TimingCategories . DriverSetup , 'completed SP setup' ) ;
362
369
const initialized = await this . mongoshRepl . initialize (
363
370
initialServiceProvider ,
364
371
await this . getMoreRecentMongoshVersion ( )
365
372
) ;
366
- markTime ( 'initialized mongosh repl' ) ;
373
+ markTime ( TimingCategories . REPLInstantiation , 'initialized mongosh repl' ) ;
367
374
this . injectReplFunctions ( ) ;
368
375
369
376
const commandLineLoadFiles = this . cliOptions . fileNames ?? [ ] ;
@@ -402,17 +409,25 @@ export class CliRepl implements MongoshIOProvider {
402
409
this . mongoshRepl
403
410
) ,
404
411
} ) ;
405
- markTime ( 'set up editor' ) ;
412
+ markTime ( TimingCategories . REPLInstantiation , 'set up editor' ) ;
406
413
407
414
if ( willExecuteCommandLineScripts ) {
408
415
this . mongoshRepl . setIsInteractive ( willEnterInteractiveMode ) ;
416
+
417
+ this . bus . emit ( 'mongosh:start-session' , {
418
+ isInteractive : false ,
419
+ timings : summariseTimingData ( getTimingData ( ) ) ,
420
+ } ) ;
421
+
409
422
this . bus . emit ( 'mongosh:start-loading-cli-scripts' , {
410
423
usesShellOption : ! ! this . cliOptions . shell ,
411
424
} ) ;
425
+
412
426
const exitCode = await this . loadCommandLineFilesAndEval (
413
427
commandLineLoadFiles ,
414
428
evalScripts
415
429
) ;
430
+
416
431
if ( exitCode !== 0 ) {
417
432
await this . exit ( exitCode ) ;
418
433
return ;
@@ -440,21 +455,27 @@ export class CliRepl implements MongoshIOProvider {
440
455
* - Programmatic users who really want a dependency on a snippet can use
441
456
* snippet('load-all') to explicitly load snippets
442
457
*/
443
- markTime ( 'start load snippets' ) ;
458
+ markTime ( TimingCategories . SnippetLoading , 'start load snippets' ) ;
444
459
await snippetManager ?. loadAllSnippets ( ) ;
445
- markTime ( 'done load snippets' ) ;
460
+ markTime ( TimingCategories . SnippetLoading , 'done load snippets' ) ;
446
461
}
462
+
463
+ markTime ( TimingCategories . ResourceFileLoading , 'loading rc files' ) ;
447
464
await this . loadRcFiles ( ) ;
448
- markTime ( 'loaded rc files' ) ;
465
+ markTime ( TimingCategories . ResourceFileLoading , 'loaded rc files' ) ;
449
466
450
467
this . verifyPlatformSupport ( ) ;
451
468
452
469
// We only enable/disable here, since the rc file/command line scripts
453
470
// can disable the telemetry setting.
454
471
this . setTelemetryEnabled ( await this . getConfig ( 'enableTelemetry' ) ) ;
455
472
this . bus . emit ( 'mongosh:start-mongosh-repl' , { version } ) ;
456
- markTime ( 'starting repl' ) ;
473
+ markTime ( TimingCategories . REPLInstantiation , 'starting repl' ) ;
457
474
await this . mongoshRepl . startRepl ( initialized ) ;
475
+ this . bus . emit ( 'mongosh:start-session' , {
476
+ isInteractive : true ,
477
+ timings : summariseTimingData ( getTimingData ( ) ) ,
478
+ } ) ;
458
479
}
459
480
460
481
injectReplFunctions ( ) : void {
@@ -537,15 +558,15 @@ export class CliRepl implements MongoshIOProvider {
537
558
let lastEvalResult : any ;
538
559
let exitCode = 0 ;
539
560
try {
540
- markTime ( 'start eval scripts' ) ;
561
+ markTime ( TimingCategories . Eval , 'start eval scripts' ) ;
541
562
for ( const script of evalScripts ) {
542
563
this . bus . emit ( 'mongosh:eval-cli-script' ) ;
543
564
lastEvalResult = await this . mongoshRepl . loadExternalCode (
544
565
script ,
545
566
'@(shell eval)'
546
567
) ;
547
568
}
548
- markTime ( 'finished eval scripts' ) ;
569
+ markTime ( TimingCategories . Eval , 'finished eval scripts' ) ;
549
570
} catch ( err ) {
550
571
// We have two distinct flows of control in the exception case;
551
572
// if we are running in --json mode, we treat the error as a
@@ -584,7 +605,8 @@ export class CliRepl implements MongoshIOProvider {
584
605
this . output . write ( formattedResult + '\n' ) ;
585
606
}
586
607
587
- markTime ( 'wrote eval output, start loading external files' ) ;
608
+ markTime ( TimingCategories . Eval , 'wrote eval output' ) ;
609
+ markTime ( TimingCategories . EvalFile , 'start loading external files' ) ;
588
610
for ( const file of files ) {
589
611
if ( ! this . cliOptions . quiet ) {
590
612
this . output . write (
@@ -593,7 +615,7 @@ export class CliRepl implements MongoshIOProvider {
593
615
}
594
616
await this . mongoshRepl . loadExternalFile ( file ) ;
595
617
}
596
- markTime ( 'finished external files' ) ;
618
+ markTime ( TimingCategories . EvalFile , 'finished external files' ) ;
597
619
return exitCode ;
598
620
}
599
621
@@ -978,7 +1000,7 @@ export class CliRepl implements MongoshIOProvider {
978
1000
* Close all open resources held by this REPL instance.
979
1001
*/
980
1002
async close ( ) : Promise < void > {
981
- markTime ( 'start closing' ) ;
1003
+ markTime ( TimingCategories . REPLInstantiation , 'start closing' ) ;
982
1004
if ( this . closing ) {
983
1005
return ;
984
1006
}
@@ -998,7 +1020,7 @@ export class CliRepl implements MongoshIOProvider {
998
1020
await new Promise ( ( resolve ) => this . output . write ( '' , resolve ) ) ;
999
1021
}
1000
1022
}
1001
- markTime ( 'output flushed' ) ;
1023
+ markTime ( TimingCategories . REPLInstantiation , 'output flushed' ) ;
1002
1024
this . closing = true ;
1003
1025
const analytics = this . toggleableAnalytics ;
1004
1026
let flushError : string | null = null ;
@@ -1007,7 +1029,7 @@ export class CliRepl implements MongoshIOProvider {
1007
1029
const flushStart = Date . now ( ) ;
1008
1030
try {
1009
1031
await promisify ( analytics . flush . bind ( analytics ) ) ( ) ;
1010
- markTime ( 'flushed analytics' ) ;
1032
+ markTime ( TimingCategories . Telemetry , 'flushed analytics' ) ;
1011
1033
} catch ( err : any ) {
1012
1034
flushError = err . message ;
1013
1035
} finally {
@@ -1025,7 +1047,7 @@ export class CliRepl implements MongoshIOProvider {
1025
1047
}
1026
1048
) ;
1027
1049
await this . logWriter ?. flush ( ) ;
1028
- markTime ( 'flushed log writer' ) ;
1050
+ markTime ( TimingCategories . Logging , 'flushed log writer' ) ;
1029
1051
this . bus . emit ( 'mongosh:closed' ) ;
1030
1052
}
1031
1053
0 commit comments