Skip to content

Commit

Permalink
sweet: add helper to write pprof files
Browse files Browse the repository at this point in the history
Change-Id: I047004d6f79048b1c6ef244e4f273e6ca0b25f56
Reviewed-on: https://go-review.googlesource.com/c/benchmarks/+/600056
Auto-Submit: Austin Clements <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Michael Knyszek <[email protected]>
  • Loading branch information
aclements authored and gopherbot committed Jul 22, 2024
1 parent 1b8120e commit bff3eae
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
10 changes: 2 additions & 8 deletions sweet/cmd/sweet/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,14 +466,8 @@ func mergeCPUProfiles(dir string) (string, error) {
}

out := filepath.Join(dir, "merged.cpu")
f, err := os.Create(out)
if err != nil {
return "", fmt.Errorf("error creating file: %w", err)
}
defer f.Close()

if err := p.Write(f); err != nil {
return "", fmt.Errorf("error writing merged profile: %w", err)
if err := sprofile.WritePprof(out, p); err != nil {
return "", err
}

return out, nil
Expand Down
19 changes: 19 additions & 0 deletions sweet/common/profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package profile

import (
"fmt"
"os"
"path/filepath"

Expand All @@ -21,6 +22,24 @@ func ReadPprof(filename string) (*profile.Profile, error) {
return profile.Parse(f)
}

func WritePprof(filename string, p *profile.Profile) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()

err = p.Write(f)
if err == nil {
err = f.Close()
}
if err != nil {
return fmt.Errorf("error writing profile %s: %s", filename, err)
}

return nil
}

// ReadDir reads all pprof profiles in dir whose name matches match(name).
func ReadDirPprof(dir string, match func(string) bool) ([]*profile.Profile, error) {
entries, err := os.ReadDir(dir)
Expand Down

0 comments on commit bff3eae

Please sign in to comment.