github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/bench/call/benchall/main.go (about)

     1  package main
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"os/exec"
     9  	"strings"
    10  
    11  	"golang.org/x/sync/errgroup"
    12  )
    13  
    14  var versions = []string{
    15  	"go1.7.6",
    16  	"go1.8.7",
    17  	"go1.9.7",
    18  	"go1.10.8",
    19  	"go1.11.13",
    20  	"go1.12.17",
    21  	"go1.13.15",
    22  	"go1.14.15",
    23  	"go1.15.15",
    24  	"go1.16.15",
    25  	"go1.17.13",
    26  	"go1.18.10",
    27  	"go1.19.12",
    28  	"go1.20.7",
    29  	"go1.21.0",
    30  }
    31  
    32  func main() {
    33  	var g errgroup.Group
    34  	g.SetLimit(4)
    35  	for _, version := range versions {
    36  		version := version
    37  		// if _, err := exec.LookPath(version); err == nil {
    38  		// 	continue
    39  		// }
    40  		g.Go(func() error {
    41  			fmt.Println("Installing", version)
    42  			must0(run("go", "install", "golang.org/dl/"+version+"@latest"))
    43  			must0(run(version, "download"))
    44  			return nil
    45  		})
    46  	}
    47  	g.Wait()
    48  
    49  	for _, version := range versions {
    50  		fmt.Println("# Benchmarking ", version)
    51  		runtee(version+".log", version, "test", "-bench=.", "-count=6", ".")
    52  	}
    53  }
    54  
    55  func must[T any](v T, err error) T {
    56  	if err != nil {
    57  		fmt.Fprintln(os.Stderr, err)
    58  		os.Exit(1)
    59  	}
    60  	return v
    61  }
    62  
    63  func must0(err error) {
    64  	if err != nil {
    65  		fmt.Fprintln(os.Stderr, err)
    66  		os.Exit(1)
    67  	}
    68  }
    69  
    70  func run(cmd string, args ...string) error {
    71  	fmt.Println("# ", cmd, strings.Join(args, " "))
    72  	x := exec.Command(cmd, args...)
    73  	x.Stderr = os.Stderr
    74  	x.Stdout = os.Stdout
    75  	return x.Run()
    76  }
    77  
    78  func runtee(outfile string, cmd string, args ...string) error {
    79  	fmt.Println("# ", cmd, strings.Join(args, " "))
    80  	x := exec.Command(cmd, args...)
    81  	f, err := os.Create(outfile)
    82  	if err != nil {
    83  		return err
    84  	}
    85  	defer f.Close()
    86  
    87  	bf := bufio.NewWriter(f)
    88  	defer bf.Flush()
    89  
    90  	x.Stderr = os.Stderr
    91  	x.Stdout = io.MultiWriter(os.Stdout, bf)
    92  	return x.Run()
    93  }
    94  
    95  /*
    96  #!/bin/bash
    97  
    98  declare -a versions=(
    99  )
   100  
   101  echo "# Installing Go versions"
   102  
   103  for ver in "${versions[@]}"
   104  do
   105  	go install "golang.org/dl/${ver}@latest"
   106  	${ver} download
   107  done
   108  
   109  echo "# Benchmarking"
   110  
   111  for ver in "${versions[@]}"
   112  do
   113  	$
   114  {ver} test -bench . -count 6 | tee ${ver}.log
   115  done
   116  */