Skip to content

Commit

Permalink
add cpu/mem profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
spezifisch committed Nov 5, 2023
1 parent 581b478 commit 1e49fdf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
racecond*
cpuprof*
memprof*
profile*pdf
test/
todo.txt
stmp.toml
Expand Down
3 changes: 1 addition & 2 deletions page_browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,8 @@ func (ui *Ui) createBrowserPage(indexes *[]subsonic.SubsonicIndex) *BrowserPage
browserPage.handleToggleEntityStar()
return nil
}
// only makes sense to add to a playlist if there are playlists
if event.Rune() == 'A' {
//////////////////////////////////////
// only makes sense to add to a playlist if there are playlists
if ui.playlistPage.GetCount() > 0 {
ui.pages.ShowPage(PageAddToPlaylist)
ui.app.SetFocus(ui.addToPlaylistList)
Expand Down
29 changes: 29 additions & 0 deletions stmps.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ package main
import (
"flag"
"fmt"
"log"
"os"
"runtime"
"runtime/pprof"

"github.com/spezifisch/stmps/logger"
"github.com/spezifisch/stmps/mpvplayer"
Expand Down Expand Up @@ -40,12 +42,27 @@ func readConfig() {
func main() {
help := flag.Bool("help", false, "Print usage")
enableMpris := flag.Bool("mpris", false, "Enable MPRIS2")
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to `file`")
memprofile := flag.String("memprofile", "", "write memory profile to `file`")

flag.Parse()
if *help {
fmt.Printf("USAGE: %s <args>\n", os.Args[0])
flag.Usage()
os.Exit(0)
}
// cpu/memprofile code straight from https://pkg.go.dev/runtime/pprof
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
defer f.Close() // error handling omitted for example
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}

readConfig()

Expand Down Expand Up @@ -108,4 +125,16 @@ func main() {
if err := ui.Run(); err != nil {
panic(err)
}

if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
defer f.Close() // error handling omitted for example
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
}
}

0 comments on commit 1e49fdf

Please sign in to comment.