Skip to content

Commit

Permalink
feat: add cpu profile flag
Browse files Browse the repository at this point in the history
  • Loading branch information
brianmcgee committed May 6, 2024
1 parent ea6fe2d commit a79ae95
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 61 deletions.
2 changes: 2 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type Format struct {

Paths []string `name:"paths" arg:"" type:"path" optional:"" help:"Paths to format. Defaults to formatting the whole tree."`
Stdin bool `help:"Format the context passed in via stdin"`

CpuProfile string `optional:"" help:"file into which a cpu profile will be written"`
}

func ConfigureLogging() {
Expand Down
20 changes: 20 additions & 0 deletions cli/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os/signal"
"path/filepath"
"runtime"
"runtime/pprof"
"strings"
"syscall"

Expand Down Expand Up @@ -39,6 +40,25 @@ var (
)

func (f *Format) Run() (err error) {
// cpu profiling
if Cli.CpuProfile != "" {
// it is not recommended to go over 500 hz, but anything less wasn't producing meaningful samples
runtime.SetCPUProfileRate(500)

cpuProfile, err := os.Create(Cli.CpuProfile)
if err != nil {
return fmt.Errorf("failed to open file for writing cpu profile: %w", err)
} else if err = pprof.StartCPUProfile(cpuProfile); err != nil {
return fmt.Errorf("failed to start cpu profile: %w", err)
}
defer func() {
pprof.StopCPUProfile()
if err := cpuProfile.Close(); err != nil {
log.Errorf("failed to close cpu profile: %v", err)
}
}()
}

// create a prefixed logger
l := log.WithPrefix("format")

Expand Down
21 changes: 21 additions & 0 deletions cli/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ import (
"github.com/stretchr/testify/require"
)

func TestCpuProfile(t *testing.T) {
as := require.New(t)
tempDir := test.TempExamples(t)

// capture current cwd, so we can replace it after the test is finished
cwd, err := os.Getwd()
as.NoError(err)

t.Cleanup(func() {
// return to the previous working directory
as.NoError(os.Chdir(cwd))
})

_, err = cmd(t, "-C", tempDir, "--allow-missing-formatter", "--cpu-profile", "cpu.pprof")
as.NoError(err)
as.FileExists(filepath.Join(tempDir, "cpu.pprof"))
file, err := os.Stat(filepath.Join(tempDir, "cpu.pprof"))
as.NoError(err)
as.False(file.Size() == 0)
}

func TestAllowMissingFormatter(t *testing.T) {
as := require.New(t)

Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ require (
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/tools v0.20.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
61 changes: 8 additions & 53 deletions go.sum

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,6 @@ schema = 3
[mod."golang.org/x/exp"]
version = "v0.0.0-20240416160154-fe59bbe5cc7f"
hash = "sha256-168CD9hlLJaQ7stQk/ztlP3zgaWXUMbIHa38gAeRRs4="
[mod."golang.org/x/mod"]
version = "v0.17.0"
hash = "sha256-CLaPeF6uTFuRDv4oHwOQE6MCMvrzkUjWN3NuyywZjKU="
[mod."golang.org/x/net"]
version = "v0.24.0"
hash = "sha256-w1c21ljta5wNIyel9CSIn/crPzwOCRofNKhqmfs4aEQ="
Expand All @@ -130,9 +127,6 @@ schema = 3
[mod."golang.org/x/sys"]
version = "v0.19.0"
hash = "sha256-cmuL31TYLJmDm/fDnI2Sn0wB88cpdOHV1+urorsJWx4="
[mod."golang.org/x/tools"]
version = "v0.20.0"
hash = "sha256-g5T5FrNPO/cf2W1lc+/93FcFB3HftPjqI72FueD9Wt8="
[mod."gopkg.in/warnings.v0"]
version = "v0.1.2"
hash = "sha256-ATVL9yEmgYbkJ1DkltDGRn/auGAjqGOfjQyBYyUo8s8="
Expand Down
1 change: 1 addition & 0 deletions nix/devshell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# golang
go
delve
pprof
graphviz
]
++
Expand Down

0 comments on commit a79ae95

Please sign in to comment.