-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgobench_suite.go
196 lines (168 loc) · 3.76 KB
/
gobench_suite.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package gobench
import (
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"sync"
)
type SuiteConfig struct {
ExecEnvConfig
Name string
Type SubBenchType
BugIDs []string
MustFork bool
WorkDirName string
Jobs int
ExecutorCreator func(config ExecBugConfig) Executor
SetUpFunc func()
ShutDownFunc func()
}
type Suite struct {
SuiteConfig
BugSet *BugSet
executors sync.Map
}
func NewSuite(config SuiteConfig) *Suite {
s := &Suite{
SuiteConfig: config,
}
if s.Jobs <= 0 {
s.Jobs = runtime.GOMAXPROCS(0)
}
var bugs []Bug
allBugs := GoBenchBugSet.ListByTypes(s.Type)
if len(s.BugIDs) == 0 {
bugs = allBugs
} else {
for _, b := range allBugs {
for _, id := range s.BugIDs {
if id != b.ID {
continue
}
bugs = append(bugs, b)
break
}
}
}
workdir := s.WorkDir()
for i := range bugs {
bugs[i].fork(workdir)
}
s.BugSet = NewBugSet(bugs)
for _, bug := range s.BugSet.Bugs() {
if bug.isGoReal() {
NewGoRealBugConfig(bug, s.BugConfigPath).UpdateDockerfile()
}
}
return s
}
func (s *Suite) WorkDir() string {
basename := fmt.Sprintf("gobench-%s", s.Name)
if s.WorkDirName == "" {
return filepath.Join(os.TempDir(), basename)
}
dir, err := os.Getwd()
if err != nil {
panic(err)
}
return filepath.Join(dir, s.WorkDirName, basename)
}
func (s *Suite) build() {
bugs := s.BugSet.Bugs()
var tasks []func()
for _, bug := range bugs {
bug := bug
tasks = append(tasks, func() {
execConfig := ExecBugConfig{
ExecEnvConfig: s.ExecEnvConfig,
Bug: bug,
SourceDir: bug.forkedPath,
OutputDir: bug.forkedPath,
}
var executor Executor
if s.ExecutorCreator != nil {
executor = s.ExecutorCreator(execConfig)
} else {
executor = NewDefaultExecutor(execConfig)
}
executor.Build()
s.executors.Store(bug.ID, executor)
})
}
if !BatchJobs(tasks, runtime.GOMAXPROCS(0)) {
panic(fmt.Sprintf("ERROR: This suite %s was failed in building.\n", s.BugSet))
}
}
func (s *Suite) Run() {
if s.SetUpFunc != nil {
s.SetUpFunc()
}
s.build()
if s.ShutDownFunc != nil {
defer s.ShutDownFunc()
}
if s.Jobs == 1 {
s.executors.Range(func(bugid interface{}, executor interface{}) bool {
for i := 0; i < s.Repeat; i++ {
executor.(Executor).Run()
}
executor.(Executor).Done()
return true
})
return
}
var tasks []func()
s.executors.Range(func(bugid interface{}, executor interface{}) bool {
worker := executor.(Executor)
tasks = append(tasks, func() {
for i := 0; i < s.Repeat; i++ {
worker.Run()
}
worker.Done()
})
return true
})
// FIXME: change to context.Timeout here
if !BatchJobs(tasks, s.Jobs) {
log.Printf("WARNING: This suite %s was not finished.\n", s.BugSet)
}
}
func (s *Suite) TestFiles() (files []string) {
if (s.Type&GoRealBlocking)|(s.Type&GoRealNonBlocking) != 0 {
panic("Not implement yet")
}
for _, bug := range s.BugSet.Bugs() {
project, id := SplitBugID(bug.ID)
testfile := fmt.Sprintf("%s%s_test.go", project, id)
files = append(files, filepath.Join(bug.Dir(), testfile))
}
return
}
func (s *Suite) GetResult(bugid string) *BatchRunResult {
if e, exist := s.executors.Load(bugid); exist {
return e.(Executor).GetResult()
}
panic(fmt.Errorf("Can't find %s in %s", bugid, s.BugSet))
}
func (s *Suite) Positives() map[string][]Bug {
results := make(map[string][]Bug)
bugs := s.BugSet.Bugs()
for _, bug := range bugs {
if s.GetResult(bug.ID).IsPositive() {
results[bug.SubType] = append(results[bug.SubType], bug)
}
}
return results
}
func (s *Suite) Negatives() map[string][]Bug {
results := make(map[string][]Bug)
bugs := s.BugSet.Bugs()
for _, bug := range bugs {
if s.GetResult(bug.ID).IsNegative() {
results[bug.SubType] = append(results[bug.SubType], bug)
}
}
return results
}