diff --git a/cmd/markdown/main.go b/cmd/markdown/main.go index 6413090..22525ae 100644 --- a/cmd/markdown/main.go +++ b/cmd/markdown/main.go @@ -6,13 +6,9 @@ import ( "fmt" "github.com/knieriem/markdown" "io/ioutil" - "log" "os" - "runtime/pprof" ) -var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file") - func main() { var b []byte @@ -37,14 +33,8 @@ func main() { Dlists: *optDlists, } - if *cpuprofile != "" { - f, err := os.Create(*cpuprofile) - if err != nil { - log.Fatal(err) - } - pprof.StartCPUProfile(f) - defer pprof.StopCPUProfile() - } + startPProf() + defer stopPProf() doc := markdown.Parse(string(b), e) w := bufio.NewWriter(os.Stdout) diff --git a/cmd/markdown/pprof.go b/cmd/markdown/pprof.go new file mode 100644 index 0000000..be33388 --- /dev/null +++ b/cmd/markdown/pprof.go @@ -0,0 +1,35 @@ +package main + +import ( + "flag" + "log" + "os" + "runtime/pprof" +) + +var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file") +var memprofile = flag.String("memprofile", "", "write memory profile to file") + +func startPProf() { + if *cpuprofile != "" { + f, err := os.Create(*cpuprofile) + if err != nil { + log.Fatal(err) + } + pprof.StartCPUProfile(f) + } +} + +func stopPProf() { + if *cpuprofile != "" { + pprof.StopCPUProfile() + } + if *memprofile != "" { + f, err := os.Create(*memprofile) + if err != nil { + log.Fatal(err) + } + pprof.WriteHeapProfile(f) + f.Close() + } +} diff --git a/misc/devel.mk b/misc/devel.mk index 9264ed9..d86446a 100644 --- a/misc/devel.mk +++ b/misc/devel.mk @@ -27,8 +27,8 @@ benchmark: m ,,pmd ,,prevmd # pprof: cmd m $(MD) -cpuprofile /tmp/md.prof /tmp/,,md.out - @echo gopprof \'--nodefraction=0.1\' $(MD) /tmp/md.prof - @echo gopprof $(MD) /tmp/md.prof + @echo go tool pprof \'--nodefraction=0.1\' $(MD) /tmp/md.prof + @echo go tool pprof $(MD) /tmp/md.prof .PHONY:\ diff\