Add cpu profiling

This commit is contained in:
Torjus Håkestad 2021-10-19 02:58:41 +02:00
parent dabbe5c305
commit 1e7a55c852
2 changed files with 20 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"runtime/pprof"
"strings" "strings"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -41,6 +42,10 @@ func main() {
Usage: "Path to config-file", Usage: "Path to config-file",
Aliases: []string{"c"}, Aliases: []string{"c"},
}, },
&cli.StringFlag{
Name: "cpu-profile",
Usage: "Profile cpu usage and write to file",
},
}, },
Commands: []*cli.Command{ Commands: []*cli.Command{
{ {
@ -69,11 +74,25 @@ func ActionServe(c *cli.Context) error {
cfg.DebugLog(logger) cfg.DebugLog(logger)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Setup RTMP-server // Setup RTMP-server
rtmpServer := server.NewRTMPServer(ctx, cfg.RTMPListenAddr) rtmpServer := server.NewRTMPServer(ctx, cfg.RTMPListenAddr)
rtmpServer.Logger = logger rtmpServer.Logger = logger
rtmpServer.Hostname = cfg.Hostname 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 // Setup web-server
webDone := make(chan struct{}) webDone := make(chan struct{})
if cfg.HTTPServerEnable { if cfg.HTTPServerEnable {

View File

@ -2,7 +2,7 @@ package dogtamer
import "fmt" import "fmt"
const Version string = "v0.1.7" const Version string = "v0.1.8"
var Build string var Build string