/
benchmark.go
64 lines (56 loc) 路 1.65 KB
/
benchmark.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
package bench
// RunHistory is a sort.Interface that sorts the most recent run first
type RunHistory []Run
// Runs returns the underlyinng runs
func (r RunHistory) Runs() []Run { return r }
func (r RunHistory) Len() int { return len(r) }
func (r RunHistory) Less(i, j int) bool { return r[i].Date > r[j].Date }
func (r RunHistory) Swap(i, j int) {
tmp := r[i]
r[i] = r[j]
r[j] = tmp
}
// Run denotes one run of gobenchdata, useful for grouping benchmark records
type Run struct {
Version string `json:",omitempty"`
Date int64
Tags []string `json:",omitempty"`
Suites []Suite
}
// FindBenchmark returns benchmark by package and bench name
func (r *Run) FindBenchmark(pkg, bench string) (*Benchmark, bool) {
for _, s := range r.Suites {
if s.Pkg == pkg {
for _, b := range s.Benchmarks {
if b.Name == bench {
return &b, true
}
}
}
}
return nil, false
}
// Suite is a suite of benchmark runs
type Suite struct {
Goos string
Goarch string
Pkg string
Benchmarks []Benchmark
}
// Benchmark is an individual run. Note that all metrics in here must be represented as
// a float type, even if Go only emits integer values, so that in checks we can correctly
// evaluate divisions so that results come out as floats instead of being truncated to
// integers.
type Benchmark struct {
Name string
Runs int
NsPerOp float64
Mem Mem // from '-benchmem'
Custom map[string]float64 `json:",omitempty"` // https://tip.golang.org/pkg/testing/#B.ReportMetric
}
// Mem is memory allocation information about a run
type Mem struct {
BytesPerOp float64
AllocsPerOp float64
MBPerSec float64
}