forked from docker/compose-on-kubernetes
/
reporting.go
103 lines (92 loc) · 2.5 KB
/
reporting.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
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"time"
"github.com/pkg/errors"
)
type reporter interface {
onTick(start time.Time, states []*workerState)
onFinish(start time.Time, states []*workerState, err error) error
}
type jsonReporter struct {
w io.Writer
}
func (r jsonReporter) onTick(start time.Time, states []*workerState) {
}
func (r jsonReporter) onFinish(start time.Time, states []*workerState, err error) error {
errText := ""
if err != nil {
errText = err.Error()
}
bytes, err := json.Marshal(&benchmarkReport{
Start: start,
Error: errText,
Succeeded: err == nil,
WorkerStates: states,
})
if err != nil {
return err
}
_, err = r.w.Write(bytes)
return err
}
type textReporter struct {
printer *statusPrinter
interactive bool
}
func (r textReporter) onTick(start time.Time, states []*workerState) {
if r.interactive {
r.printer.print(states, start, true)
}
}
func (r textReporter) onFinish(start time.Time, states []*workerState, err error) error {
fmt.Fprintf(r.printer.out, "---\nexecution total time: %v\n", time.Since(start))
if err != nil {
fmt.Fprintf(r.printer.out, "benchmark failed: %s\n", err)
return nil
}
fmt.Fprintln(r.printer.out, "workers state:")
r.printer.print(states, start, false)
fmt.Fprintln(r.printer.out)
fmt.Fprintln(r.printer.out, "average timings:")
timings := computePhaseTimingAverages(start, states)
for _, t := range timings {
fmt.Fprintf(r.printer.out, "%s: %v\n", t.name, t.duration)
}
fmt.Fprintln(r.printer.out, "benchmark succeeded")
return nil
}
func buildReporter(out io.Writer, format string) (reporter, error) {
switch format {
case "json":
return jsonReporter{w: out}, nil
case "report":
return textReporter{interactive: false, printer: &statusPrinter{out: out}}, nil
case "interactive":
return textReporter{interactive: true, printer: &statusPrinter{out: out}}, nil
}
return nil, errors.Errorf("unsupported format %s", format)
}
func reportBenchStatus(ctx context.Context, out io.Writer, finishedC chan error, start time.Time, format string, states []*workerState) error {
ticker := time.NewTicker(time.Second * 1)
reporter, err := buildReporter(out, format)
if err != nil {
return err
}
for {
select {
case err := <-finishedC:
if e := reporter.onFinish(start, states, err); e != nil {
panic(errors.Wrap(e, "failed to print report"))
}
return err
case <-ticker.C:
reporter.onTick(start, states)
case <-ctx.Done():
return errors.New("benchmark timeout")
}
}
}