From 1e7a55c852eaf5eb4928ea38872d9a7f8bd39473 Mon Sep 17 00:00:00 2001 From: = Date: Tue, 19 Oct 2021 02:58:41 +0200 Subject: [PATCH] Add cpu profiling --- cmd/dogtamer.go | 19 +++++++++++++++++++ version.go | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/cmd/dogtamer.go b/cmd/dogtamer.go index d20ba3e..f6ae427 100644 --- a/cmd/dogtamer.go +++ b/cmd/dogtamer.go @@ -6,6 +6,7 @@ import ( "net/http" "os" "os/signal" + "runtime/pprof" "strings" "github.com/urfave/cli/v2" @@ -41,6 +42,10 @@ func main() { Usage: "Path to config-file", Aliases: []string{"c"}, }, + &cli.StringFlag{ + Name: "cpu-profile", + Usage: "Profile cpu usage and write to file", + }, }, Commands: []*cli.Command{ { @@ -69,11 +74,25 @@ func ActionServe(c *cli.Context) error { cfg.DebugLog(logger) ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Setup RTMP-server rtmpServer := server.NewRTMPServer(ctx, cfg.RTMPListenAddr) rtmpServer.Logger = logger rtmpServer.Hostname = cfg.Hostname + if c.IsSet("cpu-profile") { + filename := c.String("cpu-profile") + logger.Infow("CPU-profiling enabled.", "filename", filename) + f, err := os.Create(filename) + if err != nil { + return cli.Exit(fmt.Sprintf("Unable to create cpu-profile file '%s': %s", filename, err), 1) + } + defer f.Close() + + pprof.StartCPUProfile(f) + defer pprof.StopCPUProfile() + } + // Setup web-server webDone := make(chan struct{}) if cfg.HTTPServerEnable { diff --git a/version.go b/version.go index f29c063..492b9ff 100644 --- a/version.go +++ b/version.go @@ -2,7 +2,7 @@ package dogtamer import "fmt" -const Version string = "v0.1.7" +const Version string = "v0.1.8" var Build string