...
1 package gmeasure
2
3 import (
4 "fmt"
5 "time"
6
7 "github.com/onsi/gomega/gmeasure/table"
8 )
9
10
13 type Stat uint
14
15 const (
16 StatInvalid Stat = iota
17 StatMin
18 StatMax
19 StatMean
20 StatMedian
21 StatStdDev
22 )
23
24 var statEnumSupport = newEnumSupport(map[uint]string{uint(StatInvalid): "INVALID STAT", uint(StatMin): "Min", uint(StatMax): "Max", uint(StatMean): "Mean", uint(StatMedian): "Median", uint(StatStdDev): "StdDev"})
25
26 func (s Stat) String() string { return statEnumSupport.String(uint(s)) }
27 func (s *Stat) UnmarshalJSON(b []byte) error {
28 out, err := statEnumSupport.UnmarshJSON(b)
29 *s = Stat(out)
30 return err
31 }
32 func (s Stat) MarshalJSON() ([]byte, error) { return statEnumSupport.MarshJSON(uint(s)) }
33
34 type StatsType uint
35
36 const (
37 StatsTypeInvalid StatsType = iota
38 StatsTypeValue
39 StatsTypeDuration
40 )
41
42 var statsTypeEnumSupport = newEnumSupport(map[uint]string{uint(StatsTypeInvalid): "INVALID STATS TYPE", uint(StatsTypeValue): "StatsTypeValue", uint(StatsTypeDuration): "StatsTypeDuration"})
43
44 func (s StatsType) String() string { return statsTypeEnumSupport.String(uint(s)) }
45 func (s *StatsType) UnmarshalJSON(b []byte) error {
46 out, err := statsTypeEnumSupport.UnmarshJSON(b)
47 *s = StatsType(out)
48 return err
49 }
50 func (s StatsType) MarshalJSON() ([]byte, error) { return statsTypeEnumSupport.MarshJSON(uint(s)) }
51
52
57 type Stats struct {
58
59 Type StatsType
60
61
62 ExperimentName string
63
64
65 MeasurementName string
66
67
68 Units string
69
70
71 Style string
72
73
74
75
76 PrecisionBundle PrecisionBundle
77
78
79 N int
80
81
82 ValueBundle map[Stat]float64
83
84
85 DurationBundle map[Stat]time.Duration
86
87
88
89 AnnotationBundle map[Stat]string
90 }
91
92
93 func (s Stats) String() string {
94 return fmt.Sprintf("%s < [%s] | <%s> ±%s < %s", s.StringFor(StatMin), s.StringFor(StatMedian), s.StringFor(StatMean), s.StringFor(StatStdDev), s.StringFor(StatMax))
95 }
96
97
98
99
100
101
102
103 func (s Stats) ValueFor(stat Stat) float64 {
104 return s.ValueBundle[stat]
105 }
106
107
108
109
110
111
112
113 func (s Stats) DurationFor(stat Stat) time.Duration {
114 return s.DurationBundle[stat]
115 }
116
117
118
119
120 func (s Stats) FloatFor(stat Stat) float64 {
121 switch s.Type {
122 case StatsTypeValue:
123 return s.ValueFor(stat)
124 case StatsTypeDuration:
125 return float64(s.DurationFor(stat))
126 }
127 return 0
128 }
129
130
131
132 func (s Stats) StringFor(stat Stat) string {
133 switch s.Type {
134 case StatsTypeValue:
135 return fmt.Sprintf(s.PrecisionBundle.ValueFormat, s.ValueFor(stat))
136 case StatsTypeDuration:
137 return s.DurationFor(stat).Round(s.PrecisionBundle.Duration).String()
138 }
139 return ""
140 }
141
142 func (s Stats) cells() []table.Cell {
143 out := []table.Cell{}
144 out = append(out, table.C(fmt.Sprintf("%d", s.N)))
145 for _, stat := range []Stat{StatMin, StatMedian, StatMean, StatStdDev, StatMax} {
146 content := s.StringFor(stat)
147 if s.AnnotationBundle[stat] != "" {
148 content += "\n" + s.AnnotationBundle[stat]
149 }
150 out = append(out, table.C(content))
151 }
152 return out
153 }
154
View as plain text