cmd: move profiling stuff into pprof.go

This commit is contained in:
Michael Teichgräber 2012-04-20 13:49:21 +02:00
parent 178d014fcf
commit ea9008f19c
3 changed files with 39 additions and 14 deletions

View File

@ -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)

35
cmd/markdown/pprof.go Normal file
View File

@ -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()
}
}

View File

@ -27,8 +27,8 @@ benchmark: m ,,pmd ,,prevmd
#
pprof: cmd m
$(MD) -cpuprofile /tmp/md.prof <m > /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\