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"
"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)}
}

View File

@ -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

View File

@ -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())
}
}

View File

@ -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)
}
}
}