Skip to content

Commit

Permalink
feat: add go profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
haunt98 committed Jan 17, 2023
1 parent 248f118 commit 76665be
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
33 changes: 32 additions & 1 deletion internal/cli/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package cli

import (
"fmt"
"os"
"runtime"
"runtime/pprof"

"github.com/urfave/cli/v2"

Expand All @@ -15,6 +18,7 @@ type action struct {
write bool
diff bool
verbose bool
profiler bool
}
}

Expand All @@ -23,11 +27,12 @@ func (a *action) RunHelp(c *cli.Context) error {
}

func (a *action) getFlags(c *cli.Context) {
a.flags.companyPrefix = c.String(flagCompanyPrefixName)
a.flags.list = c.Bool(flagListName)
a.flags.write = c.Bool(flagWriteName)
a.flags.diff = c.Bool(flagDiffName)
a.flags.verbose = c.Bool(flagVerboseName)
a.flags.companyPrefix = c.String(flagCompanyPrefixName)
a.flags.profiler = c.Bool(flagProfilerName)

if a.flags.verbose {
fmt.Printf("flags: %+v\n", a.flags)
Expand All @@ -49,6 +54,19 @@ func (a *action) Run(c *cli.Context) error {
return a.RunHelp(c)
}

if a.flags.profiler {
f, err := os.Create("cpuprofile")
if err != nil {
return fmt.Errorf("os: failed to create: %w", err)
}
defer f.Close()

if err := pprof.StartCPUProfile(f); err != nil {
return fmt.Errorf("pprof: failed to start cpu profile: %w", err)
}
defer pprof.StopCPUProfile()
}

ft, err := imports.NewFormmater(
imports.FormatterWithList(a.flags.list),
imports.FormatterWithWrite(a.flags.write),
Expand All @@ -65,5 +83,18 @@ func (a *action) Run(c *cli.Context) error {
return fmt.Errorf("imports formatter: failed to format %v: %w", args, err)
}

if a.flags.profiler {
f, err := os.Create("memprofile")
if err != nil {
return fmt.Errorf("os: failed to create: %w", err)
}
defer f.Close()

runtime.GC()
if err := pprof.WriteHeapProfile(f); err != nil {
return fmt.Errorf("pprof: failed to write heap profile: %w", err)
}
}

return nil
}
17 changes: 12 additions & 5 deletions internal/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const (
usage = "goimports with my opinionated preferences"

// Inspiration from gofmt flags
flagCompanyPrefixName = "company"
flagCompanyPrefixUsage = "company prefix, for example github.com/haunt98"

flagListName = "list"
flagListUsage = "list files will be changed"

Expand All @@ -25,8 +28,8 @@ const (
flagVerboseName = "verbose"
flagVerboseUsage = "show verbose output, for debug only"

flagCompanyPrefixName = "company"
flagCompanyPrefixUsage = "company prefix, for example github.com/haunt98"
flagProfilerName = "profiler"
flagProfilerUsage = "go profiler, for debug only"
)

var (
Expand All @@ -47,6 +50,10 @@ func NewApp() *App {
Name: name,
Usage: usage,
Flags: []cli.Flag{
&cli.StringFlag{
Name: flagCompanyPrefixName,
Usage: flagCompanyPrefixUsage,
},
&cli.BoolFlag{
Name: flagListName,
Usage: flagListUsage,
Expand All @@ -66,9 +73,9 @@ func NewApp() *App {
Name: flagVerboseName,
Usage: flagVerboseUsage,
},
&cli.StringFlag{
Name: flagCompanyPrefixName,
Usage: flagCompanyPrefixUsage,
&cli.BoolFlag{
Name: flagProfilerName,
Usage: flagProfilerUsage,
},
},
Action: a.Run,
Expand Down

0 comments on commit 76665be

Please sign in to comment.