-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprettybenchcmp.go
362 lines (332 loc) · 9.21 KB
/
prettybenchcmp.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io"
"os"
"os/exec"
"sort"
"strconv"
"strings"
"text/tabwriter"
"golang.org/x/tools/benchmark/parse"
)
var (
changedOnly = flag.Bool("changed", false, "show only benchmarks that have changed\n")
magSort = flag.Bool("mag", false, "sort benchmarks by magnitude of change\n")
best = flag.Bool("best", false, "compare best times from old and new\n")
shortFlag = flag.Bool("short", false, `Tell long-running tests to shorten their run time.
It is off by default but set during all.bash so that installing
the Go tree can run a sanity check but not spend time running
exhaustive tests.`+"\n")
benchTimeFlag = flag.String("benchtime", "", `Run enough iterations of each benchmark to take t, specified
as a time.Duration (for example, -benchtime 1h30s).
The default is 1 second (1s).`+"\n")
countFlag = flag.String("count", "", `Run each test and benchmark n times (default 1).
If -cpu is set, run n times for each GOMAXPROCS value.
Examples are always run once.`+"\n")
cpuFlag = flag.String("cpu", "", `Specify a list of GOMAXPROCS values for which the tests or
benchmarks should be executed. The default is the current value
of GOMAXPROCS.`+"\n")
)
// SEPARATOR contain string separator
// i doubt that somebody will use my hometown's name in name of benchmark function
const SEPARATOR = "yoshkarola"
type benchmarkObject struct {
currentHash string
file *os.File
buffer *bufio.ReadWriter
fileSize int64
currentBenchmark *bytes.Buffer
lastBenchmark *bytes.Buffer
isItInitialization bool
fileDoesNotExistInGit bool
wasNotBeforeCommit bool
truncate int64
}
func (b *benchmarkObject) doHistoryExistInGit() {
//http://stackoverflow.com/questions/2405305/git-how-to-tell-if-a-file-is-git-tracked-by-shell-exit-code
cmd := exec.Command("git", "ls-files", ".benchHistory")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
}
if !strings.Contains(out.String(), ".benchHistory") {
b.fileDoesNotExistInGit = true
}
}
func (b *benchmarkObject) initFileSize() {
po, _ := b.file.Stat()
b.fileSize = po.Size()
if b.fileSize == 0 {
b.isItInitialization = true
} else {
b.isItInitialization = false
}
}
func (b *benchmarkObject) fileExist() {
if b.fileDoesNotExistInGit {
if b.isItInitialization {
os.Stdout.Write([]byte("History is inited. Created .benchHistory.\n"))
}
_ = b.file.Truncate(0)
b.getCurrentBenchmark()
b.buffer.Write([]byte(b.currentBenchmark.String()))
b.buffer.Flush()
b.file.Close()
os.Exit(1)
}
}
func (b *benchmarkObject) getLastBenchmark() {
lines := []string{}
tail := []string{}
scan := bufio.NewScanner(b.buffer)
scan.Split(bufio.ScanLines)
for scan.Scan() {
line := scan.Text()
//if you run prettybenchcmp before. It had rewritten tail of benchlog
if b.wasNotBeforeCommit {
tail = append(tail, line)
continue
}
if strings.Contains(line, SEPARATOR) {
if strings.Contains(line, b.currentHash) {
//it's current result. It mean that we have done already benchmarks. Get previous result
b.wasNotBeforeCommit = true
// "\n" from previous result
// second one is for "\n" from end of previous-current result
tail = append(tail, "\n"+"\n"+line)
continue
} else {
// it's older result. just reset array
lines = []string{}
continue
}
}
lines = append(lines, line)
}
if err := scan.Err(); err != nil {
fatal(err.Error())
}
if b.wasNotBeforeCommit {
b.truncate = int64(len(strings.Join(tail, "\n")))
} else {
b.truncate = 0
}
b.lastBenchmark = bytes.NewBufferString(strings.Join(lines, "\n"))
}
func (b *benchmarkObject) getCurrentBenchmark() {
benchTimeValue := ""
if len(*benchTimeFlag) > 0 {
benchTimeValue = "-benchtime=" + *benchTimeFlag
}
countValue := ""
if len(*countFlag) > 0 {
println("Count Flag isn't impemeted rightly for now.")
//countValue = "-count=" + *countFlag
}
cpuValue := ""
if len(*cpuFlag) > 0 {
cpuValue = "-cpu=" + *cpuFlag
}
shortValue := ""
if *shortFlag {
shortValue = "-short"
}
println("go", "test", "-bench=.", "-benchmem", shortValue, benchTimeValue, cpuValue, countValue)
cmd := exec.Command("go", "test", "-bench=.", "-benchmem", shortValue, benchTimeValue, cpuValue, countValue)
var out bytes.Buffer
cmd.Stdout = &out
var stdErr bytes.Buffer
cmd.Stderr = &stdErr
err := cmd.Run()
if err != nil {
println("prettybenchcmpError: Tests was failed")
println(out.String())
b.file.Close()
os.Exit(1)
}
b.currentBenchmark = &out
}
func (b *benchmarkObject) writeBenchmarkToBenchLog() {
b.buffer.Write([]byte("\n" + SEPARATOR + " " + b.currentHash))
b.buffer.Write([]byte("\n\n" + b.currentBenchmark.String()))
b.buffer.Flush()
}
var hash = make(chan string)
func main() {
cmd := exec.Command("git", "status")
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fatal("git isn't exist\n" + fmt.Sprint(err) + ": " + stderr.String())
}
flag.Parse()
go getHash()
file, err := os.OpenFile(".benchHistory", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0777)
defer file.Close()
if err != nil {
fatal(err)
}
benchObject := benchmarkObject{
file: file,
}
benchObject.buffer = NewBufioNewReadWriter(benchObject.file, benchObject.file)
benchObject.initFileSize()
benchObject.doHistoryExistInGit()
benchObject.fileExist()
benchObject.currentHash = <-hash
benchObject.getLastBenchmark()
benchObject.getCurrentBenchmark()
err = file.Truncate(benchObject.fileSize - benchObject.truncate)
if err != nil {
}
benchObject.writeBenchmarkToBenchLog()
after := parseBenchmarkData(benchObject.currentBenchmark)
before := parseBenchmarkData(benchObject.lastBenchmark)
cmps, warnings := Correlate(before, after)
for _, warn := range warnings {
fmt.Fprintln(os.Stderr, warn)
}
if len(cmps) == 0 {
fatal("benchcmp: no repeated benchmarks")
}
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 0, 5, ' ', 0)
defer w.Flush()
var header bool // Has the header has been displayed yet for a given block?
if *magSort {
sort.Sort(ByDeltaNsPerOp(cmps))
} else {
sort.Sort(ByParseOrder(cmps))
}
for _, cmp := range cmps {
if !cmp.Measured(parse.NsPerOp) {
continue
}
if delta := cmp.DeltaNsPerOp(); !*changedOnly || delta.Changed() {
if !header {
fmt.Fprint(w, "benchmark\told ns/op\tnew ns/op\tdelta\n")
header = true
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", cmp.Name(), formatNs(cmp.Before.NsPerOp), formatNs(cmp.After.NsPerOp), delta.Percent())
}
}
header = false
if *magSort {
sort.Sort(ByDeltaMBPerS(cmps))
}
for _, cmp := range cmps {
if !cmp.Measured(parse.MBPerS) {
continue
}
if delta := cmp.DeltaMBPerS(); !*changedOnly || delta.Changed() {
if !header {
fmt.Fprint(w, "\nbenchmark\told MB/s\tnew MB/s\tspeedup\n")
header = true
}
fmt.Fprintf(w, "%s\t%.2f\t%.2f\t%s\n", cmp.Name(), cmp.Before.MBPerS, cmp.After.MBPerS, delta.Multiple())
}
}
header = false
if *magSort {
sort.Sort(ByDeltaAllocsPerOp(cmps))
}
for _, cmp := range cmps {
if !cmp.Measured(parse.AllocsPerOp) {
continue
}
if delta := cmp.DeltaAllocsPerOp(); !*changedOnly || delta.Changed() {
if !header {
fmt.Fprint(w, "\nbenchmark\told allocs\tnew allocs\tdelta\n")
header = true
}
fmt.Fprintf(w, "%s\t%d\t%d\t%s\n", cmp.Name(), cmp.Before.AllocsPerOp, cmp.After.AllocsPerOp, delta.Percent())
}
}
header = false
if *magSort {
sort.Sort(ByDeltaAllocedBytesPerOp(cmps))
}
for _, cmp := range cmps {
if !cmp.Measured(parse.AllocedBytesPerOp) {
continue
}
if delta := cmp.DeltaAllocedBytesPerOp(); !*changedOnly || delta.Changed() {
if !header {
fmt.Fprint(w, "\nbenchmark\told bytes\tnew bytes\tdelta\n")
header = true
}
fmt.Fprintf(w, "%s\t%d\t%d\t%s\n", cmp.Name(), cmp.Before.AllocedBytesPerOp, cmp.After.AllocedBytesPerOp, cmp.DeltaAllocedBytesPerOp().Percent())
}
}
}
func fatal(msg interface{}) {
fmt.Fprintln(os.Stderr, msg)
os.Exit(1)
}
func selectBest(bs parse.Set) {
for name, bb := range bs {
if len(bb) < 2 {
continue
}
ord := bb[0].Ord
best := bb[0]
for _, b := range bb {
if b.NsPerOp < best.NsPerOp {
b.Ord = ord
best = b
}
}
bs[name] = []*parse.Benchmark{best}
}
}
// formatNs formats ns measurements to expose a useful amount of
// precision. It mirrors the ns precision logic of testing.B.
func formatNs(ns float64) string {
prec := 0
switch {
case ns < 10:
prec = 2
case ns < 100:
prec = 1
}
return strconv.FormatFloat(ns, 'f', prec, 64)
}
func parseBenchmarkData(r io.Reader) parse.Set {
bb, err := parse.ParseSet(r)
if err != nil {
fatal(err)
}
if *best {
selectBest(bb)
}
return bb
}
func getHash() {
cmd := exec.Command("git", "log", "-1", "--pretty=tformat:%H", "-p", ".benchHistory")
var out bytes.Buffer
cmd.Stdout = &out
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
println("prettybenchcmpError: Couldn't get commit hash")
println(out.String())
os.Exit(1)
}
str := out.String()
strA := strings.Split(str, "\n")
hash <- strA[0]
close(hash)
}
func NewBufioNewReadWriter(r io.Reader, w io.Writer) *bufio.ReadWriter {
reader := bufio.NewReader(r)
writer := bufio.NewWriter(w)
return bufio.NewReadWriter(reader, writer)
}