|
| 1 | +// Command fast-phpunit runs the PHPUnit suite in parallel by splitting test |
| 2 | +// classes into N balanced "warm" chunks: each worker boots PHP once and runs |
| 3 | +// many test classes in a single process, so the container is built N times |
| 4 | +// instead of once per class (as tools that spawn a process per chunk do). |
| 5 | +// |
| 6 | +// Balancing is by fixture count, since Rector rule tests iterate one assertion |
| 7 | +// per .php.inc fixture, so fixture count approximates a class's runtime. |
| 8 | +package main |
| 9 | + |
| 10 | +import ( |
| 11 | + "flag" |
| 12 | + "fmt" |
| 13 | + "os" |
| 14 | + "os/exec" |
| 15 | + "path/filepath" |
| 16 | + "regexp" |
| 17 | + "runtime" |
| 18 | + "sort" |
| 19 | + "strings" |
| 20 | + "sync" |
| 21 | + "time" |
| 22 | +) |
| 23 | + |
| 24 | +type testClass struct { |
| 25 | + path string |
| 26 | + weight int // fixture count, min 1 |
| 27 | +} |
| 28 | + |
| 29 | +var fixtureCountRe = regexp.MustCompile(`\.php\.inc$`) |
| 30 | + |
| 31 | +func main() { |
| 32 | + workers := flag.Int("p", runtime.NumCPU(), "number of parallel workers") |
| 33 | + php := flag.String("php", "php", "php interpreter") |
| 34 | + // the real PHP entry script (runs cross-platform via `php`); vendor/bin/phpunit |
| 35 | + // is a shell/batch proxy on Windows and cannot be passed to php directly. |
| 36 | + phpunit := flag.String("bin", "vendor/phpunit/phpunit/phpunit", "phpunit entry script") |
| 37 | + flag.Parse() |
| 38 | + |
| 39 | + dirs := flag.Args() |
| 40 | + if len(dirs) == 0 { |
| 41 | + dirs = []string{"rules-tests", "tests"} |
| 42 | + } |
| 43 | + |
| 44 | + classes := discover(dirs) |
| 45 | + if len(classes) == 0 { |
| 46 | + fmt.Fprintln(os.Stderr, "no test classes found") |
| 47 | + os.Exit(1) |
| 48 | + } |
| 49 | + |
| 50 | + chunks := balance(classes, *workers) |
| 51 | + |
| 52 | + start := time.Now() |
| 53 | + failed := run(chunks, *php, *phpunit, *workers) |
| 54 | + elapsed := time.Since(start) |
| 55 | + |
| 56 | + totalFixtures := 0 |
| 57 | + for _, c := range classes { |
| 58 | + totalFixtures += c.weight |
| 59 | + } |
| 60 | + fmt.Printf("\n%d classes, %d fixtures, %d chunks, %d workers\n", |
| 61 | + len(classes), totalFixtures, len(chunks), *workers) |
| 62 | + fmt.Printf("wall time: %.2fs\n", elapsed.Seconds()) |
| 63 | + |
| 64 | + if failed > 0 { |
| 65 | + fmt.Printf("FAILED chunks: %d\n", failed) |
| 66 | + os.Exit(1) |
| 67 | + } |
| 68 | + fmt.Println("OK") |
| 69 | +} |
| 70 | + |
| 71 | +// discover finds *Test.php files and weights each by sibling Fixture/ file count. |
| 72 | +func discover(dirs []string) []testClass { |
| 73 | + var classes []testClass |
| 74 | + for _, dir := range dirs { |
| 75 | + _ = filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error { |
| 76 | + if err != nil || d.IsDir() || !strings.HasSuffix(path, "Test.php") { |
| 77 | + return nil |
| 78 | + } |
| 79 | + classes = append(classes, testClass{path: path, weight: fixtureWeight(path)}) |
| 80 | + return nil |
| 81 | + }) |
| 82 | + } |
| 83 | + return classes |
| 84 | +} |
| 85 | + |
| 86 | +func fixtureWeight(testPath string) int { |
| 87 | + fixtureDir := filepath.Join(filepath.Dir(testPath), "Fixture") |
| 88 | + entries, err := os.ReadDir(fixtureDir) |
| 89 | + if err != nil { |
| 90 | + return 1 |
| 91 | + } |
| 92 | + count := 0 |
| 93 | + for _, e := range entries { |
| 94 | + if !e.IsDir() && fixtureCountRe.MatchString(e.Name()) { |
| 95 | + count++ |
| 96 | + } |
| 97 | + } |
| 98 | + if count < 1 { |
| 99 | + return 1 |
| 100 | + } |
| 101 | + return count |
| 102 | +} |
| 103 | + |
| 104 | +// balance greedily packs classes (heaviest first) into n bins, always adding to |
| 105 | +// the lightest bin. Minimizes the heaviest chunk, so wall time is bounded by the |
| 106 | +// slowest worker rather than by unlucky static splits. |
| 107 | +func balance(classes []testClass, n int) [][]testClass { |
| 108 | + sort.Slice(classes, func(i, j int) bool { |
| 109 | + return classes[i].weight > classes[j].weight |
| 110 | + }) |
| 111 | + bins := make([][]testClass, n) |
| 112 | + loads := make([]int, n) |
| 113 | + for _, c := range classes { |
| 114 | + min := 0 |
| 115 | + for i := 1; i < n; i++ { |
| 116 | + if loads[i] < loads[min] { |
| 117 | + min = i |
| 118 | + } |
| 119 | + } |
| 120 | + bins[min] = append(bins[min], c) |
| 121 | + loads[min] += c.weight |
| 122 | + } |
| 123 | + var out [][]testClass |
| 124 | + for _, b := range bins { |
| 125 | + if len(b) > 0 { |
| 126 | + out = append(out, b) |
| 127 | + } |
| 128 | + } |
| 129 | + return out |
| 130 | +} |
| 131 | + |
| 132 | +func run(chunks [][]testClass, php, phpunit string, workers int) int { |
| 133 | + sem := make(chan struct{}, workers) |
| 134 | + var wg sync.WaitGroup |
| 135 | + var mu sync.Mutex |
| 136 | + failed := 0 |
| 137 | + |
| 138 | + for idx, chunk := range chunks { |
| 139 | + wg.Add(1) |
| 140 | + go func(idx int, chunk []testClass) { |
| 141 | + defer wg.Done() |
| 142 | + sem <- struct{}{} |
| 143 | + defer func() { <-sem }() |
| 144 | + |
| 145 | + // Each worker gets its own temp dir so Rector's file cache |
| 146 | + // (sys_get_temp_dir()/rector_cached_files) and the fixture temp |
| 147 | + // dumper never race across processes. sys_get_temp_dir() reads |
| 148 | + // TMPDIR on Linux/macOS and TMP/TEMP on Windows, so set all three. |
| 149 | + tmp := filepath.Join(os.TempDir(), fmt.Sprintf("fast-phpunit-%d", idx)) |
| 150 | + _ = os.MkdirAll(tmp, 0o755) |
| 151 | + defer os.RemoveAll(tmp) |
| 152 | + |
| 153 | + // invoke via `php <phpunit>` so it works uniformly on Windows, |
| 154 | + // where vendor/bin/phpunit is not directly executable. |
| 155 | + args := make([]string, 0, len(chunk)+1) |
| 156 | + args = append(args, phpunit) |
| 157 | + for _, c := range chunk { |
| 158 | + args = append(args, c.path) |
| 159 | + } |
| 160 | + cmd := exec.Command(php, args...) |
| 161 | + cmd.Env = append(os.Environ(), "TMPDIR="+tmp, "TMP="+tmp, "TEMP="+tmp) |
| 162 | + out, err := cmd.CombinedOutput() |
| 163 | + if err != nil { |
| 164 | + mu.Lock() |
| 165 | + failed++ |
| 166 | + fmt.Printf("chunk FAILED (%d classes): %v\n%s\n", len(chunk), err, tail(string(out), 15)) |
| 167 | + mu.Unlock() |
| 168 | + } |
| 169 | + }(idx, chunk) |
| 170 | + } |
| 171 | + wg.Wait() |
| 172 | + return failed |
| 173 | +} |
| 174 | + |
| 175 | +func tail(s string, lines int) string { |
| 176 | + parts := strings.Split(strings.TrimRight(s, "\n"), "\n") |
| 177 | + if len(parts) > lines { |
| 178 | + parts = parts[len(parts)-lines:] |
| 179 | + } |
| 180 | + return strings.Join(parts, "\n") |
| 181 | +} |
0 commit comments