From 8616fe67dcf476d3d527f1e443a894d24733a788 Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Wed, 17 Aug 2022 23:38:41 +0200 Subject: [PATCH] show individual time --- compile.go | 12 ++++++++---- compile_report.go | 7 +++++-- main.go | 2 +- watch.go | 18 ++++++++++++++---- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/compile.go b/compile.go index bd07e96..d55fff8 100644 --- a/compile.go +++ b/compile.go @@ -5,9 +5,13 @@ import ( "path/filepath" "strings" "sync" + "time" ) func Compile(cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) { + start := time.Now() + + // calculate filepath b := new(strings.Builder) data := &OutputFileTmplData{ Name: ProjectName, @@ -20,7 +24,6 @@ func Compile(cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) { wg.Done() return } - filePath := filepath.Join(*OutputDir, b.String()) filePath, err := filepath.Abs(filePath) @@ -45,7 +48,8 @@ func Compile(cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) { wg.Done() return } - Compress(filePath, cfg, ch, wg) + + Compress(filePath, start, cfg, ch, wg) // uncomment for independent compile and compress tasks // (slightly slower in tests, most likely because of more context switches) @@ -55,7 +59,7 @@ func Compile(cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) { */ } -func Compress(filePath string, cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) { +func Compress(filePath string, start time.Time, cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) { defer wg.Done() ch <- &CompileReport{Config: cfg, State: StateCompressing} @@ -75,5 +79,5 @@ func Compress(filePath string, cfg *CompileConfig, ch chan<- *CompileReport, wg } } - ch <- &CompileReport{Config: cfg, State: StateDone} + ch <- &CompileReport{Config: cfg, State: StateDone, Duration: time.Since(start)} } diff --git a/compile_report.go b/compile_report.go index 2bc8289..fa291a0 100644 --- a/compile_report.go +++ b/compile_report.go @@ -1,12 +1,15 @@ package main import ( + "time" + "github.com/fatih/color" ) type CompileReport struct { - Config *CompileConfig - State CompileState + Config *CompileConfig + State CompileState + Duration time.Duration } type CompileState string diff --git a/main.go b/main.go index 0c0802b..899caa9 100644 --- a/main.go +++ b/main.go @@ -82,7 +82,7 @@ func main() { <-doneCh // wait for Watch routine if !*Silent { - fmt.Printf("compilation took %s (using %s threads)\n", end.Sub(start), GetThreadCountString()) + fmt.Printf("compilation took %s (using %s threads)\n", end.Sub(start).Truncate(time.Second/100), GetThreadCountString()) } } diff --git a/watch.go b/watch.go index 2ac89d4..764e57e 100644 --- a/watch.go +++ b/watch.go @@ -1,10 +1,15 @@ package main -import "fmt" +import ( + "fmt" + "time" +) func Watch(ch <-chan *CompileReport, doneCh chan<- struct{}) { defer close(doneCh) + times := map[*CompileConfig]time.Duration{} + states := map[*CompileConfig]CompileState{} for _, cfg := range CompileConfigs { states[cfg] = StateWaiting @@ -14,8 +19,9 @@ func Watch(ch <-chan *CompileReport, doneCh chan<- struct{}) { for report := range ch { states[report.Config] = report.State + times[report.Config] = report.Duration.Truncate(time.Second / 100) if !*Silent { - PrintStates(states, cleared) + PrintStates(states, times, cleared) } cleared = true } @@ -23,7 +29,7 @@ func Watch(ch <-chan *CompileReport, doneCh chan<- struct{}) { doneCh <- struct{}{} } -func PrintStates(states map[*CompileConfig]CompileState, clear bool) { +func PrintStates(states map[*CompileConfig]CompileState, times map[*CompileConfig]time.Duration, clear bool) { if clear { for i := 0; i < len(CompileConfigs); i++ { goUp() @@ -38,6 +44,10 @@ func PrintStates(states map[*CompileConfig]CompileState, clear bool) { continue } - fmt.Printf(fmt.Sprintf("%%-%ds %%s\n", MaxConfigStringLength), cfg.String(), state) + if state == StateDone { + fmt.Printf(fmt.Sprintf("%%-%ds %%s (%%s)\n", MaxConfigStringLength), cfg.String(), state, times[cfg]) + } else { + fmt.Printf(fmt.Sprintf("%%-%ds %%s\n", MaxConfigStringLength), cfg.String(), state) + } } }