-
Notifications
You must be signed in to change notification settings - Fork 5
/
experiment.go
140 lines (111 loc) · 3.26 KB
/
experiment.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
package scientist
import (
"fmt"
"os"
"reflect"
)
var ErrorOnMismatches bool
func New(name string) *Experiment {
return &Experiment{
Name: name,
Context: make(map[string]string),
ErrorOnMismatches: ErrorOnMismatches,
behaviors: make(map[string]behaviorFunc),
comparator: defaultComparator,
runcheck: defaultRunCheck,
publisher: defaultPublisher,
errorReporter: defaultErrorReporter,
beforeRun: defaultBeforeRun,
cleaner: defaultCleaner,
}
}
type behaviorFunc func() (value interface{}, err error)
type Experiment struct {
Name string
Context map[string]string
ErrorOnMismatches bool
behaviors map[string]behaviorFunc
ignores []func(control, candidate interface{}) (bool, error)
comparator func(control, candidate interface{}) (bool, error)
runcheck func() (bool, error)
publisher func(Result) error
errorReporter func(...ResultError)
beforeRun func() error
cleaner func(interface{}) (interface{}, error)
}
func (e *Experiment) Use(fn func() (interface{}, error)) {
e.Behavior(controlBehavior, fn)
}
func (e *Experiment) Try(fn func() (interface{}, error)) {
e.Behavior(candidateBehavior, fn)
}
func (e *Experiment) Behavior(name string, fn func() (interface{}, error)) {
e.behaviors[name] = fn
}
func (e *Experiment) Compare(fn func(control, candidate interface{}) (bool, error)) {
e.comparator = fn
}
func (e *Experiment) Clean(fn func(v interface{}) (interface{}, error)) {
e.cleaner = fn
}
func (e *Experiment) Ignore(fn func(control, candidate interface{}) (bool, error)) {
e.ignores = append(e.ignores, fn)
}
func (e *Experiment) RunIf(fn func() (bool, error)) {
e.runcheck = fn
}
func (e *Experiment) BeforeRun(fn func() error) {
e.beforeRun = fn
}
func (e *Experiment) Publish(fn func(Result) error) {
e.publisher = fn
}
func (e *Experiment) ReportErrors(fn func(...ResultError)) {
e.errorReporter = fn
}
func (e *Experiment) Run() (interface{}, error) {
return e.RunBehavior(controlBehavior)
}
func (e *Experiment) RunBehavior(name string) (interface{}, error) {
enabled, err := e.runcheck()
if err != nil {
enabled = true
e.errorReporter(e.resultErr("run_if", err))
return nil, err
}
if enabled && len(e.behaviors) > 1 {
r := Run(e, name)
if r.Control.Err == nil && e.ErrorOnMismatches && r.IsMismatched() {
return nil, MismatchError{r}
}
return r.Control.Value, r.Control.Err
}
behavior, ok := e.behaviors[name]
if !ok {
return nil, behaviorNotFound(e, name)
}
return behavior()
}
func (e *Experiment) resultErr(name string, err error) ResultError {
return ResultError{name, e.Name, err}
}
func defaultComparator(candidate, control interface{}) (bool, error) {
return reflect.DeepEqual(candidate, control), nil
}
func defaultRunCheck() (bool, error) {
return true, nil
}
func defaultCleaner(v interface{}) (interface{}, error) {
return v, nil
}
func defaultPublisher(r Result) error {
return nil
}
func defaultErrorReporter(errs ...ResultError) {
for _, err := range errs {
fmt.Fprintf(os.Stderr, "[scientist] error during %q for %q experiment: (%T) %v\n", err.Operation, err.Experiment, err.Err, err.Err)
}
}
func defaultBeforeRun() error {
return nil
}