Automatically limit integration test parallelism

pull/8537/head
Thomas Stromberg 2020-06-23 09:10:56 -07:00
parent f994b702f6
commit 0fd4f17c0a
1 changed files with 33 additions and 0 deletions

View File

@ -19,8 +19,10 @@ package integration
import ( import (
"flag" "flag"
"fmt" "fmt"
"math"
"os" "os"
"runtime" "runtime"
"strconv"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -51,12 +53,43 @@ const (
// TestMain is the test main // TestMain is the test main
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
flag.Parse() flag.Parse()
setMaxParallelism()
start := time.Now() start := time.Now()
code := m.Run() code := m.Run()
fmt.Printf("Tests completed in %s (result code %d)\n", time.Since(start), code) fmt.Printf("Tests completed in %s (result code %d)\n", time.Since(start), code)
os.Exit(code) os.Exit(code)
} }
// setMaxParallelism caps the max parallelism. Go assumes 1 core per test, whereas minikube needs 2 cores per test.
func setMaxParallelism() {
procs := runtime.GOMAXPROCS(0)
pval := flag.Lookup("test.parallel").Value.String()
pv, err := strconv.Atoi(pval)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to parse --test.parallel value: %q\n", pval)
return
}
if procs != pv {
fmt.Fprintf(os.Stderr, "--test-parallel=%d was set via flags (system has %d cores)\n", pv, procs)
return
}
if procs == 2 {
fmt.Fprintf(os.Stderr, "Found %d cores, will not round down core count.\n", procs)
return
}
// During "minikube start", minikube briefly consumes ~1.5 cores. Most of the time it's less.
limit := int(math.Floor(float64(procs) * 0.75))
fmt.Fprintf(os.Stderr, "Found %d cores, limiting parallelism with --test.parallel=%d\n", procs, limit)
if err := flag.Set("test.parallel", strconv.Itoa(limit)); err != nil {
fmt.Fprintf(os.Stderr, "Unable to set test.parallel: %v\n", err)
}
runtime.GOMAXPROCS(limit)
}
// StartArgs returns the arguments normally used for starting minikube // StartArgs returns the arguments normally used for starting minikube
func StartArgs() []string { func StartArgs() []string {
return strings.Split(*startArgs, " ") return strings.Split(*startArgs, " ")