Commit 62a90583 authored by Quentin Smith's avatar Quentin Smith

storage/benchfmt: process header lines

Change-Id: I11df054853be604fcabec6f20da7f34c970d7bfa
Reviewed-on: https://go-review.googlesource.com/35154Reviewed-by: default avatarRuss Cox <rsc@golang.org>
parent e1f4cdb4
......@@ -19,9 +19,12 @@ import (
// Reader reads benchmark results from an io.Reader.
type Reader struct {
s *bufio.Scanner
labels Labels
lineNum int
s *bufio.Scanner
labels Labels
// permLabels are permanent labels read from the start of the
// file or provided by AddLabels. They cannot be overridden.
permLabels Labels
lineNum int
}
// TODO(quentin): Make Reader have a Scanner-style interface instead, to match db.Query.
......@@ -34,9 +37,10 @@ func NewReader(r io.Reader) *Reader {
}
}
// AddLabels adds additional labels as if they had been read from the file.
// AddLabels adds additional labels as if they had been read from the header of a file.
// It must be called before the first call to r.Next.
func (r *Reader) AddLabels(labels Labels) {
r.permLabels = labels.copy()
for k, v := range labels {
r.labels[k] = v
}
......@@ -173,10 +177,14 @@ func (l Labels) copy() Labels {
// no further results, it returns nil, io.EOF.
func (r *Reader) Next() (*Result, error) {
copied := false
havePerm := r.permLabels != nil
for r.s.Scan() {
r.lineNum++
line := r.s.Text()
if key, value, ok := parseKeyValueLine(line); ok {
if _, ok := r.permLabels[key]; ok {
continue
}
if !copied {
copied = true
r.labels = r.labels.copy()
......@@ -191,6 +199,14 @@ func (r *Reader) Next() (*Result, error) {
}
continue
}
// Blank line delimits the header. If we find anything else, the file must not have a header.
if !havePerm {
if line == "" {
r.permLabels = r.labels.copy()
} else {
r.permLabels = Labels{}
}
}
if fullName, ok := parseBenchmarkLine(line); ok {
return newResult(r.labels, r.lineNum, fullName, line), nil
}
......@@ -217,6 +233,9 @@ func parseKeyValueLine(line string) (key, val string, ok bool) {
break
}
}
if key == "" {
return
}
if val == "" {
ok = true
return
......
......@@ -84,6 +84,22 @@ BenchmarkOne 1 ns/sec
},
},
},
{
"parse file headers",
`key: fixed
key: haha
BenchmarkOne 1 ns/sec
`,
[]*Result{
{
Labels{"key": "fixed"},
Labels{"name": "One"},
4,
"BenchmarkOne 1 ns/sec",
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment