-
Notifications
You must be signed in to change notification settings - Fork 5
/
scientist.go
184 lines (150 loc) · 3.57 KB
/
scientist.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
package scientist
import (
"fmt"
"time"
)
const (
controlBehavior = "control"
candidateBehavior = "candidate"
)
type Observation struct {
Experiment *Experiment
Name string
Started time.Time
Runtime time.Duration
Value interface{}
Err error
}
func (o *Observation) CleanedValue() (interface{}, error) {
return o.Experiment.cleaner(o.Value)
}
type Result struct {
Experiment *Experiment
Control *Observation
Observations []*Observation
Candidates []*Observation
Ignored []*Observation
Mismatched []*Observation
Errors []ResultError
}
func (r Result) IsMatched() bool {
if r.IsMismatched() || r.IsIgnored() {
return false
}
return true
}
func (r Result) IsMismatched() bool {
return len(r.Mismatched) > 0
}
func (r Result) IsIgnored() bool {
return len(r.Ignored) > 0
}
func Run(e *Experiment, name string) Result {
r := Result{Experiment: e}
if err := e.beforeRun(); err != nil {
r.Errors = append(r.Errors, e.resultErr("before_run", err))
}
numCandidates := len(e.behaviors) - 1
r.Control = observe(e, name, e.behaviors[name])
r.Candidates = make([]*Observation, numCandidates)
r.Ignored = make([]*Observation, 0, numCandidates)
r.Mismatched = make([]*Observation, 0, numCandidates)
r.Observations = make([]*Observation, numCandidates+1)
r.Observations[0] = r.Control
i := 0
for bname, b := range e.behaviors {
if bname == name {
continue
}
c := observe(e, bname, b)
r.Candidates[i] = c
i += 1
r.Observations[i] = c
ok, err := matching(e, r.Control, c)
if err != nil {
ok = false
r.Errors = append(r.Errors, e.resultErr("compare", err))
}
if ok {
continue
}
ignored, err := ignoring(e, r.Control, c)
if err != nil {
ignored = false
r.Errors = append(r.Errors, e.resultErr("ignore", err))
}
if ignored {
r.Ignored = append(r.Ignored, c)
} else {
r.Mismatched = append(r.Mismatched, c)
}
}
if err := e.publisher(r); err != nil {
r.Errors = append(r.Errors, e.resultErr("publish", err))
}
if len(r.Errors) > 0 {
e.errorReporter(r.Errors...)
}
return r
}
func matching(e *Experiment, control, candidate *Observation) (bool, error) {
// neither returned errors
if control.Err == nil && candidate.Err == nil {
return e.comparator(control.Value, candidate.Value)
}
// both returned errors
if control.Err != nil && candidate.Err != nil {
return control.Err.Error() == candidate.Err.Error(), nil
}
// returned different errors
return false, nil
}
func ignoring(e *Experiment, control, candidate *Observation) (bool, error) {
for _, i := range e.ignores {
ok, err := i(control.Value, candidate.Value)
if err != nil {
return false, err
}
if ok {
return true, nil
}
}
return false, nil
}
func behaviorNotFound(e *Experiment, name string) error {
return fmt.Errorf("Behavior %q not found for experiment %q", name, e.Name)
}
func observe(e *Experiment, name string, b behaviorFunc) *Observation {
o := &Observation{
Experiment: e,
Name: name,
Started: time.Now(),
}
if b == nil {
b = e.behaviors[name]
}
if b == nil {
o.Runtime = time.Since(o.Started)
o.Err = behaviorNotFound(e, name)
} else {
v, err := b()
o.Runtime = time.Since(o.Started)
o.Value = v
o.Err = err
}
return o
}
type ResultError struct {
Operation string
Experiment string
Err error
}
func (e ResultError) Error() string {
return e.Err.Error()
}
type MismatchError struct {
Result Result
}
func (e MismatchError) Error() string {
return fmt.Sprintf("[scientist] experiment %q observations mismatched", e.Result.Experiment.Name)
}