From 8b1a331a8a8b41898593701689c5034dd4070ad0 Mon Sep 17 00:00:00 2001 From: Jan Rada <31016933+ZelvaMan@users.noreply.github.com> Date: Mon, 20 May 2024 22:22:48 +0200 Subject: [PATCH] add better timing tracking to generate command --- cmd/generate.go | 11 +++++++++++ common/timer.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100755 common/timer.go diff --git a/cmd/generate.go b/cmd/generate.go index 8e8a694..c25dc1a 100755 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -49,6 +49,7 @@ func init() { } func doGenerate() error { + timer := common.NewTimer() log.Printf("Getting configurations...") config, err := dbGen.GetAndValidateConfig() @@ -56,6 +57,7 @@ func doGenerate() error { return fmt.Errorf("error getting config %s", err) } + timer.AddEntry("getting config") common.LogDebug("Debug logging is enabled") var routines []dbGen.DbRoutine @@ -67,12 +69,15 @@ func doGenerate() error { } log.Printf("Got %d routines", len(routines)) + timer.AddEntry("getting routines") if config.Debug { common.LogDebug("Saving to debug file...") err = common.SaveToTempFile(routines, "dbRoutines") if err != nil { return fmt.Errorf("error saving debug file: %s", err) } + timer.AddEntry("saving debug file") + } log.Printf("Preprocessing...") @@ -81,6 +86,7 @@ func doGenerate() error { return fmt.Errorf("error preprocessing: %s", err) } log.Printf("After preprocessing %d - %d = %d functions left", len(routines), len(routines)-len(processedFunctions), len(processedFunctions)) + timer.AddEntry("preprocessing") if config.Debug { common.LogDebug("Saving to debug file...") @@ -88,6 +94,8 @@ func doGenerate() error { if err != nil { return fmt.Errorf("error saving debug file: %s", err) } + timer.AddEntry("saving debug file") + } log.Printf("Generating...") @@ -96,5 +104,8 @@ func doGenerate() error { return fmt.Errorf("error generating: %s", err) } + timer.AddEntry("generating files") + timer.Finish() + log.Printf(timer.String()) return nil } diff --git a/common/timer.go b/common/timer.go new file mode 100755 index 0000000..8d91669 --- /dev/null +++ b/common/timer.go @@ -0,0 +1,50 @@ +package common + +import ( + "fmt" + "strings" + "time" +) + +type entry struct { + time time.Time + label string +} + +type Timer struct { + timers []entry +} + +func NewTimer() *Timer { + timer := &Timer{timers: []entry{}} + timer.AddEntry("start") + + return timer +} + +func (t *Timer) Finish() { + t.AddEntry("finish") +} + +func (t *Timer) String() string { + if len(t.timers) < 2 { + return "Timer not stopped" + } + var str strings.Builder + + startTime := t.timers[0].time + endTime := t.timers[len(t.timers)-1].time + totalExecTime := endTime.Sub(startTime) + str.WriteString(fmt.Sprintf("Total execution time %s \n", totalExecTime)) + + for i, entry := range t.timers[1 : len(t.timers)-1] { + sinceLast := entry.time.Sub(t.timers[i].time) + sinceStart := entry.time.Sub(startTime) + str.WriteString(fmt.Sprintf(" %s: %s (%s)\n", entry.label, sinceLast, sinceStart)) + } + return str.String() +} + +func (t *Timer) AddEntry(label string) { + t.timers = append(t.timers, entry{time: time.Now(), label: label}) +}