Performance improvements (avoid allocations for every packet)

This commit is contained in:
Jonas Köritz 2019-09-03 12:40:10 +02:00
parent 3006672e43
commit ce0a817cb9
3 changed files with 65 additions and 14 deletions

View file

@ -11,6 +11,8 @@ import (
"net/http"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"github.com/jonas-koeritz/actioncam/libipcamera"
"github.com/jonas-koeritz/actioncam/rtsp"
@ -30,6 +32,10 @@ func main() {
var password string
var port int16
var verbose bool
var cpuprofile string
var memoryprofile string
var cpuprofileFile *os.File
var camera *libipcamera.Camera
@ -46,18 +52,50 @@ func main() {
bufio.NewReader(os.Stdin).ReadBytes('\n')
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if cpuprofile != "" {
cpuprofileFile, err := os.Create(cpuprofile)
if err != nil {
log.Printf("Could not create CPU profiling file: %s\n", err)
return
}
err = pprof.StartCPUProfile(cpuprofileFile)
if err != nil {
log.Printf("Could not start CPU profiling: %s\n", err)
}
}
},
PreRun: func(cmd *cobra.Command, args []string) {
camera = connectAndLogin(net.ParseIP(args[0]), int(port), username, password, verbose)
},
PostRun: func(cmd *cobra.Command, args []string) {
camera.Disconnect()
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
pprof.StopCPUProfile()
cpuprofileFile.Close()
runtime.GC()
if memoryprofile != "" {
f, err := os.Create(memoryprofile)
if err != nil {
log.Printf("Could not create Memory profiling file: %s\n", err)
return
}
err = pprof.WriteHeapProfile(f)
if err != nil {
log.Printf("Could not start Memory profiling: %s\n", err)
}
}
},
}
rootCmd.PersistentFlags().Int16VarP(&port, "port", "P", 6666, "Specify an alternative camera port to connect to")
rootCmd.PersistentFlags().StringVarP(&username, "username", "u", "admin", "Specify the camera username")
rootCmd.PersistentFlags().StringVarP(&password, "password", "p", "12345", "Specify the camera password")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Print verbose output")
rootCmd.PersistentFlags().StringVarP(&cpuprofile, "cpuprofile", "c", "", "Profile CPU usage")
rootCmd.PersistentFlags().StringVarP(&memoryprofile, "memoryprofile", "m", "", "Profile memory usage")
var ls = &cobra.Command{
Use: "ls [Cameras IP Address]",