From ea9008f19c6f1b21bcd6dcc787d9662aed805b98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Teichgr=C3=A4ber?= Date: Fri, 20 Apr 2012 13:49:21 +0200 Subject: [PATCH] cmd: move profiling stuff into pprof.go --- cmd/markdown/main.go | 14 ++------------ cmd/markdown/pprof.go | 35 +++++++++++++++++++++++++++++++++++ misc/devel.mk | 4 ++-- 3 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 cmd/markdown/pprof.go 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\