2020-01-28 22:41:27 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2020-02-21 22:23:20 +00:00
|
|
|
"bytes"
|
2020-02-10 13:41:58 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2020-02-12 22:54:25 +00:00
|
|
|
"fmt"
|
2020-01-28 22:41:27 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2020-02-10 13:41:58 +00:00
|
|
|
"strings"
|
2020-01-28 22:41:27 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2020-02-21 22:23:20 +00:00
|
|
|
"golang.org/x/perf/benchstat"
|
2020-02-12 22:25:12 +00:00
|
|
|
|
|
|
|
"gitlab.com/slon/shad-go/tools/testtool"
|
2020-01-28 22:41:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
problemFlag = "problem"
|
|
|
|
studentRepoFlag = "student-repo"
|
|
|
|
privateRepoFlag = "private-repo"
|
|
|
|
|
2020-02-10 13:41:58 +00:00
|
|
|
testdataDir = "testdata"
|
|
|
|
moduleImportPath = "gitlab.com/slon/shad-go"
|
2020-01-28 22:41:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var testSubmissionCmd = &cobra.Command{
|
2020-02-12 22:54:25 +00:00
|
|
|
Use: "check-task",
|
|
|
|
Short: "test single task",
|
2020-01-28 22:41:27 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
problem, err := cmd.Flags().GetString(problemFlag)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
studentRepo := mustParseDirFlag(studentRepoFlag, cmd)
|
|
|
|
if !problemDirExists(studentRepo, problem) {
|
|
|
|
log.Fatalf("%s does not have %s directory", studentRepo, problem)
|
|
|
|
}
|
|
|
|
|
|
|
|
privateRepo := mustParseDirFlag(privateRepoFlag, cmd)
|
|
|
|
if !problemDirExists(privateRepo, problem) {
|
|
|
|
log.Fatalf("%s does not have %s directory", privateRepo, problem)
|
|
|
|
}
|
|
|
|
|
2020-02-12 22:54:25 +00:00
|
|
|
if err := testSubmission(studentRepo, privateRepo, problem); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-01-28 22:41:27 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(testSubmissionCmd)
|
|
|
|
|
|
|
|
testSubmissionCmd.Flags().String(problemFlag, "", "problem directory name (required)")
|
|
|
|
_ = testSubmissionCmd.MarkFlagRequired(problemFlag)
|
|
|
|
|
|
|
|
testSubmissionCmd.Flags().String(studentRepoFlag, ".", "path to student repo root")
|
|
|
|
testSubmissionCmd.Flags().String(privateRepoFlag, ".", "path to shad-go-private repo root")
|
|
|
|
}
|
|
|
|
|
|
|
|
// mustParseDirFlag parses string directory flag with given name.
|
|
|
|
//
|
|
|
|
// Exits on any error.
|
|
|
|
func mustParseDirFlag(name string, cmd *cobra.Command) string {
|
|
|
|
dir, err := cmd.Flags().GetString(name)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
dir, err = filepath.Abs(dir)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that repo dir contains problem subdir.
|
|
|
|
func problemDirExists(repo, problem string) bool {
|
|
|
|
info, err := os.Stat(path.Join(repo, problem))
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return info.IsDir()
|
|
|
|
}
|
|
|
|
|
2020-02-12 22:54:25 +00:00
|
|
|
func testSubmission(studentRepo, privateRepo, problem string) error {
|
2020-01-28 22:41:27 +00:00
|
|
|
// Create temp directory to store all files required to test the solution.
|
2020-02-10 13:41:58 +00:00
|
|
|
tmpRepo, err := ioutil.TempDir("/tmp", problem+"-")
|
2020-01-28 22:41:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-02-10 13:41:58 +00:00
|
|
|
if err := os.Chmod(tmpRepo, 0755); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() { _ = os.RemoveAll(tmpRepo) }()
|
|
|
|
log.Printf("testing submission in %s", tmpRepo)
|
2020-01-28 22:41:27 +00:00
|
|
|
|
|
|
|
// Path to private problem folder.
|
|
|
|
privateProblem := path.Join(privateRepo, problem)
|
|
|
|
|
2020-02-10 13:41:58 +00:00
|
|
|
// Copy student repo files to temp dir.
|
|
|
|
log.Printf("copying student repo")
|
|
|
|
copyContents(studentRepo, ".", tmpRepo)
|
2020-01-28 22:41:27 +00:00
|
|
|
|
|
|
|
// Copy tests from private repo to temp dir.
|
|
|
|
log.Printf("copying tests")
|
|
|
|
tests := listTestFiles(privateProblem)
|
2020-02-10 13:41:58 +00:00
|
|
|
copyFiles(privateRepo, relPaths(privateRepo, tests), tmpRepo)
|
2020-01-28 22:41:27 +00:00
|
|
|
|
|
|
|
// Copy !change files from private repo to temp dir.
|
|
|
|
log.Printf("copying !change files")
|
|
|
|
protected := listProtectedFiles(privateProblem)
|
2020-02-10 13:41:58 +00:00
|
|
|
copyFiles(privateRepo, relPaths(privateRepo, protected), tmpRepo)
|
2020-01-28 22:41:27 +00:00
|
|
|
|
|
|
|
// Copy testdata directory from private repo to temp dir.
|
|
|
|
log.Printf("copying testdata directory")
|
2020-02-10 13:41:58 +00:00
|
|
|
copyDir(privateRepo, path.Join(problem, testdataDir), tmpRepo)
|
2020-01-28 22:41:27 +00:00
|
|
|
|
|
|
|
// Copy go.mod and go.sum from private repo to temp dir.
|
2020-02-14 12:55:10 +00:00
|
|
|
log.Printf("copying go.mod, go.sum and .golangci.yml")
|
|
|
|
copyFiles(privateRepo, []string{"go.mod", "go.sum", ".golangci.yml"}, tmpRepo)
|
|
|
|
|
2020-02-21 22:23:20 +00:00
|
|
|
log.Printf("running tests")
|
|
|
|
if err := runTests(tmpRepo, privateRepo, problem); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-14 12:55:10 +00:00
|
|
|
log.Printf("running linter")
|
|
|
|
if err := runLinter(tmpRepo, problem); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-28 22:41:27 +00:00
|
|
|
|
2020-02-21 22:23:20 +00:00
|
|
|
return nil
|
2020-01-28 22:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// copyDir recursively copies src directory to dst.
|
2020-02-10 13:41:58 +00:00
|
|
|
func copyDir(baseDir, src, dst string) {
|
2020-01-28 22:41:27 +00:00
|
|
|
_, err := os.Stat(src)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-10 13:41:58 +00:00
|
|
|
cmd := exec.Command("rsync", "-prR", src, dst)
|
2020-01-28 22:41:27 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
2020-02-10 13:41:58 +00:00
|
|
|
cmd.Dir = baseDir
|
2020-01-28 22:41:27 +00:00
|
|
|
|
2020-02-01 22:21:12 +00:00
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
log.Fatalf("directory copying failed: %s", err)
|
2020-01-28 22:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 22:14:20 +00:00
|
|
|
// copyContents recursively copies src contents to dst.
|
2020-02-10 13:41:58 +00:00
|
|
|
func copyContents(baseDir, src, dst string) {
|
|
|
|
copyDir(baseDir, src+"/", dst)
|
2020-01-28 22:41:27 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 22:14:20 +00:00
|
|
|
// copyFiles copies files preserving directory structure relative to baseDir.
|
2020-01-28 22:41:27 +00:00
|
|
|
//
|
|
|
|
// Existing files get replaced.
|
|
|
|
func copyFiles(baseDir string, relPaths []string, dst string) {
|
|
|
|
for _, p := range relPaths {
|
2020-02-10 13:41:58 +00:00
|
|
|
cmd := exec.Command("rsync", "-prR", p, dst)
|
2020-01-28 22:41:27 +00:00
|
|
|
cmd.Dir = baseDir
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
2020-02-01 22:21:12 +00:00
|
|
|
if err := cmd.Run(); err != nil {
|
2020-01-28 22:41:27 +00:00
|
|
|
log.Fatalf("file copying failed: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 13:41:58 +00:00
|
|
|
func randomName() string {
|
|
|
|
var raw [8]byte
|
|
|
|
_, _ = rand.Read(raw[:])
|
|
|
|
return hex.EncodeToString(raw[:])
|
|
|
|
}
|
2020-01-28 22:41:27 +00:00
|
|
|
|
2020-02-12 22:54:25 +00:00
|
|
|
type TestFailedError struct {
|
|
|
|
E error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *TestFailedError) Error() string {
|
2020-02-12 23:31:40 +00:00
|
|
|
return fmt.Sprintf("test failed: %v", e.E)
|
2020-02-12 22:54:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *TestFailedError) Unwrap() error {
|
|
|
|
return e.E
|
|
|
|
}
|
|
|
|
|
2020-02-14 12:55:10 +00:00
|
|
|
func runLinter(testDir, problem string) error {
|
|
|
|
cmd := exec.Command("golangci-lint", "run", "--modules-download-mode", "readonly", "--build-tags", "private", fmt.Sprintf("./%s/...", problem))
|
|
|
|
cmd.Dir = testDir
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return fmt.Errorf("linter failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-10 13:41:58 +00:00
|
|
|
// runTests runs all tests in directory with race detector.
|
2020-02-21 22:23:20 +00:00
|
|
|
func runTests(testDir, privateRepo, problem string) error {
|
2020-02-10 13:41:58 +00:00
|
|
|
binCache, err := ioutil.TempDir("/tmp", "bincache")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-04-30 10:33:35 +00:00
|
|
|
if err = os.Chmod(binCache, 0755); err != nil {
|
2020-02-10 13:41:58 +00:00
|
|
|
log.Fatal(err)
|
2020-01-28 22:41:27 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 10:10:10 +00:00
|
|
|
var goCache string
|
|
|
|
goCache, err = ioutil.TempDir("/tmp", "gocache")
|
2021-04-30 10:00:54 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-04-30 10:33:35 +00:00
|
|
|
if err = os.Chmod(goCache, 0777); err != nil {
|
2021-04-30 10:00:54 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-02-12 22:54:25 +00:00
|
|
|
runGo := func(arg ...string) error {
|
2020-02-10 13:41:58 +00:00
|
|
|
log.Printf("> go %s", strings.Join(arg, " "))
|
|
|
|
|
|
|
|
cmd := exec.Command("go", arg...)
|
|
|
|
cmd.Env = append(os.Environ(), "GOFLAGS=")
|
|
|
|
cmd.Dir = testDir
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
2020-02-12 22:54:25 +00:00
|
|
|
return cmd.Run()
|
2020-01-28 22:41:27 +00:00
|
|
|
}
|
2020-02-10 13:41:58 +00:00
|
|
|
|
2022-03-12 13:25:16 +00:00
|
|
|
var (
|
|
|
|
binaries = make(map[string]string)
|
|
|
|
testBinaries = make(map[string]string)
|
|
|
|
raceBinaries = make(map[string]string)
|
|
|
|
)
|
2020-02-10 13:41:58 +00:00
|
|
|
|
2023-03-12 13:25:51 +00:00
|
|
|
coverageReq := getCoverageRequirements(path.Join(privateRepo, problem))
|
|
|
|
if coverageReq.Enabled {
|
|
|
|
log.Printf("required coverage: %.2f%%", coverageReq.Percent)
|
|
|
|
}
|
|
|
|
|
|
|
|
testListDir := testDir
|
|
|
|
if !coverageReq.Enabled {
|
|
|
|
testListDir = privateRepo
|
|
|
|
}
|
|
|
|
|
2022-02-10 22:06:57 +00:00
|
|
|
//binPkgs, testPkgs := listTestsAndBinaries(filepath.Join(testDir, problem), []string{"-tags", "private", "-mod", "readonly"}) // todo return readonly
|
2023-03-12 13:25:51 +00:00
|
|
|
binPkgs, testPkgs := listTestsAndBinaries(filepath.Join(testListDir, problem), []string{"-tags", "private"})
|
2020-02-10 13:41:58 +00:00
|
|
|
for binaryPkg := range binPkgs {
|
|
|
|
binPath := filepath.Join(binCache, randomName())
|
|
|
|
binaries[binaryPkg] = binPath
|
2020-02-12 22:54:25 +00:00
|
|
|
|
|
|
|
if err := runGo("build", "-mod", "readonly", "-tags", "private", "-o", binPath, binaryPkg); err != nil {
|
|
|
|
return fmt.Errorf("error building binary in %s: %w", binaryPkg, err)
|
|
|
|
}
|
2020-01-28 22:41:27 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 22:25:12 +00:00
|
|
|
binariesJSON, _ := json.Marshal(binaries)
|
2020-02-10 13:41:58 +00:00
|
|
|
|
|
|
|
for testPkg := range testPkgs {
|
2022-03-12 13:25:16 +00:00
|
|
|
testPath := filepath.Join(binCache, randomName())
|
|
|
|
testBinaries[testPkg] = testPath
|
|
|
|
|
|
|
|
cmd := []string{"test", "-mod", "readonly", "-tags", "private", "-c", "-o", testPath, testPkg}
|
2020-03-18 22:19:44 +00:00
|
|
|
if coverageReq.Enabled {
|
2023-02-22 13:02:44 +00:00
|
|
|
pkgs := make([]string, len(coverageReq.Packages))
|
|
|
|
for i, pkg := range coverageReq.Packages {
|
|
|
|
pkgs[i] = path.Join(moduleImportPath, problem, pkg)
|
|
|
|
}
|
|
|
|
cmd = append(cmd, "-cover", "-coverpkg", strings.Join(pkgs, ","))
|
2020-03-18 22:19:44 +00:00
|
|
|
}
|
|
|
|
if err := runGo(cmd...); err != nil {
|
2020-02-12 22:54:25 +00:00
|
|
|
return fmt.Errorf("error building test in %s: %w", testPkg, err)
|
|
|
|
}
|
2022-03-12 13:25:16 +00:00
|
|
|
|
|
|
|
racePath := filepath.Join(binCache, randomName())
|
|
|
|
raceBinaries[testPkg] = racePath
|
|
|
|
|
|
|
|
cmd = []string{"test", "-mod", "readonly", "-race", "-tags", "private", "-c", "-o", racePath, testPkg}
|
|
|
|
if err := runGo(cmd...); err != nil {
|
|
|
|
return fmt.Errorf("error building test in %s: %w", testPkg, err)
|
|
|
|
}
|
2020-01-28 22:41:27 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 23:51:13 +00:00
|
|
|
coverProfiles := []string{}
|
2020-02-10 13:41:58 +00:00
|
|
|
for testPkg, testBinary := range testBinaries {
|
|
|
|
relPath := strings.TrimPrefix(testPkg, moduleImportPath)
|
2020-03-20 10:00:17 +00:00
|
|
|
coverProfile := path.Join(os.TempDir(), randomName())
|
2020-02-10 13:41:58 +00:00
|
|
|
|
2020-02-21 22:23:20 +00:00
|
|
|
{
|
|
|
|
cmd := exec.Command(testBinary)
|
2020-03-18 22:19:44 +00:00
|
|
|
if coverageReq.Enabled {
|
2020-03-20 10:00:17 +00:00
|
|
|
cmd = exec.Command(testBinary, "-test.coverprofile", coverProfile)
|
2020-03-21 23:51:13 +00:00
|
|
|
coverProfiles = append(coverProfiles, coverProfile)
|
2020-03-18 22:19:44 +00:00
|
|
|
}
|
2020-02-21 22:23:20 +00:00
|
|
|
if currentUserIsRoot() {
|
|
|
|
if err := sandbox(cmd); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Dir = filepath.Join(testDir, relPath)
|
2020-04-21 13:21:00 +00:00
|
|
|
cmd.Env = []string{
|
|
|
|
testtool.BinariesEnv + "=" + string(binariesJSON),
|
|
|
|
"PATH=" + os.Getenv("PATH"),
|
2021-04-30 09:30:53 +00:00
|
|
|
"HOME=" + os.Getenv("HOME"),
|
2021-04-30 10:00:54 +00:00
|
|
|
"GOCACHE=" + goCache,
|
2020-04-21 13:21:00 +00:00
|
|
|
}
|
2020-02-21 22:23:20 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return &TestFailedError{E: err}
|
2020-02-13 00:02:31 +00:00
|
|
|
}
|
2020-01-28 22:41:27 +00:00
|
|
|
}
|
2020-02-10 13:41:58 +00:00
|
|
|
|
2022-03-12 13:25:16 +00:00
|
|
|
{
|
|
|
|
cmd := exec.Command(raceBinaries[testPkg], "-test.bench=.")
|
|
|
|
if currentUserIsRoot() {
|
|
|
|
if err := sandbox(cmd); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Dir = filepath.Join(testDir, relPath)
|
|
|
|
cmd.Env = []string{
|
|
|
|
testtool.BinariesEnv + "=" + string(binariesJSON),
|
|
|
|
"PATH=" + os.Getenv("PATH"),
|
|
|
|
"HOME=" + os.Getenv("HOME"),
|
|
|
|
"GOCACHE=" + goCache,
|
|
|
|
}
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return &TestFailedError{E: err}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 22:23:20 +00:00
|
|
|
{
|
|
|
|
benchCmd := exec.Command(testBinary, "-test.bench=.", "-test.run=^$")
|
|
|
|
if currentUserIsRoot() {
|
|
|
|
if err := sandbox(benchCmd); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2020-02-10 13:41:58 +00:00
|
|
|
|
2020-02-21 22:23:20 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
benchCmd.Dir = filepath.Join(testDir, relPath)
|
2020-04-21 13:21:00 +00:00
|
|
|
benchCmd.Env = []string{
|
|
|
|
testtool.BinariesEnv + "=" + string(binariesJSON),
|
|
|
|
"PATH=" + os.Getenv("PATH"),
|
2021-04-30 09:30:53 +00:00
|
|
|
"HOME=" + os.Getenv("HOME"),
|
2021-04-30 10:00:54 +00:00
|
|
|
"GOCACHE=" + goCache,
|
2020-04-21 13:21:00 +00:00
|
|
|
}
|
2020-02-21 22:23:20 +00:00
|
|
|
benchCmd.Stdout = &buf
|
|
|
|
benchCmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
if err := benchCmd.Run(); err != nil {
|
|
|
|
return &TestFailedError{E: err}
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(buf.String(), "no tests to run") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := compareToBaseline(testPkg, privateRepo, buf.Bytes()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-21 23:51:13 +00:00
|
|
|
if coverageReq.Enabled {
|
|
|
|
log.Printf("checking coverage is at least %.2f%%...", coverageReq.Percent)
|
|
|
|
|
2023-02-22 13:02:44 +00:00
|
|
|
percent, err := calCoverage(coverProfiles)
|
2020-03-21 23:51:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("coverage is %.2f%%", percent)
|
|
|
|
|
|
|
|
if percent < coverageReq.Percent {
|
|
|
|
return fmt.Errorf("poor coverage %.2f%%; expected at least %.2f%%",
|
|
|
|
percent, coverageReq.Percent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 22:23:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func noMoreThanTwoTimesWorse(old, new *benchstat.Metrics) (float64, error) {
|
2022-02-24 12:32:06 +00:00
|
|
|
if new.Mean > 1.99*old.Mean {
|
2020-02-21 22:23:20 +00:00
|
|
|
return 0.0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1.0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareToBaseline(testPkg, privateRepo string, run []byte) error {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2022-03-31 17:23:32 +00:00
|
|
|
goTest := exec.Command("go", "test", "-tags", "private,solution", "-bench=.", "-run=^$", testPkg)
|
2020-02-21 22:23:20 +00:00
|
|
|
goTest.Dir = privateRepo
|
|
|
|
goTest.Stdout = &buf
|
|
|
|
goTest.Stderr = os.Stderr
|
|
|
|
if err := goTest.Run(); err != nil {
|
|
|
|
return fmt.Errorf("baseline benchmark failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &benchstat.Collection{
|
|
|
|
DeltaTest: noMoreThanTwoTimesWorse,
|
|
|
|
}
|
|
|
|
c.AddConfig("baseline.txt", buf.Bytes())
|
|
|
|
c.AddConfig("new.txt", run)
|
|
|
|
|
|
|
|
tables := c.Tables()
|
|
|
|
benchstat.FormatText(os.Stderr, tables)
|
|
|
|
|
|
|
|
for _, c := range tables {
|
|
|
|
for _, r := range c.Rows {
|
|
|
|
if r.Change == -1 {
|
2020-02-22 12:37:16 +00:00
|
|
|
return fmt.Errorf("solution is worse than baseline on benchmark %q", r.Benchmark)
|
2020-02-21 22:23:20 +00:00
|
|
|
}
|
2020-01-28 22:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-12 22:54:25 +00:00
|
|
|
|
|
|
|
return nil
|
2020-01-28 22:41:27 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 22:14:20 +00:00
|
|
|
// relPaths converts paths to relative (to the baseDir) ones.
|
2020-01-28 22:41:27 +00:00
|
|
|
func relPaths(baseDir string, paths []string) []string {
|
|
|
|
ret := make([]string, len(paths))
|
|
|
|
for i, p := range paths {
|
|
|
|
relPath, err := filepath.Rel(baseDir, p)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
ret[i] = relPath
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|