build(ci): implement PGO (#1812)

* build(ci): implement pgo

Implement PGO (performance guided optimizations) for Go builds.
This commit is contained in:
Kyle Sanderson 2024-11-16 14:57:41 -08:00 committed by GitHub
parent fc137f2077
commit 50f1e4e7d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 199 additions and 14 deletions

View file

@ -4,9 +4,12 @@
package main
import (
"log"
"os"
"os/signal"
"runtime/pprof"
"syscall"
"time"
_ "time/tzdata"
"github.com/autobrr/autobrr/internal/action"
@ -47,10 +50,13 @@ var (
)
func main() {
var configPath string
var configPath, profilePath string
pflag.StringVar(&configPath, "config", "", "path to configuration file")
pflag.StringVar(&profilePath, "pgo", "", "internal build flag")
pflag.Parse()
shutdownFunc := pgoRun(profilePath)
// read config
cfg := config.New(configPath, version)
@ -167,6 +173,11 @@ func main() {
return
}
if shutdownFunc != nil {
time.Sleep(5 * time.Second)
sigCh <- syscall.SIGQUIT
}
for sig := range sigCh {
log.Info().Msgf("received signal: %v, shutting down server.", sig)
@ -174,8 +185,30 @@ func main() {
if err := db.Close(); err != nil {
log.Error().Err(err).Msg("failed to close the database connection properly")
shutdownFunc()
os.Exit(1)
}
shutdownFunc()
os.Exit(0)
}
}
func pgoRun(file string) func() {
if len(file) == 0 {
return nil
}
f, err := os.Create(file)
if err != nil {
log.Fatalf("could not create CPU profile: %v", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatalf("could not create CPU profile: %v", err)
}
return func() {
defer f.Close()
defer pprof.StopCPUProfile()
}
}