github.com/golangci/go-tools@v0.0.0-20190318060251-af6baa5dc196/_benchmarks/benchmark.go (about)

     1  // +build ignore
     2  
     3  package main
     4  
     5  import (
     6  	"bytes"
     7  	"flag"
     8  	"fmt"
     9  	"os/exec"
    10  	"strings"
    11  )
    12  
    13  const theBigMerge = "0a9027c2bab9ca0d25a5db0f906fd1793774fd67"
    14  const N = 10
    15  
    16  func isAfterMerge(sha string) bool {
    17  	cmd := exec.Command("git", "merge-base", "--is-ancestor", theBigMerge, sha)
    18  	err := cmd.Run()
    19  	if err == nil {
    20  		return true
    21  	}
    22  	_ = err.(*exec.ExitError)
    23  	return false
    24  }
    25  
    26  func checkout(sha string) {
    27  	cmd := exec.Command("git", "checkout", "-q", sha)
    28  	err := cmd.Run()
    29  	if err != nil {
    30  		panic(err)
    31  	}
    32  }
    33  
    34  func build(tool string) {
    35  	err := exec.Command("go", "build", "-o", "/tmp/"+tool, "github.com/golangci/go-tools/cmd/"+tool).Run()
    36  	if err != nil {
    37  		panic(err)
    38  	}
    39  }
    40  
    41  func run(tool, target string) (time, mem string) {
    42  	cmd := exec.Command("/usr/bin/time", "-f", "%e %M", "/tmp/"+tool, target)
    43  	out, _ := cmd.CombinedOutput()
    44  	lines := bytes.Split(out, []byte("\n"))
    45  	res := string(lines[len(lines)-2])
    46  	fields := strings.Split(res, " ")
    47  	return fields[0], fields[1]
    48  }
    49  
    50  func main() {
    51  	var (
    52  		shas    string
    53  		targets string
    54  		version string
    55  	)
    56  	flag.StringVar(&shas, "shas", "HEAD", "")
    57  	flag.StringVar(&targets, "targets", "std", "")
    58  	flag.StringVar(&version, "version", "unknown", "")
    59  	flag.Parse()
    60  
    61  	for _, sha := range strings.Split(shas, ",") {
    62  		tool := "megacheck"
    63  		if isAfterMerge(sha) {
    64  			tool = "staticcheck"
    65  		}
    66  		checkout(sha)
    67  		build(tool)
    68  
    69  		for _, target := range strings.Split(targets, ",") {
    70  			for i := 0; i < N; i++ {
    71  				time, mem := run(tool, target)
    72  				fmt.Printf("%s %s %s %s %s\n", sha, version, target, time, mem)
    73  			}
    74  		}
    75  	}
    76  }