feat: add go profiler
parent
248f118186
commit
76665bed73
|
@ -2,6 +2,9 @@ package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"runtime/pprof"
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
|
|
||||||
|
@ -15,6 +18,7 @@ type action struct {
|
||||||
write bool
|
write bool
|
||||||
diff bool
|
diff bool
|
||||||
verbose bool
|
verbose bool
|
||||||
|
profiler bool
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,11 +27,12 @@ func (a *action) RunHelp(c *cli.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *action) getFlags(c *cli.Context) {
|
func (a *action) getFlags(c *cli.Context) {
|
||||||
|
a.flags.companyPrefix = c.String(flagCompanyPrefixName)
|
||||||
a.flags.list = c.Bool(flagListName)
|
a.flags.list = c.Bool(flagListName)
|
||||||
a.flags.write = c.Bool(flagWriteName)
|
a.flags.write = c.Bool(flagWriteName)
|
||||||
a.flags.diff = c.Bool(flagDiffName)
|
a.flags.diff = c.Bool(flagDiffName)
|
||||||
a.flags.verbose = c.Bool(flagVerboseName)
|
a.flags.verbose = c.Bool(flagVerboseName)
|
||||||
a.flags.companyPrefix = c.String(flagCompanyPrefixName)
|
a.flags.profiler = c.Bool(flagProfilerName)
|
||||||
|
|
||||||
if a.flags.verbose {
|
if a.flags.verbose {
|
||||||
fmt.Printf("flags: %+v\n", a.flags)
|
fmt.Printf("flags: %+v\n", a.flags)
|
||||||
|
@ -49,6 +54,19 @@ func (a *action) Run(c *cli.Context) error {
|
||||||
return a.RunHelp(c)
|
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(
|
ft, err := imports.NewFormmater(
|
||||||
imports.FormatterWithList(a.flags.list),
|
imports.FormatterWithList(a.flags.list),
|
||||||
imports.FormatterWithWrite(a.flags.write),
|
imports.FormatterWithWrite(a.flags.write),
|
||||||
|
@ -65,5 +83,18 @@ func (a *action) Run(c *cli.Context) error {
|
||||||
return fmt.Errorf("imports formatter: failed to format %v: %w", args, err)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,9 @@ const (
|
||||||
usage = "goimports with my opinionated preferences"
|
usage = "goimports with my opinionated preferences"
|
||||||
|
|
||||||
// Inspiration from gofmt flags
|
// Inspiration from gofmt flags
|
||||||
|
flagCompanyPrefixName = "company"
|
||||||
|
flagCompanyPrefixUsage = "company prefix, for example github.com/haunt98"
|
||||||
|
|
||||||
flagListName = "list"
|
flagListName = "list"
|
||||||
flagListUsage = "list files will be changed"
|
flagListUsage = "list files will be changed"
|
||||||
|
|
||||||
|
@ -25,8 +28,8 @@ const (
|
||||||
flagVerboseName = "verbose"
|
flagVerboseName = "verbose"
|
||||||
flagVerboseUsage = "show verbose output, for debug only"
|
flagVerboseUsage = "show verbose output, for debug only"
|
||||||
|
|
||||||
flagCompanyPrefixName = "company"
|
flagProfilerName = "profiler"
|
||||||
flagCompanyPrefixUsage = "company prefix, for example github.com/haunt98"
|
flagProfilerUsage = "go profiler, for debug only"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -47,6 +50,10 @@ func NewApp() *App {
|
||||||
Name: name,
|
Name: name,
|
||||||
Usage: usage,
|
Usage: usage,
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: flagCompanyPrefixName,
|
||||||
|
Usage: flagCompanyPrefixUsage,
|
||||||
|
},
|
||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
Name: flagListName,
|
Name: flagListName,
|
||||||
Usage: flagListUsage,
|
Usage: flagListUsage,
|
||||||
|
@ -66,9 +73,9 @@ func NewApp() *App {
|
||||||
Name: flagVerboseName,
|
Name: flagVerboseName,
|
||||||
Usage: flagVerboseUsage,
|
Usage: flagVerboseUsage,
|
||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.BoolFlag{
|
||||||
Name: flagCompanyPrefixName,
|
Name: flagProfilerName,
|
||||||
Usage: flagCompanyPrefixUsage,
|
Usage: flagProfilerUsage,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: a.Run,
|
Action: a.Run,
|
||||||
|
|
Loading…
Reference in New Issue