Add printing of number of goroutines to assist with performance improvements

This commit is contained in:
Fred Boniface
2024-04-08 21:22:56 +01:00
parent 3e5ed2c10a
commit 7146d1a883
4 changed files with 32 additions and 7 deletions

View File

@@ -1,6 +1,8 @@
package background
import (
"fmt"
"runtime"
"time"
"git.fjla.uk/owlboard/timetable-mgr/cif"
@@ -9,11 +11,16 @@ import (
"git.fjla.uk/owlboard/timetable-mgr/log"
)
const frequency = 600 * time.Hour // Figure out a sensible frequency!
const frequency = 2 * time.Hour // Figure out a sensible frequency!
// Starts a background ticker to run background tasks. Uses the frequency configured in the background/ticker.go file
func InitTicker(cfg *helpers.Configuration, stop <-chan struct{}) {
go runTicker(cfg, stop)
// Run goroutine logging ticker if runtime set to debug
if helpers.Runtime == "debug" {
go goroutineTicker(stop)
}
}
// Runs the ticker and handles tick events
@@ -32,3 +39,18 @@ func runTicker(cfg *helpers.Configuration, stop <-chan struct{}) {
}
}
}
// Starts a ticker that logs how many goroutines are running every two seconds
func goroutineTicker(stop <-chan struct{}) {
log.Msg.Warn("Starting goroutine Tracker ticker - DEBUG USE ONLY")
ticker := time.NewTicker(1000 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-stop:
return
case <-ticker.C:
fmt.Printf("Number of goroutines running: %d\n", runtime.NumGoroutine())
}
}
}