Skip to content

Commit

Permalink
Simplify writing output to file
Browse files Browse the repository at this point in the history
  • Loading branch information
WillAbides committed May 15, 2023
1 parent d0d8e0a commit 760ce02
Showing 1 changed file with 31 additions and 42 deletions.
73 changes: 31 additions & 42 deletions cmd/benchdiff/internal/benchdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ stderr: %s`, cmd.String(), exitErr.ExitCode(), bufStderr.String())
return err
}

func (c *Benchdiff) runBenchmark(ref, filename, extraArgs string, pause time.Duration, force bool) (errOut error) {
cmd := exec.Command(c.BenchCmd, strings.Fields(c.BenchArgs+" "+extraArgs)...)
func (c *Benchdiff) runBenchmark(ref, filename, extraArgs string, pause time.Duration, force bool) error {
cmd := exec.Command(c.BenchCmd, strings.Fields(c.BenchArgs+" "+extraArgs)...) //nolint:gosec // this is fine

stdlib := false
if rootPath, err := runGitCmd(c.debug(), c.gitCmd(), c.Path, "rev-parse", "--show-toplevel"); err == nil {
Expand All @@ -114,6 +114,7 @@ func (c *Benchdiff) runBenchmark(ref, filename, extraArgs string, pause time.Dur
}
}

fileBuffer := &bytes.Buffer{}
if filename != "" {
c.debug().Printf("output file: %s", filename)
if ref != "" && !force {
Expand All @@ -122,53 +123,41 @@ func (c *Benchdiff) runBenchmark(ref, filename, extraArgs string, pause time.Dur
return nil
}
}
buf := &bytes.Buffer{}
defer func() {
if errOut != nil {
return
}
file, cErr := os.Create(filename)
if cErr != nil {
errOut = cErr
return
}
if _, wErr := file.Write(buf.Bytes()); wErr != nil {
errOut = wErr
return
}
if cErr := file.Close(); cErr != nil {
errOut = cErr
return
}
}()
cmd.Stdout = buf
cmd.Stdout = fileBuffer
}

var runErr error
if ref == "" {
return runCmd(cmd, c.debug())
}
err := runAtGitRef(c.debug(), c.gitCmd(), c.Path, c.BaseRef, func(workPath string) {
if pause > 0 {
time.Sleep(pause)
}
if stdlib {
makeCmd := exec.Command(filepath.Join(workPath, "src", "make.bash"))
makeCmd.Dir = filepath.Join(workPath, "src")
makeCmd.Env = append(os.Environ(), "GOOS=", "GOARCH=")
runErr = runCmd(makeCmd, c.debug())
if runErr != nil {
return
runErr = runCmd(cmd, c.debug())
} else {
err := runAtGitRef(c.debug(), c.gitCmd(), c.Path, c.BaseRef, func(workPath string) {
if pause > 0 {
time.Sleep(pause)
}
if stdlib {
makeCmd := exec.Command(filepath.Join(workPath, "src", "make.bash"))
makeCmd.Dir = filepath.Join(workPath, "src")
makeCmd.Env = append(os.Environ(), "GOOS=", "GOARCH=")
runErr = runCmd(makeCmd, c.debug())
if runErr != nil {
return
}
cmd.Path = filepath.Join(workPath, "bin", "go")
}
cmd.Path = filepath.Join(workPath, "bin", "go")
cmd.Dir = workPath // TODO: add relative path of working directory
runErr = runCmd(cmd, c.debug())
})
if err != nil {
return err
}
cmd.Dir = workPath // TODO: add relative path of working directory
runErr = runCmd(cmd, c.debug())
})
if err != nil {
return err
}
return runErr
if runErr != nil {
return runErr
}
if filename == "" {
return nil
}
return os.WriteFile(filename, fileBuffer.Bytes(), 0o666)
}

func (c *Benchdiff) runBenchmarks() (result *runBenchmarksResults, err error) {
Expand Down

0 comments on commit 760ce02

Please sign in to comment.