|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + |
| 10 | + zglob "github.com/mattn/go-zglob" |
| 11 | +) |
| 12 | + |
| 13 | +var useCircleCI bool |
| 14 | +var useJUnitXML bool |
| 15 | +var junitXMLPath string |
| 16 | +var testFilePattern = "" |
| 17 | +var circleCIProjectPrefix = "" |
| 18 | +var circleCIBranchName = os.Getenv("CIRCLE_BRANCH") |
| 19 | +var splitIndex, _ = strconv.Atoi(os.Getenv("CIRCLE_NODE_INDEX")) |
| 20 | +var splitTotal, _ = strconv.Atoi(os.Getenv("CIRCLE_NODE_TOTAL")) |
| 21 | +var circleCIAPIKey = os.Getenv("CIRCLECI_API_KEY") |
| 22 | + |
| 23 | +func printMsg(msg string, args ...interface{}) { |
| 24 | + if len(args) == 0 { |
| 25 | + fmt.Fprint(os.Stderr, msg) |
| 26 | + } else { |
| 27 | + fmt.Fprintf(os.Stderr, msg, args...) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func fatalMsg(msg string, args ...interface{}) { |
| 32 | + printMsg(msg, args...) |
| 33 | + os.Exit(1) |
| 34 | +} |
| 35 | + |
| 36 | +func removeDeletedFiles(fileTimes map[string]float64, currentFileSet map[string]bool) { |
| 37 | + for file := range fileTimes { |
| 38 | + if !currentFileSet[file] { |
| 39 | + delete(fileTimes, file) |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func addNewFiles(fileTimes map[string]float64, currentFileSet map[string]bool) { |
| 45 | + averageFileTime := 0.0 |
| 46 | + if len(fileTimes) > 0 { |
| 47 | + for _, time := range fileTimes { |
| 48 | + averageFileTime += time |
| 49 | + } |
| 50 | + averageFileTime /= float64(len(fileTimes)) |
| 51 | + } else { |
| 52 | + averageFileTime = 1.0 |
| 53 | + } |
| 54 | + |
| 55 | + for file := range currentFileSet { |
| 56 | + if _, isSet := fileTimes[file]; isSet { |
| 57 | + continue |
| 58 | + } |
| 59 | + printMsg("missing file time for %s\n", file) |
| 60 | + fileTimes[file] = averageFileTime |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func splitFiles(fileTimes map[string]float64, splitTotal int) ([][]string, []float64) { |
| 65 | + buckets := make([][]string, splitTotal) |
| 66 | + bucketTimes := make([]float64, splitTotal) |
| 67 | + for len(fileTimes) > 0 { |
| 68 | + // find file with max weight |
| 69 | + maxFile := "" |
| 70 | + for file, time := range fileTimes { |
| 71 | + if len(maxFile) == 0 || time > fileTimes[maxFile] { |
| 72 | + maxFile = file |
| 73 | + } |
| 74 | + } |
| 75 | + // find bucket with min weight |
| 76 | + minBucket := 0 |
| 77 | + for bucket := 1; bucket < splitTotal; bucket++ { |
| 78 | + if bucketTimes[bucket] < bucketTimes[minBucket] { |
| 79 | + minBucket = bucket |
| 80 | + } |
| 81 | + } |
| 82 | + buckets[minBucket] = append(buckets[minBucket], maxFile) |
| 83 | + bucketTimes[minBucket] += fileTimes[maxFile] |
| 84 | + delete(fileTimes, maxFile) |
| 85 | + } |
| 86 | + |
| 87 | + return buckets, bucketTimes |
| 88 | +} |
| 89 | + |
| 90 | +func parseFlags() { |
| 91 | + flag.StringVar(&testFilePattern, "glob", "spec/**/*_spec.rb", "Glob pattern to find test files") |
| 92 | + |
| 93 | + flag.IntVar(&splitIndex, "split-index", -1, "This test container's index (or set CIRCLE_NODE_INDEX)") |
| 94 | + flag.IntVar(&splitTotal, "split-total", -1, "Total number of containers (or set CIRCLE_NODE_TOTAL)") |
| 95 | + |
| 96 | + flag.StringVar(&circleCIAPIKey, "circleci-key", "", "CircleCI API key (or set CIRCLECI_API_KEY environment variable) - required to use CircleCI") |
| 97 | + flag.StringVar(&circleCIProjectPrefix, "circleci-project", "", "CircleCI project name (e.g. github/leonid-shevtsov/circleci-test-balancer) - required to use CircleCI") |
| 98 | + flag.StringVar(&circleCIBranchName, "circleci-branch", "", "Current branch for CircleCI (or set CIRCLE_BRANCH) - required to use CircleCI") |
| 99 | + |
| 100 | + flag.BoolVar(&useJUnitXML, "junit", false, "Use a JUnit XML report for test times") |
| 101 | + flag.StringVar(&junitXMLPath, "junit-path", "", "Path to a JUnit XML report (leave empty to read from stdin)") |
| 102 | + |
| 103 | + var showHelp bool |
| 104 | + flag.BoolVar(&showHelp, "help", false, "Show this help text") |
| 105 | + |
| 106 | + flag.Parse() |
| 107 | + |
| 108 | + var err error |
| 109 | + if circleCIAPIKey == "" { |
| 110 | + circleCIAPIKey = os.Getenv("CIRCLECI_API_KEY") |
| 111 | + } |
| 112 | + if circleCIBranchName == "" { |
| 113 | + circleCIBranchName = os.Getenv("CIRCLE_BRANCH") |
| 114 | + } |
| 115 | + if splitTotal == -1 { |
| 116 | + splitTotal, err = strconv.Atoi(os.Getenv("CIRCLE_NODE_TOTAL")) |
| 117 | + if err != nil { |
| 118 | + splitIndex = -1 |
| 119 | + } |
| 120 | + } |
| 121 | + if splitIndex == -1 { |
| 122 | + splitIndex, err = strconv.Atoi(os.Getenv("CIRCLE_NODE_INDEX")) |
| 123 | + if err != nil { |
| 124 | + splitIndex = -1 |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + useCircleCI = circleCIAPIKey != "" |
| 129 | + showHelp = showHelp || !useCircleCI && !useJUnitXML |
| 130 | + |
| 131 | + if showHelp { |
| 132 | + printMsg("Splits test files into containers of even duration\n\n") |
| 133 | + flag.PrintDefaults() |
| 134 | + os.Exit(1) |
| 135 | + } |
| 136 | + if useCircleCI && (circleCIProjectPrefix == "" || circleCIBranchName == "") { |
| 137 | + fatalMsg("Incomplete CircleCI configuration (set -circleci-key, -circleci-project, and -circleci-branch\n") |
| 138 | + } |
| 139 | + if splitTotal == 0 || splitIndex < 0 || splitIndex > splitTotal { |
| 140 | + fatalMsg("-split-index and -split-total (and environment variables) are missing or invalid\n") |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func main() { |
| 145 | + parseFlags() |
| 146 | + |
| 147 | + fileTimes := make(map[string]float64) |
| 148 | + if useJUnitXML { |
| 149 | + getFileTimesFromJUnitXML(fileTimes) |
| 150 | + } else if useCircleCI { |
| 151 | + getFileTimesFromCircleCI(fileTimes) |
| 152 | + } |
| 153 | + |
| 154 | + currentFiles, _ := zglob.Glob(testFilePattern) |
| 155 | + currentFileSet := make(map[string]bool) |
| 156 | + |
| 157 | + for _, file := range currentFiles { |
| 158 | + currentFileSet[file] = true |
| 159 | + } |
| 160 | + |
| 161 | + removeDeletedFiles(fileTimes, currentFileSet) |
| 162 | + addNewFiles(fileTimes, currentFileSet) |
| 163 | + |
| 164 | + buckets, bucketTimes := splitFiles(fileTimes, splitTotal) |
| 165 | + printMsg("expected test time: %0.1fs\n", bucketTimes[splitIndex]) |
| 166 | + |
| 167 | + fmt.Println(strings.Join(buckets[splitIndex], " ")) |
| 168 | +} |
0 commit comments