| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"crypto/sha256" |
| 6 |
"encoding/hex" |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"io/fs" |
| 11 |
"os" |
| 12 |
"os/exec" |
| 13 |
"path/filepath" |
| 14 |
"strings" |
| 15 |
) |
| 16 |
|
| 17 |
func goCommand(ctx context.Context, dir string, args ...string) (string, error) { |
| 18 |
cmd := exec.CommandContext(ctx, "go", args...) |
| 19 |
cmd.Dir = dir |
| 20 |
cmd.Stderr = os.Stderr |
| 21 |
out, err := cmd.Output() |
| 22 |
if err != nil { |
| 23 |
return "", fmt.Errorf("failed to run go %s: %w", strings.Join(args, " "), err) |
| 24 |
} |
| 25 |
return strings.TrimSpace(string(out)), nil |
| 26 |
} |
| 27 |
|
| 28 |
func copyFile(dst, src string) error { |
| 29 |
in, err := os.Open(src) |
| 30 |
if err != nil { |
| 31 |
return err |
| 32 |
} |
| 33 |
defer in.Close() |
| 34 |
out, err := os.Create(dst) |
| 35 |
if err != nil { |
| 36 |
return err |
| 37 |
} |
| 38 |
if _, err := io.Copy(out, in); err != nil { |
| 39 |
out.Close() |
| 40 |
return err |
| 41 |
} |
| 42 |
return out.Close() |
| 43 |
} |
| 44 |
|
| 45 |
// createModFile creates, in tmpDir, a copy of the go.mod of the benchmark module in benchDir |
| 46 |
// whose replace directive for the library points to libDir. |
| 47 |
// It makes it possible to run the benchmarks of the working tree against the library of another commit. |
| 48 |
func createModFile(ctx context.Context, tmpDir, benchDir, libDir string) (string, error) { |
| 49 |
libPath, err := goCommand(ctx, libDir, "list", "-m") |
| 50 |
if err != nil { |
| 51 |
return "", err |
| 52 |
} |
| 53 |
goMod, err := goCommand(ctx, benchDir, "list", "-m", "-f", "{{.GoMod}}") |
| 54 |
if err != nil { |
| 55 |
return "", err |
| 56 |
} |
| 57 |
modFile := filepath.Join(tmpDir, "go.mod") |
| 58 |
if err := copyFile(modFile, goMod); err != nil { |
| 59 |
return "", err |
| 60 |
} |
| 61 |
// the go command looks for go.sum next to the file specified by -modfile. |
| 62 |
// go.sum doesn't exist if the benchmark module has no external dependency. |
| 63 |
goSum := filepath.Join(filepath.Dir(goMod), "go.sum") |
| 64 |
if err := copyFile(filepath.Join(tmpDir, "go.sum"), goSum); err != nil && !errors.Is(err, fs.ErrNotExist) { |
| 65 |
return "", err |
| 66 |
} |
| 67 |
if _, err := goCommand(ctx, benchDir, "mod", "edit", "-replace", libPath+"="+libDir, modFile); err != nil { |
| 68 |
return "", err |
| 69 |
} |
| 70 |
return modFile, nil |
| 71 |
} |
| 72 |
|
| 73 |
// hashDir returns the hash of the names and the contents of all files under dir. |
| 74 |
func hashDir(dir string) (string, error) { |
| 75 |
h := sha256.New() |
| 76 |
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { |
| 77 |
if err != nil { |
| 78 |
return err |
| 79 |
} |
| 80 |
if !d.Type().IsRegular() { |
| 81 |
return nil |
| 82 |
} |
| 83 |
rel, err := filepath.Rel(dir, path) |
| 84 |
if err != nil { |
| 85 |
return err |
| 86 |
} |
| 87 |
content, err := os.ReadFile(path) |
| 88 |
if err != nil { |
| 89 |
return err |
| 90 |
} |
| 91 |
fmt.Fprintf(h, "%s\x00%d\x00", filepath.ToSlash(rel), len(content)) |
| 92 |
h.Write(content) |
| 93 |
return nil |
| 94 |
}) |
| 95 |
if err != nil { |
| 96 |
return "", err |
| 97 |
} |
| 98 |
return hex.EncodeToString(h.Sum(nil)), nil |
| 99 |
} |