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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
package main
import (
"fmt"
"log"
"os"
"os/exec"
"strings"
"sync"
"time"
)
const (
progName = "test-cover-parallel.go"
)
func main() {
if len(os.Args) <= 2 {
log.Fatalf("usage %s OUT_DIR PKG [PKG...]", progName)
}
outDir := os.Args[1]
packages := os.Args[2:]
if err := buildDependentPackages(packages); err != nil {
log.Fatal(err)
}
numWorkers := 2
cmdChan := make(chan *exec.Cmd)
wg := &sync.WaitGroup{}
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go func() {
for cmd := range cmdChan {
runCover(cmd)
}
wg.Done()
}()
}
packageMap := make(map[string]bool, len(packages))
for _, pkg := range packages {
packageMap[pkg] = true
}
for _, pkg := range packages {
deps, err := depsForPackage(pkg, packageMap)
if err != nil {
log.Fatal(err)
}
args := []string{
"go",
"test",
fmt.Sprintf("-coverpkg=%s", strings.Join(deps, ",")),
fmt.Sprintf("-coverprofile=%s/unit-%s.out", outDir, strings.Replace(pkg, "/", "_", -1)),
pkg,
}
cmdChan <- exec.Command(args[0], args[1:]...)
}
close(cmdChan)
wg.Wait()
}
func depsForPackage(pkg string, packageMap map[string]bool) ([]string, error) {
depsOut, err := exec.Command("go", "list", "-f", `{{ join .Deps "\n" }}`, pkg).Output()
if err != nil {
return nil, err
}
deps := []string{pkg}
for _, d := range strings.Split(string(depsOut), "\n") {
if packageMap[d] {
deps = append(deps, d)
}
}
return deps, nil
}
func buildDependentPackages(packages []string) error {
buildDeps := exec.Command("go", append([]string{"test", "-i"}, packages...)...)
buildDeps.Stdout = os.Stdout
buildDeps.Stderr = os.Stderr
start := time.Now()
if err := buildDeps.Run(); err != nil {
log.Printf("command failed: %s", strings.Join(buildDeps.Args, " "))
return err
}
log.Printf("go test -i\t%.3fs", time.Since(start).Seconds())
return nil
}
func runCover(cmd *exec.Cmd) {
start := time.Now()
err := cmd.Run()
duration := time.Since(start)
status := fmt.Sprintf("%s\t%.3fs", cmd.Args[len(cmd.Args)-1], duration.Seconds())
if err != nil {
fmt.Printf("FAIL\t%s\n", status)
fmt.Printf("command was: %s\n", strings.Join(cmd.Args, " "))
} else {
fmt.Printf("ok \t%s\n", status)
}
}
|