Skip to content

Commit

Permalink
Add flags for profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
aburdulescu committed Mar 3, 2024
1 parent a5a4dab commit 3dd1d50
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -2,3 +2,5 @@
/*.txt
/*.html
/dist/
/profile.cpu
/profile.mem
22 changes: 22 additions & 0 deletions main.go
Expand Up @@ -8,6 +8,7 @@ import (
"html"
"os"
"runtime/debug"
"runtime/pprof"
"strings"
"text/tabwriter"
)
Expand Down Expand Up @@ -51,6 +52,8 @@ func mainErr(args []string) error {
ignoreFile := fset.String("i", "", "`File` with keywords to ignored, one per line")
outputHtml := fset.Bool("html", false, "Generate HTML output")
printVersion := fset.Bool("version", false, "Print version")
cpuProfile := fset.Bool("profile-cpu", false, "Write CPU profile")
memProfile := fset.Bool("profile-mem", false, "Write memory profile")

if err := fset.Parse(args); err != nil {
if errors.Is(err, flag.ErrHelp) {
Expand All @@ -69,6 +72,25 @@ func mainErr(args []string) error {
return fmt.Errorf("need DHAT file")
}

if *cpuProfile {
f, err := os.Create("profile.cpu")
if err != nil {
return err
}
_ = pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
defer func() {
if *memProfile {
f, err := os.Create("profile.mem")
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
}
_ = pprof.WriteHeapProfile(f)
f.Close()
}
}()

ignoreList, err := parseIgnoreFile(*ignoreFile)
if err != nil {
return err
Expand Down

0 comments on commit 3dd1d50

Please sign in to comment.