-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
292 lines (256 loc) · 6.09 KB
/
main.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package kagenui
import (
"fmt"
"io"
"math"
"sort"
"strings"
"time"
)
var (
mp *Kagenui
enabled bool
condition func() bool
)
type KagenuiData struct {
description string
steps map[string]int64
lastStep time.Time
memos []string
}
type Kagenui struct {
profiles []*KagenuiData
}
func init() {
mp = new(Kagenui)
mp.profiles = make([]*KagenuiData, 0)
enabled = true
condition = func() bool { return true }
}
func Enable() {
enabled = true
}
func Disable() {
enabled = false
}
func SetCondition(c func() bool) {
condition = c
}
func Begin(description string) *KagenuiData {
if !enabled {
return nil
}
if !condition() {
return nil
}
return &KagenuiData{description, make(map[string]int64, 0), time.Now(), make([]string, 0)}
}
func Dump(writer io.Writer) {
for _, prof := range mp.profiles {
outputs := []string{"log:MP"}
for tag, val := range prof.steps {
outputs = append(outputs, fmt.Sprintf("%s:%d", tag, val))
}
outputs = append(outputs, fmt.Sprintf("description:%s", prof.description))
memoOutput := []string{}
for _, m := range prof.memos {
memoOutput = append(memoOutput, m)
}
outputs = append(outputs, fmt.Sprintf("memo:%s", strings.Join(memoOutput, ",")))
fmt.Fprintln(writer, strings.Join(outputs, "\t"))
}
}
func Flush() {
mp.profiles = make([]*KagenuiData, 0)
}
type Measure struct {
Description string
Count int
Total int
Mean int
Stddev float64
Min int
Max int
}
type By func(a, b *Measure) bool
func (by By) Sort(measures []*Measure) {
ms := &measureSorter{
measures: measures,
by: by,
}
sort.Sort(ms)
}
type measureSorter struct {
measures []*Measure
by func(a, b *Measure) bool
}
func (s *measureSorter) Len() int {
return len(s.measures)
}
func (s *measureSorter) Swap(i, j int) {
s.measures[i], s.measures[j] = s.measures[j], s.measures[i]
}
func (s *measureSorter) Less(i, j int) bool {
return s.by(s.measures[i], s.measures[j])
}
type Column struct {
Name string
Summary string
Sort By
}
var (
columns = []*Column{
&Column{Name: "Count", Summary: "Count", Sort: func(a, b *Measure) bool { return a.Count > b.Count }},
&Column{Name: "Total", Summary: "Total", Sort: func(a, b *Measure) bool { return a.Total > b.Total }},
&Column{Name: "Mean", Summary: "Mean", Sort: func(a, b *Measure) bool { return a.Mean > b.Mean }},
&Column{Name: "Stddev", Summary: "Standard Deviation", Sort: func(a, b *Measure) bool { return a.Stddev > b.Stddev }},
&Column{Name: "Min"},
&Column{Name: "Max", Summary: "Maximum(100 Percentile)", Sort: func(a, b *Measure) bool { return a.Max > b.Max }},
}
)
func getIntegerDigitWidth(i int) int {
var w int
switch {
case i < 1:
w = 1
default:
w = int(math.Log10(float64(i)) + 1)
}
return w
}
func showMeasures(writer io.Writer, measures []*Measure) {
countWidth := 5
totalWidth := 5
meanWidth := 4
stddevWidth := 6
maxWidth := 3
for _, m := range measures {
var w int
w = getIntegerDigitWidth(m.Count)
if countWidth < w {
countWidth = w
}
w = getIntegerDigitWidth(m.Total)
if totalWidth < w {
totalWidth = w
}
w = getIntegerDigitWidth(int(m.Mean))
if meanWidth < w {
meanWidth = w
}
w = getIntegerDigitWidth(int(m.Stddev)) + 4
if stddevWidth < w {
stddevWidth = w
}
w = getIntegerDigitWidth(m.Max)
if maxWidth < w {
maxWidth = w
}
}
var format string
for _, column := range columns {
switch column.Name {
case "Count":
fmt.Fprintf(writer, fmt.Sprintf("%%%ds ", countWidth), column.Name)
format += fmt.Sprintf("%%%dd ", countWidth)
case "Total":
fmt.Fprintf(writer, fmt.Sprintf("%%%ds ", totalWidth), column.Name)
format += fmt.Sprintf("%%%dd ", totalWidth)
case "Mean":
fmt.Fprintf(writer, fmt.Sprintf("%%%ds ", meanWidth), column.Name)
format += fmt.Sprintf("%%%dd ", meanWidth)
case "Stddev":
fmt.Fprintf(writer, fmt.Sprintf("%%%ds ", stddevWidth), column.Name)
format += fmt.Sprintf("%%%d.3f ", stddevWidth)
default:
fmt.Fprintf(writer, fmt.Sprintf("%%%ds ", maxWidth), column.Name)
format += fmt.Sprintf("%%%dd ", maxWidth)
}
}
fmt.Fprintln(writer, "Description")
format += "%s\n"
for _, m := range measures {
fmt.Fprintf(writer, format, m.Count, m.Total, m.Mean, m.Stddev, m.Min, m.Max, m.Description)
}
}
func Analyze(writer io.Writer) {
var times = make(map[string][]int)
var totals = make(map[string]int)
for _, prof := range mp.profiles {
for tag, val := range prof.steps {
description := fmt.Sprintf("%s/%s", prof.description, tag)
times[description] = append(times[description], int(val))
totals[description] += int(val)
}
}
var measures []*Measure
for description, times := range times {
sort.Ints(times)
count := len(times)
total := totals[description]
m := &Measure{
Description: description,
Count: count,
Total: total,
Mean: total / count,
Min: times[0],
Max: times[count-1],
}
var sum int
for _, t := range times {
sum += (t - m.Mean) * (t - m.Mean)
}
m.Stddev = math.Sqrt(float64(sum) / float64(count))
measures = append(measures, m)
}
for _, column := range columns {
if column.Sort != nil {
fmt.Fprintf(writer, "Sort by %s\n", column.Summary)
By(column.Sort).Sort(measures)
showMeasures(writer, measures)
fmt.Fprintln(writer)
}
}
}
func (mpd *KagenuiData) Step(tag string) {
if !enabled {
return
}
if mpd == nil {
return
}
now := time.Now()
thisstep := now.Sub(mpd.lastStep).Nanoseconds()
mpd.steps[tag] = thisstep
mpd.lastStep = now
}
func (mpd *KagenuiData) Memo(memo string) {
if !enabled {
return
}
if mpd == nil {
return
}
memo = memoFilter(memo)
mpd.memos = append(mpd.memos, memo)
}
/*
remove (,|\t|\r|\n)
*/
func memoFilter(memo string) string {
memo = strings.Replace(memo, ",", "", -1)
memo = strings.Replace(memo, "\t", "", -1)
memo = strings.Replace(memo, "\r", "", -1)
memo = strings.Replace(memo, "\n", "", -1)
return memo
}
func (mpd *KagenuiData) End() {
if !enabled {
return
}
if mpd == nil {
return
}
mpd.Step("Last Step to End")
mp.profiles = append(mp.profiles, mpd)
}