Skip to content

Commit 9ca67e4

Browse files
authored
Measure config parse / file scan time, actual total time (#1069)
1 parent bdee372 commit 9ca67e4

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

cmd/tsgo/sys.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ type osSys struct {
2121
defaultLibraryPath string
2222
newLine string
2323
cwd string
24+
start time.Time
25+
}
26+
27+
func (s *osSys) SinceStart() time.Duration {
28+
return time.Since(s.start)
2429
}
2530

2631
func (s *osSys) Now() time.Time {
@@ -65,5 +70,6 @@ func newSystem() *osSys {
6570
defaultLibraryPath: bundled.LibPath(),
6671
writer: os.Stdout,
6772
newLine: core.IfElse(runtime.GOOS == "windows", "\r\n", "\n"),
73+
start: time.Now(),
6874
}
6975
}

internal/execute/outputs.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ func reportStatistics(sys System, program *compiler.Program, result compileAndEm
7979
stats.add("Instantiations", program.InstantiationCount())
8080
stats.add("Memory used", fmt.Sprintf("%vK", memStats.Alloc/1024))
8181
stats.add("Memory allocs", strconv.FormatUint(memStats.Mallocs, 10))
82+
if result.configTime != 0 {
83+
stats.add("Config time", result.configTime)
84+
}
8285
stats.add("Parse time", result.parseTime)
8386
if result.bindTime != 0 {
8487
stats.add("Bind time", result.bindTime)

internal/execute/system.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import (
1010
type System interface {
1111
Writer() io.Writer
1212
EndWrite() // needed for testing
13-
Now() time.Time
1413
FS() vfs.FS
1514
DefaultLibraryPath() string
1615
GetCurrentDirectory() string
1716
NewLine() string // #241 eventually we want to use "\n"
17+
18+
Now() time.Time
19+
SinceStart() time.Duration
1820
}
1921

2022
type ExitStatus int

internal/execute/testsys_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func newTestSys(fileOrFolderList FileMap, cwd string, args ...string) *testSys {
2828
files: slices.Collect(maps.Keys(fileOrFolderList)),
2929
output: []string{},
3030
currentWrite: &strings.Builder{},
31+
start: time.Now(),
3132
}
3233
}
3334

@@ -41,6 +42,8 @@ type testSys struct {
4142
defaultLibraryPath string
4243
cwd string
4344
files []string
45+
46+
start time.Time
4447
}
4548

4649
func (s *testSys) IsTestDone() bool {
@@ -53,6 +56,10 @@ func (s *testSys) Now() time.Time {
5356
return time.Now()
5457
}
5558

59+
func (s *testSys) SinceStart() time.Duration {
60+
return time.Since(s.start)
61+
}
62+
5663
func (s *testSys) FS() vfs.FS {
5764
return s.fs
5865
}

internal/execute/tsc.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,10 @@ func executeCommandLineWorker(sys System, cb cbType, commandLine *tsoptions.Pars
114114
compilerOptionsFromCommandLine := commandLine.CompilerOptions()
115115

116116
if configFileName != "" {
117+
configStart := sys.Now()
117118
extendedConfigCache := map[tspath.Path]*tsoptions.ExtendedConfigCacheEntry{}
118119
configParseResult, errors := tsoptions.GetParsedCommandLineOfConfigFile(configFileName, compilerOptionsFromCommandLine, sys, extendedConfigCache)
120+
configTime := sys.Now().Sub(configStart)
119121
if len(errors) != 0 {
120122
// these are unrecoverable errors--exit to report them as diagnostics
121123
for _, e := range errors {
@@ -137,6 +139,7 @@ func executeCommandLineWorker(sys System, cb cbType, commandLine *tsoptions.Pars
137139
cb,
138140
configParseResult,
139141
reportDiagnostic,
142+
configTime,
140143
), nil
141144
} else {
142145
if compilerOptionsFromCommandLine.ShowConfig.IsTrue() {
@@ -155,6 +158,7 @@ func executeCommandLineWorker(sys System, cb cbType, commandLine *tsoptions.Pars
155158
cb,
156159
commandLine,
157160
reportDiagnostic,
161+
0, /*configTime*/
158162
), nil
159163
}
160164

@@ -172,24 +176,25 @@ func findConfigFile(searchPath string, fileExists func(string) bool, configName
172176
return result
173177
}
174178

175-
func performCompilation(sys System, cb cbType, config *tsoptions.ParsedCommandLine, reportDiagnostic diagnosticReporter) ExitStatus {
179+
func performCompilation(sys System, cb cbType, config *tsoptions.ParsedCommandLine, reportDiagnostic diagnosticReporter, configTime time.Duration) ExitStatus {
176180
host := compiler.NewCachedFSCompilerHost(config.CompilerOptions(), sys.GetCurrentDirectory(), sys.FS(), sys.DefaultLibraryPath())
177181
// todo: cache, statistics, tracing
178-
parseStart := time.Now()
182+
parseStart := sys.Now()
179183
program := compiler.NewProgram(compiler.ProgramOptions{
180184
Config: config,
181185
Host: host,
182186
})
183-
parseTime := time.Since(parseStart)
187+
parseTime := sys.Now().Sub(parseStart)
184188

185189
result := emitFilesAndReportErrors(sys, program, reportDiagnostic)
186190
if result.status != ExitStatusSuccess {
187191
// compile exited early
188192
return result.status
189193
}
190194

195+
result.configTime = configTime
191196
result.parseTime = parseTime
192-
result.totalTime = time.Since(parseStart)
197+
result.totalTime = sys.SinceStart()
193198

194199
if config.CompilerOptions().Diagnostics.IsTrue() || config.CompilerOptions().ExtendedDiagnostics.IsTrue() {
195200
var memStats runtime.MemStats
@@ -217,11 +222,12 @@ type compileAndEmitResult struct {
217222
diagnostics []*ast.Diagnostic
218223
emitResult *compiler.EmitResult
219224
status ExitStatus
225+
configTime time.Duration
220226
parseTime time.Duration
221227
bindTime time.Duration
222228
checkTime time.Duration
223-
emitTime time.Duration
224229
totalTime time.Duration
230+
emitTime time.Duration
225231
}
226232

227233
func emitFilesAndReportErrors(sys System, program *compiler.Program, reportDiagnostic diagnosticReporter) (result compileAndEmitResult) {
@@ -236,19 +242,19 @@ func emitFilesAndReportErrors(sys System, program *compiler.Program, reportDiagn
236242
// Options diagnostics include global diagnostics (even though we collect them separately),
237243
// and global diagnostics create checkers, which then bind all of the files. Do this binding
238244
// early so we can track the time.
239-
bindStart := time.Now()
245+
bindStart := sys.Now()
240246
_ = program.GetBindDiagnostics(ctx, nil)
241-
result.bindTime = time.Since(bindStart)
247+
result.bindTime = sys.Now().Sub(bindStart)
242248

243249
allDiagnostics = append(allDiagnostics, program.GetOptionsDiagnostics(ctx)...)
244250

245251
if options.ListFilesOnly.IsFalseOrUnknown() {
246252
allDiagnostics = append(allDiagnostics, program.GetGlobalDiagnostics(ctx)...)
247253

248254
if len(allDiagnostics) == configFileParsingDiagnosticsLength {
249-
checkStart := time.Now()
255+
checkStart := sys.Now()
250256
allDiagnostics = append(allDiagnostics, program.GetSemanticDiagnostics(ctx, nil)...)
251-
result.checkTime = time.Since(checkStart)
257+
result.checkTime = sys.Now().Sub(checkStart)
252258
}
253259

254260
if options.NoEmit.IsTrue() && options.GetEmitDeclarations() && len(allDiagnostics) == configFileParsingDiagnosticsLength {
@@ -259,9 +265,9 @@ func emitFilesAndReportErrors(sys System, program *compiler.Program, reportDiagn
259265

260266
emitResult := &compiler.EmitResult{EmitSkipped: true, Diagnostics: []*ast.Diagnostic{}}
261267
if !options.ListFilesOnly.IsTrue() {
262-
emitStart := time.Now()
268+
emitStart := sys.Now()
263269
emitResult = program.Emit(compiler.EmitOptions{})
264-
result.emitTime = time.Since(emitStart)
270+
result.emitTime = sys.Now().Sub(emitStart)
265271
}
266272
allDiagnostics = append(allDiagnostics, emitResult.Diagnostics...)
267273

0 commit comments

Comments
 (0)