/
shampoo.go
92 lines (75 loc) · 1.74 KB
/
shampoo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"log"
"os/exec"
"strconv"
"sync"
"github.com/tj/docopt"
)
const (
Usage = `Shampoo.
Shampoo away your flaky tests.
Usage:
shampoo [--iterations=<i>] [--parallel] <cmd> [<args>...]
shampoo -h | --help
shampoo --version
Options:
-h --help Show this screen.
--version Show version.
--iterations=<i> Number of iterations to run [default: 10].
--parallel Run in parallel.`
Version = `1.0.0`
)
func main() {
arguments, err := docopt.Parse(Usage, nil, true, Version, false)
check(err)
cmd := arguments["<cmd>"].(string)
args := arguments["<args>"].([]string)
iterations := parseInt(arguments["--iterations"].(string))
parallel := arguments["--parallel"].(bool)
fmt.Println("Running", cmd, args, iterations, "times.")
if parallel {
executeParallel(iterations, cmd, args...)
} else {
executeSerial(iterations, cmd, args...)
}
fmt.Println()
fmt.Println("Completed", iterations, "iterations without any errors.")
}
func execute(cmd string, args ...string) {
fmt.Printf(".")
output, err := exec.Command(cmd, args...).Output()
if err != nil {
// Dump the output.
fmt.Println(output)
// Print the error.
log.Fatal(err)
}
}
func executeSerial(iterations int, cmd string, args ...string) {
for i := 0; i < iterations; i++ {
execute(cmd, args...)
}
}
func executeParallel(iterations int, cmd string, args ...string) {
var wg sync.WaitGroup
for i := 0; i < iterations; i++ {
wg.Add(1)
go func() {
defer wg.Done()
execute(cmd, args...)
}()
}
wg.Wait()
}
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func parseInt(arg string) int {
num, err := strconv.ParseInt(arg, 10, 32)
check(err)
return int(num)
}