show individual time

This commit is contained in:
Timon Ringwald 2022-08-17 23:38:41 +02:00
parent 2027f3412c
commit 8616fe67dc
4 changed files with 28 additions and 11 deletions

View File

@ -5,9 +5,13 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
"time"
) )
func Compile(cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) { func Compile(cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) {
start := time.Now()
// calculate filepath
b := new(strings.Builder) b := new(strings.Builder)
data := &OutputFileTmplData{ data := &OutputFileTmplData{
Name: ProjectName, Name: ProjectName,
@ -20,7 +24,6 @@ func Compile(cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) {
wg.Done() wg.Done()
return return
} }
filePath := filepath.Join(*OutputDir, b.String()) filePath := filepath.Join(*OutputDir, b.String())
filePath, err := filepath.Abs(filePath) filePath, err := filepath.Abs(filePath)
@ -45,7 +48,8 @@ func Compile(cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) {
wg.Done() wg.Done()
return return
} }
Compress(filePath, cfg, ch, wg)
Compress(filePath, start, cfg, ch, wg)
// uncomment for independent compile and compress tasks // uncomment for independent compile and compress tasks
// (slightly slower in tests, most likely because of more context switches) // (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() defer wg.Done()
ch <- &CompileReport{Config: cfg, State: StateCompressing} 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)}
} }

View File

@ -1,12 +1,15 @@
package main package main
import ( import (
"time"
"github.com/fatih/color" "github.com/fatih/color"
) )
type CompileReport struct { type CompileReport struct {
Config *CompileConfig Config *CompileConfig
State CompileState State CompileState
Duration time.Duration
} }
type CompileState string type CompileState string

View File

@ -82,7 +82,7 @@ func main() {
<-doneCh // wait for Watch routine <-doneCh // wait for Watch routine
if !*Silent { 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())
} }
} }

View File

@ -1,10 +1,15 @@
package main package main
import "fmt" import (
"fmt"
"time"
)
func Watch(ch <-chan *CompileReport, doneCh chan<- struct{}) { func Watch(ch <-chan *CompileReport, doneCh chan<- struct{}) {
defer close(doneCh) defer close(doneCh)
times := map[*CompileConfig]time.Duration{}
states := map[*CompileConfig]CompileState{} states := map[*CompileConfig]CompileState{}
for _, cfg := range CompileConfigs { for _, cfg := range CompileConfigs {
states[cfg] = StateWaiting states[cfg] = StateWaiting
@ -14,8 +19,9 @@ func Watch(ch <-chan *CompileReport, doneCh chan<- struct{}) {
for report := range ch { for report := range ch {
states[report.Config] = report.State states[report.Config] = report.State
times[report.Config] = report.Duration.Truncate(time.Second / 100)
if !*Silent { if !*Silent {
PrintStates(states, cleared) PrintStates(states, times, cleared)
} }
cleared = true cleared = true
} }
@ -23,7 +29,7 @@ func Watch(ch <-chan *CompileReport, doneCh chan<- struct{}) {
doneCh <- 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 { if clear {
for i := 0; i < len(CompileConfigs); i++ { for i := 0; i < len(CompileConfigs); i++ {
goUp() goUp()
@ -38,6 +44,10 @@ func PrintStates(states map[*CompileConfig]CompileState, clear bool) {
continue continue
} }
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) fmt.Printf(fmt.Sprintf("%%-%ds %%s\n", MaxConfigStringLength), cfg.String(), state)
} }
} }
}