From 633c353442ddf3467b6ee558697dfe31b5e00722 Mon Sep 17 00:00:00 2001 From: Qi Xiao Date: Thu, 17 Nov 2022 23:25:12 +0000 Subject: [PATCH] pkg/md/mdrun: Support -cpuprofile. --- pkg/md/mdrun/main.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/pkg/md/mdrun/main.go b/pkg/md/mdrun/main.go index 3a6c3431..e9991570 100644 --- a/pkg/md/mdrun/main.go +++ b/pkg/md/mdrun/main.go @@ -6,13 +6,15 @@ import ( "fmt" "io" "os" + "runtime/pprof" "src.elv.sh/pkg/md" ) var ( - codec = flag.String("codec", "html", "codec to use; one of html, trace, fmt, tty") - width = flag.Int("width", 0, "text width; relevant with fmt or tty") + cpuprofile = flag.String("cpuprofile", "", "name of file to store CPU profile in") + codec = flag.String("codec", "html", "codec to use; one of html, trace, fmt, tty") + width = flag.Int("width", 0, "text width; relevant with fmt or tty") ) func main() { @@ -24,6 +26,20 @@ func main() { fmt.Fprintln(os.Stderr, "read stdin:", err) os.Exit(2) } + if *cpuprofile != "" { + f, err := os.OpenFile(*cpuprofile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644) + if err != nil { + fmt.Printf("create cpu profile file %q: %v\n", *cpuprofile, err) + os.Exit(2) + } + defer f.Close() + err = pprof.StartCPUProfile(f) + if err != nil { + fmt.Println("start cpu profile:", err) + os.Exit(2) + } + defer pprof.StopCPUProfile() + } fmt.Print(md.RenderString(string(bs), c)) }