/
T.go
312 lines (263 loc) · 6.25 KB
/
T.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package benchmark
import (
"bytes"
"container/list"
"context"
"flag"
"fmt"
"io/ioutil"
"os"
"runtime"
"runtime/pprof"
"time"
ppf "github.com/sudachen/benchmark/ppftool"
"github.com/sudachen/misc"
)
var flagNoGC = flag.Bool("nogc", false, "disable GC on benchmark")
var flagPprof = flag.Bool("pprof", false, "profile benchmarks")
var flagMprof = flag.Bool("mprof", false, "profile memory allocations")
var flagCpuProf = flag.String("cpuprof", misc.NulStr, "where to store cpuprofile")
var flagMemProf = flag.String("memprof", misc.NulStr, "where to store memprofile")
var flagCallGrapth = flag.Int("callgraph", 0, "count of nodes to write PNG callgraph")
var flagNodeCount = flag.Int("nodecount", 20, "count of nodes to gather top calls")
var flagResult = flag.String("result", misc.NulStr, "file name to write the benchmarking result")
type messageKind byte
const (
MsgError messageKind = iota
MsgInfo
MsgDebug
MsgOpt
)
func (mk messageKind) String() string {
switch mk {
case MsgError:
return "MsgError"
case MsgInfo:
return "MsgInfo"
case MsgDebug:
return "MsgDebug"
case MsgOpt:
return "MsgOpt"
}
return ""
}
type Message struct {
Kind messageKind
Text string
}
type T struct {
enableGC, isStarted, stopProfiler bool
processor func(t *T, finished *T) *T
startedAt, runOn time.Time
chActive, chPaused time.Duration
Err error
Label string
Count int
Active, Total time.Duration
Children, Messages *list.List
Heap *ppf.Report
}
type Benchmark struct {
*T
Pprof *list.List
}
func New(label string) *T {
t := &T{
Label: label,
Children: list.New(),
Messages: list.New(),
}
return t
}
var heapbf bytes.Buffer
/*func (t *T) profileHeap() {
if *flagMprof {
runtime.GC()
pprof.SetGoroutineLabels(pprof.WithLabels(context.Background(), pprof.Labels("_", "heap")))
heapbf.Reset()
pprof.WriteHeapProfile(&heapbf)
opt := &ppf.Options{
Unit: ppf.Second,
Count: *flagNodeCount,
TagHide: []string{"_:"},
Hide: []string{"google/pprof\\.:"},
}
if rpt, err := ppf.Top(heapbf.Bytes(), opt); err == nil {
rpt.Label = "heap"
t.Heap = rpt
}
pprof.SetGoroutineLabels(context.Background())
}
}*/
func (t *T) run(f func(*T) error) (err error) {
if t.startedAt != (time.Time{}) {
panic("start is allowed only in leaf tasks")
} else {
pprof.SetGoroutineLabels(pprof.WithLabels(context.Background(), pprof.Labels("_", "prepare")))
}
t.runOn = time.Now()
t.Err = f(t)
t.Total = time.Since(t.runOn)
if t.Children.Len() != 0 {
t.Active = t.chActive
} else {
if t.isStarted {
t.Active = time.Since(t.startedAt)
if t.enableGC {
enableGC()
}
//t.profileHeap()
}
}
return
}
const PprofBufferReserve = 1024 * 1024
func (t *Benchmark) pprofRun(f func(*T) error) {
var cpubuf bytes.Buffer
var membuf bytes.Buffer
if *flagPprof || *flagCpuProf != misc.NulStr {
cpubuf.Grow(PprofBufferReserve)
//runtime.SetCPUProfileRate(10000)
pprof.StartCPUProfile(&cpubuf)
}
t.run(f)
if *flagPprof || *flagCpuProf != misc.NulStr {
pprof.StopCPUProfile()
}
if *flagMemProf != misc.NulStr || *flagMprof {
runtime.GC()
pprof.WriteHeapProfile(&membuf)
if *flagMemProf != misc.NulStr {
if f, err := os.Create(*flagMemProf); err == nil {
f.Write(membuf.Bytes())
} else {
fmt.Fprintln(os.Stderr, err)
}
}
}
if *flagCpuProf != misc.NulStr {
if err := ioutil.WriteFile(*flagCpuProf, membuf.Bytes(), 0644); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
if *flagPprof || *flagMprof {
t.Pprof = list.New()
count := *flagNodeCount
pngcount := *flagCallGrapth
opt := &ppf.Options{
Count: count,
Graph: ppf.DOT,
Gcount: pngcount,
NoLegend: true,
TagHide: []string{"_:"},
Hide: []string{"google/pprof\\."},
}
if *flagPprof {
opt.Unit = ppf.Second
opt.Index = ppf.CpuProfIndex
rpt, _ := ppf.Top(cpubuf.Bytes(), opt)
rpt.Label = "top"
t.Pprof.PushBack(rpt)
}
if *flagMprof {
opt.Unit = ppf.None
opt.Index = ppf.AllocObjectsIndex
rpt, _ := ppf.Top(membuf.Bytes(), opt)
rpt.Label = "alloc"
t.Pprof.PushBack(rpt)
}
}
}
func Run(label string, f func(*T) error) *Benchmark {
if !flag.Parsed() {
flag.Parse()
}
runtime.LockOSThread()
b := &Benchmark{T: New(label)}
b.pprofRun(f)
return b
}
func RunWithProcessor(label string, processor func(*T, *T) *T, f func(*T) error) *Benchmark {
if !flag.Parsed() {
flag.Parse()
}
runtime.LockOSThread()
b := &Benchmark{T: New(label)}
b.processor = processor
b.pprofRun(f)
if b.processor != nil {
b.processor(nil, b.T)
}
return b
}
func (t *T) Run(label string, f func(*T) error) (err error) {
t0 := New(label)
t0.processor = t.processor
if *flagPprof || *flagCpuProf != misc.NulStr {
defer pprof.SetGoroutineLabels(context.Background())
}
t0.run(f)
if t.processor != nil {
t0 = t.processor(t, t0)
}
if t0 != nil {
t.Children.PushBack(t0)
t.chActive += t0.Active
}
return t0.Err
}
func (t *T) Start() {
if t.Children.Len() != 0 {
panic("start is allowed only in leaf tasks")
}
if !t.isStarted {
runtime.GC()
if *flagNoGC {
t.enableGC = true
disableGC()
}
t.isStarted = true
t.startedAt = time.Now()
pprof.SetGoroutineLabels(pprof.WithLabels(context.Background(), pprof.Labels("t", "active")))
}
t.Count++
}
func (t *T) Errorf(ft string, a ...interface{}) {
m := &Message{MsgError, fmt.Sprintf(ft, a...)}
t.Messages.PushBack(m)
}
func (t *T) Error(a ...interface{}) {
m := &Message{MsgError, fmt.Sprint(a...)}
t.Messages.PushBack(m)
}
func (t *T) Debugf(ft string, a ...interface{}) {
m := &Message{MsgDebug, fmt.Sprintf(ft, a...)}
t.Messages.PushBack(m)
}
func (t *T) Debug(a ...interface{}) {
m := &Message{MsgDebug, fmt.Sprint(a...)}
t.Messages.PushBack(m)
}
func (t *T) Infof(ft string, a ...interface{}) {
m := &Message{MsgInfo, fmt.Sprintf(ft, a...)}
t.Messages.PushBack(m)
}
func (t *T) Info(a ...interface{}) {
m := &Message{MsgInfo, fmt.Sprint(a...)}
t.Messages.PushBack(m)
}
func (t *T) Opt(a string) {
m := &Message{MsgOpt, a}
t.Messages.PushBack(m)
}
func (b *Benchmark) WriteJsonResult() (int, error) {
if *flagResult != misc.NulStr {
if f, err := os.Create(*flagResult); err != nil {
return 0, err
} else {
return b.WriteJson(f)
}
} else {
return b.WriteJson(os.Stderr)
}
}