-
Notifications
You must be signed in to change notification settings - Fork 385
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
- Loading branch information
1 parent
b625896
commit 437c191
Showing
6 changed files
with
120 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,49 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"runtime" | ||
"runtime/pprof" | ||
) | ||
|
||
type profiler struct { | ||
cpuProfile *os.File | ||
memProfile string | ||
} | ||
|
||
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 != "" { | ||
f, err := os.Create(p.memProfile) | ||
if err != nil { | ||
return err | ||
} | ||
runtime.GC() | ||
pprof.WriteHeapProfile(f) | ||
f.Close() | ||
} | ||
return nil | ||
} |
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,40 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
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