Skip to content

Commit f6dc114

Browse files
Merge pull request #37 from PatrickCronin/pcronin/mem-profile
Facilitate mem profiling in the feature branch
2 parents b0c7154 + 82874fd commit f6dc114

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

cmd/routesum/main.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import (
1515
)
1616

1717
func main() {
18-
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file")
18+
cpuProfile := flag.String("cpuprofile", "", "write cpu profile to file")
19+
memProfile := flag.String("memprofile", "", "write mem profile to file")
1920

2021
showMemStats := flag.Bool(
2122
"show-mem-stats",
@@ -30,15 +31,23 @@ func main() {
3031
}
3132

3233
var cpuProfOut io.Writer
33-
if *cpuprofile != "" {
34+
if *cpuProfile != "" {
3435
var err error
35-
if cpuProfOut, err = os.Create(*cpuprofile); err != nil {
36+
if cpuProfOut, err = os.Create(*cpuProfile); err != nil {
3637
fmt.Fprint(os.Stderr, errors.Wrap(err, "create cpu profile output file").Error())
3738
os.Exit(1)
3839
}
3940
}
4041

41-
if err := summarize(os.Stdin, os.Stdout, memStatsOut, cpuProfOut); err != nil {
42+
var memProfOut io.WriteCloser
43+
if *memProfile != "" {
44+
var err error
45+
if memProfOut, err = os.Create(*memProfile); err != nil {
46+
fmt.Fprint(os.Stderr, errors.Wrap(err, "create mem profile output file").Error())
47+
}
48+
}
49+
50+
if err := summarize(os.Stdin, os.Stdout, memStatsOut, cpuProfOut, memProfOut); err != nil {
4251
fmt.Fprintf(os.Stderr, "summarize: %s\n", err.Error())
4352
os.Exit(1)
4453
}
@@ -47,6 +56,7 @@ func main() {
4756
func summarize(
4857
in io.Reader,
4958
out, memStatsOut, cpuProfOut io.Writer,
59+
memProfOut io.WriteCloser,
5060
) error {
5161
if cpuProfOut != nil {
5262
if err := pprof.StartCPUProfile(cpuProfOut); err != nil {
@@ -68,6 +78,15 @@ func summarize(
6878
}
6979
}
7080

81+
if memProfOut != nil {
82+
if err := pprof.WriteHeapProfile(memProfOut); err != nil {
83+
return errors.Wrap(err, "write mem profile")
84+
}
85+
if err := memProfOut.Close(); err != nil {
86+
return errors.Wrap(err, "close mem profile")
87+
}
88+
}
89+
7190
if memStatsOut != nil {
7291
numInternalNodes, numLeafNodes, internalNodesTotalSize, leafNodesTotalSize := rs.MemUsage()
7392
fmt.Fprintf(memStatsOut,

cmd/routesum/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestSummarize(t *testing.T) {
4141
memStatsOut = &memStatsBuilder
4242
}
4343

44-
err := summarize(in, &out, memStatsOut, nil)
44+
err := summarize(in, &out, memStatsOut, nil, nil)
4545
require.NoError(t, err, "summarize does not throw an error")
4646

4747
assert.Equal(t, "192.0.2.0/31\n", out.String(), "read expected output")

0 commit comments

Comments
 (0)