| 1 | // Copyright 2014 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package parse provides support for parsing benchmark results as |
| 6 | // generated by 'go test -bench'. |
| 7 | package parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/ // import "golang.org/x/tools/benchmark/parse" |
| 8 | |
| 9 | import ( |
| 10 | "bufioscip-go gomod github.com/golang/go/src go1.25.0 bufio/" |
| 11 | "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 12 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 13 | "ioscip-go gomod github.com/golang/go/src go1.25.0 io/" |
| 14 | "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/" |
| 15 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 16 | ) |
| 17 | |
| 18 | // Flags used by Benchmark.Measured to indicate |
| 19 | // which measurements a Benchmark contains. |
| 20 | const ( |
| 21 | NsPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/NsPerOp. = 1 << iota |
| 22 | MBPerSscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/MBPerS. |
| 23 | AllocedBytesPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/AllocedBytesPerOp. |
| 24 | AllocsPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/AllocsPerOp. |
| 25 | ) |
| 26 | |
| 27 | // Benchmark is one run of a single benchmark. |
| 28 | type Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark# struct { |
| 29 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name. string // benchmark name |
| 30 | Nscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#N. int // number of iterations |
| 31 | NsPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#NsPerOp. float64 // nanoseconds per iteration |
| 32 | AllocedBytesPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#AllocedBytesPerOp. uint64 // bytes allocated per iteration |
| 33 | AllocsPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#AllocsPerOp. uint64 // allocs per iteration |
| 34 | MBPerSscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#MBPerS. float64 // MB processed per second |
| 35 | Measuredscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Measured. int // which measurements were recorded |
| 36 | Ordscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Ord. int // ordinal position within a benchmark run |
| 37 | } |
| 38 | |
| 39 | // ParseLine extracts a Benchmark from a single line of testing.B |
| 40 | // output. |
| 41 | func ParseLinescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/ParseLine().(line string) (*Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#, error) { |
| 42 | fields := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Fieldsscip-go gomod github.com/golang/go/src go1.25.0 strings/Fields().(line) |
| 43 | |
| 44 | // Two required, positional fields: Name and iterations. |
| 45 | if len(fields) < 2 { |
| 46 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("two fields required, have %d", len(fields)) |
| 47 | } |
| 48 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(fields[0], "Benchmark") { |
| 49 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().(`first field does not start with "Benchmark"`) |
| 50 | } |
| 51 | n, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.0 strconv/Atoi().(fields[1]) |
| 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | b := &Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#{Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: fields[0], Nscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#N.: n} |
| 56 | |
| 57 | // Parse any remaining pairs of fields; we've parsed one pair already. |
| 58 | for i := 1; i < len(fields)/2; i++ { |
| 59 | b.parseMeasurementscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#parseMeasurement().(fields[i*2], fields[i*2+1]) |
| 60 | } |
| 61 | return b, nil |
| 62 | } |
| 63 | |
| 64 | func (b *Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#) parseMeasurementscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#parseMeasurement().(quant string, unit string) { |
| 65 | switch unit { |
| 66 | case "ns/op": |
| 67 | if f, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.ParseFloatscip-go gomod github.com/golang/go/src go1.25.0 strconv/ParseFloat().(quant, 64); err == nil { |
| 68 | b.NsPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#NsPerOp. = f |
| 69 | b.Measuredscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Measured. |= NsPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/NsPerOp. |
| 70 | } |
| 71 | case "MB/s": |
| 72 | if f, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.ParseFloatscip-go gomod github.com/golang/go/src go1.25.0 strconv/ParseFloat().(quant, 64); err == nil { |
| 73 | b.MBPerSscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#MBPerS. = f |
| 74 | b.Measuredscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Measured. |= MBPerSscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/MBPerS. |
| 75 | } |
| 76 | case "B/op": |
| 77 | if i, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.ParseUintscip-go gomod github.com/golang/go/src go1.25.0 strconv/ParseUint().(quant, 10, 64); err == nil { |
| 78 | b.AllocedBytesPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#AllocedBytesPerOp. = i |
| 79 | b.Measuredscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Measured. |= AllocedBytesPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/AllocedBytesPerOp. |
| 80 | } |
| 81 | case "allocs/op": |
| 82 | if i, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.ParseUintscip-go gomod github.com/golang/go/src go1.25.0 strconv/ParseUint().(quant, 10, 64); err == nil { |
| 83 | b.AllocsPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#AllocsPerOp. = i |
| 84 | b.Measuredscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Measured. |= AllocsPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/AllocsPerOp. |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func (b *Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#) Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#String().() string { |
| 90 | buf := new(bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#) |
| 91 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(buf, "%s %d", b.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name., b.Nscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#N.) |
| 92 | if (b.Measuredscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Measured. & NsPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/NsPerOp.) != 0 { |
| 93 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(buf, " %.2f ns/op", b.NsPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#NsPerOp.) |
| 94 | } |
| 95 | if (b.Measuredscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Measured. & MBPerSscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/MBPerS.) != 0 { |
| 96 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(buf, " %.2f MB/s", b.MBPerSscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#MBPerS.) |
| 97 | } |
| 98 | if (b.Measuredscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Measured. & AllocedBytesPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/AllocedBytesPerOp.) != 0 { |
| 99 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(buf, " %d B/op", b.AllocedBytesPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#AllocedBytesPerOp.) |
| 100 | } |
| 101 | if (b.Measuredscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Measured. & AllocsPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/AllocsPerOp.) != 0 { |
| 102 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(buf, " %d allocs/op", b.AllocsPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#AllocsPerOp.) |
| 103 | } |
| 104 | return buf.Stringscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#String().() |
| 105 | } |
| 106 | |
| 107 | // Set is a collection of benchmarks from one |
| 108 | // testing.B run, keyed by name to facilitate comparison. |
| 109 | type Setscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Set# map[string][]*Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark# |
| 110 | |
| 111 | // ParseSet extracts a Set from testing.B output. |
| 112 | // ParseSet preserves the order of benchmarks that have identical |
| 113 | // names. |
| 114 | func ParseSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/ParseSet().(r ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Readerscip-go gomod github.com/golang/go/src go1.25.0 io/Reader#) (Setscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Set#, error) { |
| 115 | bb := make(Setscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Set#) |
| 116 | scan := bufioscip-go gomod github.com/golang/go/src go1.25.0 bufio/.NewScannerscip-go gomod github.com/golang/go/src go1.25.0 bufio/NewScanner().(r) |
| 117 | ord := 0 |
| 118 | for scan.Scanscip-go gomod github.com/golang/go/src go1.25.0 bufio/Scanner#Scan().() { |
| 119 | if b, err := ParseLinescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/ParseLine().(scan.Textscip-go gomod github.com/golang/go/src go1.25.0 bufio/Scanner#Text().()); err == nil { |
| 120 | b.Ordscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Ord. = ord |
| 121 | ord++ |
| 122 | bb[b.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.] = append(bb[b.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.], b) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if err := scan.Errscip-go gomod github.com/golang/go/src go1.25.0 bufio/Scanner#Err().(); err != nil { |
| 127 | return nil, err |
| 128 | } |
| 129 | |
| 130 | return bb, nil |
| 131 | } |