-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fix-update): allow user to profile commands with pprof (#1685)
This adds two flags to the `fix` and the `update` command, `-cpuprofile` and `-memprofile`, which allow users to create output CPU and memory `pprof` files to better understand the performance of Gazelle. This is very helpful in debugging issues like #1688, which show up in certain repository setups. The implementation mostly followed the instructions on https://pkg.go.dev/runtime/pprof#hdr-Profiling_a_Go_program to set up profiling options for the command in the `cmd/gazelle`. Tests were added to ensure that the `newProfile` would return a `profile{}` that wouldn't panic, even if either provider is empty. Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
- Loading branch information
1 parent
073699e
commit 0737e71
Showing
6 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"runtime" | ||
"runtime/pprof" | ||
) | ||
|
||
type profiler struct { | ||
cpuProfile *os.File | ||
memProfile string | ||
} | ||
|
||
// newProfiler creates a profiler that writes to the given files. | ||
// it returns an empty profiler if both files are empty. | ||
// so that stop() will never fail. | ||
func newProfiler(cpuProfile, memProfile string) (profiler, error) { | ||
if cpuProfile == "" { | ||
return profiler{ | ||
memProfile: memProfile, | ||
}, nil | ||
} | ||
|
||
f, err := os.Create(cpuProfile) | ||
if err != nil { | ||
return profiler{}, err | ||
} | ||
pprof.StartCPUProfile(f) | ||
|
||
return profiler{ | ||
cpuProfile: f, | ||
memProfile: memProfile, | ||
}, nil | ||
} | ||
|
||
func (p *profiler) stop() error { | ||
if p.cpuProfile != nil { | ||
pprof.StopCPUProfile() | ||
p.cpuProfile.Close() | ||
} | ||
|
||
if p.memProfile == "" { | ||
return nil | ||
} | ||
|
||
f, err := os.Create(p.memProfile) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
runtime.GC() | ||
return pprof.WriteHeapProfile(f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestEmptyProfiler(t *testing.T) { | ||
dir := t.TempDir() | ||
tests := []struct { | ||
name string | ||
cpuProfile string | ||
memProfile string | ||
}{ | ||
{ | ||
name: "cpuProfile", | ||
cpuProfile: filepath.Join(dir, "cpu.prof"), | ||
}, | ||
{ | ||
name: "memProfile", | ||
memProfile: filepath.Join(dir, "mem.prof"), | ||
}, | ||
{ | ||
name: "empty", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run("", func(t *testing.T) { | ||
p, err := newProfiler(test.cpuProfile, test.memProfile) | ||
if err != nil { | ||
t.Fatalf("newProfiler failed: %v", err) | ||
} | ||
if err := p.stop(); err != nil { | ||
t.Fatalf("stop failed: %v", err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestProfiler(t *testing.T) { | ||
dir := t.TempDir() | ||
cpuProfileName := filepath.Join(dir, "cpu.prof") | ||
memProfileName := filepath.Join(dir, "mem.prof") | ||
t.Cleanup(func() { | ||
os.Remove(cpuProfileName) | ||
os.Remove(memProfileName) | ||
}) | ||
|
||
p, err := newProfiler(cpuProfileName, memProfileName) | ||
if err != nil { | ||
t.Fatalf("newProfiler failed: %v", err) | ||
} | ||
if p.cpuProfile == nil { | ||
t.Fatal("Expected cpuProfile to be non-nil") | ||
} | ||
if p.memProfile != memProfileName { | ||
t.Fatalf("Expected memProfile to be %s, got %s", memProfileName, p.memProfile) | ||
} | ||
|
||
if err := p.stop(); err != nil { | ||
t.Fatalf("stop failed: %v", err) | ||
} | ||
|
||
if _, err := os.Stat(cpuProfileName); os.IsNotExist(err) { | ||
t.Fatalf("CPU profile file %s was not created", cpuProfileName) | ||
} | ||
|
||
if _, err := os.Stat(memProfileName); os.IsNotExist(err) { | ||
t.Fatalf("Memory profile file %s was not created", memProfileName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters