-
Notifications
You must be signed in to change notification settings - Fork 2
/
runner.go
80 lines (66 loc) · 1.92 KB
/
runner.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
package bigo
// TestRunner runs big o time complexity tests.
// BigO.Run() calls the Step method for every N and expects OMeasures to be returned.
type TestRunner interface {
Step(n float64) OMeasures
}
// RunConfig gives detailed configuration to BigO.
type RunConfig struct {
Speed float64
}
// New *BigO.
func New(name string, testRunner TestRunner, stepper Stepper) *BigO {
return NewWithConfig(name, testRunner, stepper, RunConfig{Speed: 1})
}
// New *BigO with config.
func NewWithConfig(name string, testRunner TestRunner, stepper Stepper, runConfig RunConfig) *BigO {
return &BigO{
Name: name,
Runner: testRunner,
Results: Results{},
NStepper: stepper,
RunConfig: runConfig,
}
}
// BigO holds values and methods to run tests.
type BigO struct {
Name string
Runner TestRunner
Results Results
NStepper Stepper
RunConfig RunConfig
}
// Run starts a test run, calls the given TestRunner for every N consumed from the given Stepper.
func (r *BigO) Run() *BigO {
for {
n, ok := r.NStepper.Next()
if !ok {
break
}
results := r.Runner.Step(n / r.RunConfig.Speed)
r.Results = append(r.Results, Result{
N: n,
OMeasures: results,
})
}
return r
}
// WriteResultsToJSON writes the captured results to a json file.
func (r *BigO) WriteResultsToJSON() *BigO {
WriteResultsToJSONFile(r.Name, r.Results)
return r
}
// PlotResults plots a graph from the captured results to a png file.
func (r *BigO) PlotResults() *BigO {
PlotTestResults(r.Name, PlotSeriesList{PlotSeries{Name: r.Name, Results: r.Results}})
return r
}
// PlotResultsWithConfig plots a graph from the captured results to a png file.
func (r *BigO) PlotResultsWithConfig(plotConfig PlotConfig) *BigO {
PlotTestResultsWithConfig(r.Name, PlotSeriesList{PlotSeries{Name: r.Name, Results: r.Results}}, plotConfig)
return r
}
// GetResults returns captured results.
func (r *BigO) GetResults() Results {
return r.Results
}