forked from timmyyuan/gobench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goker_executor.go
101 lines (87 loc) · 2.19 KB
/
goker_executor.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
93
94
95
96
97
98
99
100
101
package gobench
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
)
type GoKerExecuter struct {
*BatchRunResult
Binary string
Source string
TestFn string
}
func newGoKerExecuter(config ExecBugConfig) *GoKerExecuter {
g := &GoKerExecuter{
BatchRunResult: &BatchRunResult{
ExecBugConfig: config,
},
}
tmpfile, err := ioutil.TempFile("", g.Bug.ID+"_*")
if err != nil {
panic(err)
}
g.Binary = tmpfile.Name()
project, id := SplitBugID(g.Bug.ID)
testfile := filepath.Join(g.SourceDir, fmt.Sprintf("%s%s_test.go", project, id))
r, _ := regexp.Compile("func Test(.*)\\(t \\*testing\\.T\\)")
code, err := ioutil.ReadFile(testfile)
if err != nil {
panic(err)
}
testfunc := strings.Split(string(r.Find(code)), " ")[1]
bound := strings.Index(testfunc, "(")
g.Source, g.TestFn = testfile, testfunc[0:bound]
return g
}
func (g *GoKerExecuter) TestEntryAndFunc() (string, string) {
return g.Source, g.TestFn
}
func (g *GoKerExecuter) Build() {
build := "test -c %v -o %v"
if g.Bug.Type == GoKerNonBlocking {
build += " -race"
}
source, _ := g.TestEntryAndFunc()
args := strings.Split(fmt.Sprintf(build, source, g.Binary), " ")
if out, err := exec.Command("go", args...).CombinedOutput(); err != nil {
panic(fmt.Sprintln(string(out), "\n", err))
}
}
func (g *GoKerExecuter) Run() *SingleRunResult {
command := "%v -test.v -test.count %v -test.failfast -test.timeout %v"
vals := []interface{}{g.Binary, g.Count, g.Timeout}
if g.Cpu != 0 {
command += " -test.cpu %v"
vals = append(vals, g.Cpu)
}
args := strings.Split(fmt.Sprintf(command, vals...), " ")
result := g.next()
result.Command = strings.Join(args, " ")
result.process(func() {
var err error
result.Logs, err = exec.Command(args[0], args[1:]...).CombinedOutput()
if err != nil {
result.ExitCode = 1
}
})
return result
}
func (g *GoKerExecuter) Done() {
fulllog := g.log()
backup := filepath.Join(g.OutputDir, "full.log")
if err := ioutil.WriteFile(backup, []byte(fulllog), 0644); err != nil {
panic(err)
}
if g.ClearDirs {
if err := os.Remove(g.Binary); err != nil {
panic(err)
}
}
}
func (g *GoKerExecuter) GetResult() *BatchRunResult {
return g.BatchRunResult
}