2017-04-19 05:43:09 -07:00
|
|
|
// Copyright 2017 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-01-14 07:39:04 -08:00
|
|
|
package textparse
|
|
|
|
|
|
|
|
import (
|
2017-01-15 04:55:53 -08:00
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
|
|
|
"io"
|
2017-01-14 07:39:04 -08:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2017-01-15 04:55:53 -08:00
|
|
|
"github.com/prometheus/common/expfmt"
|
|
|
|
"github.com/prometheus/common/model"
|
2017-01-14 07:39:04 -08:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-10-04 06:52:03 -07:00
|
|
|
func TestPromParse(t *testing.T) {
|
2017-01-14 10:30:19 -08:00
|
|
|
input := `# HELP go_gc_duration_seconds A summary of the GC invocation durations.
|
2018-05-14 13:19:53 -07:00
|
|
|
# TYPE go_gc_duration_seconds summary
|
2017-01-14 10:30:19 -08:00
|
|
|
go_gc_duration_seconds{quantile="0"} 4.9351e-05
|
2017-05-22 02:15:40 -07:00
|
|
|
go_gc_duration_seconds{quantile="0.25",} 7.424100000000001e-05
|
2017-01-14 10:30:19 -08:00
|
|
|
go_gc_duration_seconds{quantile="0.5",a="b"} 8.3835e-05
|
2017-06-22 00:38:55 -07:00
|
|
|
go_gc_duration_seconds{quantile="0.8", a="b"} 8.3835e-05
|
|
|
|
go_gc_duration_seconds{ quantile="0.9", a="b"} 8.3835e-05
|
2018-05-14 13:19:53 -07:00
|
|
|
# Hrandom comment starting with prefix of HELP
|
|
|
|
#
|
|
|
|
# comment with escaped \n newline
|
|
|
|
# comment with escaped \ escape character
|
2018-08-01 05:29:58 -07:00
|
|
|
# HELP nohelp1
|
|
|
|
# HELP nohelp2
|
2017-06-22 00:38:55 -07:00
|
|
|
go_gc_duration_seconds{ quantile="1.0", a="b" } 8.3835e-05
|
|
|
|
go_gc_duration_seconds { quantile="1.0", a="b" } 8.3835e-05
|
2018-05-14 13:19:53 -07:00
|
|
|
go_gc_duration_seconds { quantile= "1.0", a= "b", } 8.3835e-05
|
2017-06-22 00:38:55 -07:00
|
|
|
go_gc_duration_seconds { quantile = "1.0", a = "b" } 8.3835e-05
|
2017-01-14 10:30:19 -08:00
|
|
|
go_gc_duration_seconds_count 99
|
2017-05-09 03:21:19 -07:00
|
|
|
some:aggregate:rate5m{a_b="c"} 1
|
2017-01-14 10:30:19 -08:00
|
|
|
# HELP go_goroutines Number of goroutines that currently exist.
|
|
|
|
# TYPE go_goroutines gauge
|
2017-07-18 03:35:41 -07:00
|
|
|
go_goroutines 33 123123
|
|
|
|
_metric_starting_with_underscore 1
|
2017-07-27 06:15:41 -07:00
|
|
|
testmetric{_label_starting_with_underscore="foo"} 1
|
|
|
|
testmetric{label="\"bar\""} 1`
|
2018-06-01 01:22:32 -07:00
|
|
|
input += "\n# HELP metric foo\x00bar"
|
2017-05-24 05:52:46 -07:00
|
|
|
input += "\nnull_byte_metric{a=\"abc\x00\"} 1"
|
2017-04-27 08:02:07 -07:00
|
|
|
|
|
|
|
int64p := func(x int64) *int64 { return &x }
|
2017-01-14 10:30:19 -08:00
|
|
|
|
|
|
|
exp := []struct {
|
2018-05-14 13:19:53 -07:00
|
|
|
lset labels.Labels
|
|
|
|
m string
|
|
|
|
t *int64
|
|
|
|
v float64
|
|
|
|
typ MetricType
|
|
|
|
help string
|
|
|
|
comment string
|
2017-01-14 10:30:19 -08:00
|
|
|
}{
|
|
|
|
{
|
2018-05-14 13:19:53 -07:00
|
|
|
m: "go_gc_duration_seconds",
|
|
|
|
help: "A summary of the GC invocation durations.",
|
|
|
|
}, {
|
|
|
|
m: "go_gc_duration_seconds",
|
|
|
|
typ: MetricTypeSummary,
|
|
|
|
}, {
|
2017-01-14 10:30:19 -08:00
|
|
|
m: `go_gc_duration_seconds{quantile="0"}`,
|
|
|
|
v: 4.9351e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go_gc_duration_seconds", "quantile", "0"),
|
|
|
|
}, {
|
2017-05-22 02:15:40 -07:00
|
|
|
m: `go_gc_duration_seconds{quantile="0.25",}`,
|
2017-01-14 10:30:19 -08:00
|
|
|
v: 7.424100000000001e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go_gc_duration_seconds", "quantile", "0.25"),
|
|
|
|
}, {
|
|
|
|
m: `go_gc_duration_seconds{quantile="0.5",a="b"}`,
|
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go_gc_duration_seconds", "quantile", "0.5", "a", "b"),
|
2017-06-22 00:38:55 -07:00
|
|
|
}, {
|
|
|
|
m: `go_gc_duration_seconds{quantile="0.8", a="b"}`,
|
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go_gc_duration_seconds", "quantile", "0.8", "a", "b"),
|
|
|
|
}, {
|
|
|
|
m: `go_gc_duration_seconds{ quantile="0.9", a="b"}`,
|
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go_gc_duration_seconds", "quantile", "0.9", "a", "b"),
|
2018-05-14 13:19:53 -07:00
|
|
|
}, {
|
|
|
|
comment: "# Hrandom comment starting with prefix of HELP",
|
|
|
|
}, {
|
|
|
|
comment: "#",
|
|
|
|
}, {
|
|
|
|
comment: "# comment with escaped \\n newline",
|
|
|
|
}, {
|
|
|
|
comment: "# comment with escaped \\ escape character",
|
2018-08-01 05:29:58 -07:00
|
|
|
}, {
|
|
|
|
m: "nohelp1",
|
|
|
|
help: "",
|
|
|
|
}, {
|
|
|
|
m: "nohelp2",
|
|
|
|
help: "",
|
2017-06-22 00:38:55 -07:00
|
|
|
}, {
|
|
|
|
m: `go_gc_duration_seconds{ quantile="1.0", a="b" }`,
|
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go_gc_duration_seconds", "quantile", "1.0", "a", "b"),
|
|
|
|
}, {
|
|
|
|
m: `go_gc_duration_seconds { quantile="1.0", a="b" }`,
|
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go_gc_duration_seconds", "quantile", "1.0", "a", "b"),
|
|
|
|
}, {
|
2018-05-14 13:19:53 -07:00
|
|
|
m: `go_gc_duration_seconds { quantile= "1.0", a= "b", }`,
|
2017-06-22 00:38:55 -07:00
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go_gc_duration_seconds", "quantile", "1.0", "a", "b"),
|
|
|
|
}, {
|
|
|
|
m: `go_gc_duration_seconds { quantile = "1.0", a = "b" }`,
|
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go_gc_duration_seconds", "quantile", "1.0", "a", "b"),
|
2017-01-14 10:30:19 -08:00
|
|
|
}, {
|
|
|
|
m: `go_gc_duration_seconds_count`,
|
|
|
|
v: 99,
|
|
|
|
lset: labels.FromStrings("__name__", "go_gc_duration_seconds_count"),
|
2017-05-09 03:21:19 -07:00
|
|
|
}, {
|
|
|
|
m: `some:aggregate:rate5m{a_b="c"}`,
|
|
|
|
v: 1,
|
|
|
|
lset: labels.FromStrings("__name__", "some:aggregate:rate5m", "a_b", "c"),
|
2018-05-14 13:19:53 -07:00
|
|
|
}, {
|
|
|
|
m: "go_goroutines",
|
|
|
|
help: "Number of goroutines that currently exist.",
|
|
|
|
}, {
|
|
|
|
m: "go_goroutines",
|
|
|
|
typ: MetricTypeGauge,
|
2017-01-14 10:30:19 -08:00
|
|
|
}, {
|
|
|
|
m: `go_goroutines`,
|
|
|
|
v: 33,
|
2017-04-27 08:02:07 -07:00
|
|
|
t: int64p(123123),
|
2017-01-14 10:30:19 -08:00
|
|
|
lset: labels.FromStrings("__name__", "go_goroutines"),
|
2017-07-18 03:35:41 -07:00
|
|
|
}, {
|
|
|
|
m: "_metric_starting_with_underscore",
|
|
|
|
v: 1,
|
|
|
|
lset: labels.FromStrings("__name__", "_metric_starting_with_underscore"),
|
|
|
|
}, {
|
|
|
|
m: "testmetric{_label_starting_with_underscore=\"foo\"}",
|
|
|
|
v: 1,
|
|
|
|
lset: labels.FromStrings("__name__", "testmetric", "_label_starting_with_underscore", "foo"),
|
2017-07-27 06:15:41 -07:00
|
|
|
}, {
|
|
|
|
m: "testmetric{label=\"\\\"bar\\\"\"}",
|
|
|
|
v: 1,
|
|
|
|
lset: labels.FromStrings("__name__", "testmetric", "label", `"bar"`),
|
2018-06-01 01:22:32 -07:00
|
|
|
}, {
|
|
|
|
m: "metric",
|
|
|
|
help: "foo\x00bar",
|
2017-05-24 05:52:46 -07:00
|
|
|
}, {
|
|
|
|
m: "null_byte_metric{a=\"abc\x00\"}",
|
|
|
|
v: 1,
|
|
|
|
lset: labels.FromStrings("__name__", "null_byte_metric", "a", "abc\x00"),
|
2017-01-14 10:30:19 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-10-04 06:52:03 -07:00
|
|
|
p := NewPromParser([]byte(input))
|
2017-01-14 10:30:19 -08:00
|
|
|
i := 0
|
|
|
|
|
|
|
|
var res labels.Labels
|
|
|
|
|
2018-05-14 13:19:53 -07:00
|
|
|
for {
|
|
|
|
et, err := p.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
switch et {
|
|
|
|
case EntrySeries:
|
|
|
|
m, ts, v := p.Series()
|
2017-01-14 10:30:19 -08:00
|
|
|
|
2018-05-14 13:19:53 -07:00
|
|
|
p.Metric(&res)
|
2017-01-14 10:30:19 -08:00
|
|
|
|
2018-05-14 13:19:53 -07:00
|
|
|
require.Equal(t, exp[i].m, string(m))
|
|
|
|
require.Equal(t, exp[i].t, ts)
|
|
|
|
require.Equal(t, exp[i].v, v)
|
|
|
|
require.Equal(t, exp[i].lset, res)
|
|
|
|
res = res[:0]
|
|
|
|
|
|
|
|
case EntryType:
|
|
|
|
m, typ := p.Type()
|
|
|
|
require.Equal(t, exp[i].m, string(m))
|
|
|
|
require.Equal(t, exp[i].typ, typ)
|
|
|
|
|
|
|
|
case EntryHelp:
|
|
|
|
m, h := p.Help()
|
|
|
|
require.Equal(t, exp[i].m, string(m))
|
|
|
|
require.Equal(t, exp[i].help, string(h))
|
|
|
|
|
|
|
|
case EntryComment:
|
|
|
|
require.Equal(t, exp[i].comment, string(p.Comment()))
|
|
|
|
}
|
2017-01-14 10:30:19 -08:00
|
|
|
|
|
|
|
i++
|
|
|
|
}
|
2017-04-27 08:02:07 -07:00
|
|
|
require.Equal(t, len(exp), i)
|
2017-01-14 10:30:19 -08:00
|
|
|
}
|
|
|
|
|
2018-10-04 06:52:03 -07:00
|
|
|
func TestPromParseErrors(t *testing.T) {
|
2017-06-16 05:09:50 -07:00
|
|
|
cases := []struct {
|
|
|
|
input string
|
|
|
|
err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: "a",
|
2018-05-14 13:19:53 -07:00
|
|
|
err: "expected value after metric, got \"MNAME\"",
|
2017-06-16 05:09:50 -07:00
|
|
|
},
|
2017-06-19 08:46:50 -07:00
|
|
|
{
|
|
|
|
input: "a{b='c'} 1\n",
|
2018-05-14 13:19:53 -07:00
|
|
|
err: "expected label value, got \"INVALID\"",
|
2017-06-19 08:46:50 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a{b=\n",
|
2018-05-14 13:19:53 -07:00
|
|
|
err: "expected label value, got \"INVALID\"",
|
2017-06-19 08:46:50 -07:00
|
|
|
},
|
2017-06-16 05:09:50 -07:00
|
|
|
{
|
|
|
|
input: "a{\xff=\"foo\"} 1\n",
|
2018-05-14 13:19:53 -07:00
|
|
|
err: "expected label name, got \"INVALID\"",
|
2017-06-16 05:09:50 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a{b=\"\xff\"} 1\n",
|
2017-10-23 06:57:30 -07:00
|
|
|
err: "invalid UTF-8 label value",
|
2017-06-16 05:09:50 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a true\n",
|
|
|
|
err: "strconv.ParseFloat: parsing \"true\": invalid syntax",
|
|
|
|
},
|
2017-07-07 01:29:38 -07:00
|
|
|
{
|
|
|
|
input: "something_weird{problem=\"",
|
2018-05-14 13:19:53 -07:00
|
|
|
err: "expected label value, got \"INVALID\"",
|
2017-07-07 01:29:38 -07:00
|
|
|
},
|
2018-02-04 08:37:46 -08:00
|
|
|
{
|
|
|
|
input: "empty_label_name{=\"\"} 0",
|
2018-05-14 13:19:53 -07:00
|
|
|
err: "expected label name, got \"EQUAL\"",
|
2018-02-04 08:37:46 -08:00
|
|
|
},
|
2017-06-16 05:09:50 -07:00
|
|
|
}
|
|
|
|
|
2018-05-14 13:19:53 -07:00
|
|
|
for i, c := range cases {
|
2018-10-04 06:52:03 -07:00
|
|
|
p := NewPromParser([]byte(c.input))
|
2018-05-14 13:19:53 -07:00
|
|
|
var err error
|
|
|
|
for err == nil {
|
|
|
|
_, err = p.Next()
|
2017-06-16 05:09:50 -07:00
|
|
|
}
|
2018-05-14 13:19:53 -07:00
|
|
|
require.NotNil(t, err)
|
|
|
|
require.Equal(t, c.err, err.Error(), "test %d", i)
|
2017-06-16 05:09:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 06:52:03 -07:00
|
|
|
func TestPromNullByteHandling(t *testing.T) {
|
2017-07-07 01:29:38 -07:00
|
|
|
cases := []struct {
|
|
|
|
input string
|
|
|
|
err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: "null_byte_metric{a=\"abc\x00\"} 1",
|
|
|
|
err: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a{b=\"\x00ss\"} 1\n",
|
|
|
|
err: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a{b=\"\x00\"} 1\n",
|
|
|
|
err: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a{b=\"\x00\"} 1\n",
|
|
|
|
err: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a{b=\x00\"ssss\"} 1\n",
|
2018-05-14 13:19:53 -07:00
|
|
|
err: "expected label value, got \"INVALID\"",
|
2017-07-07 01:29:38 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a{b=\"\x00",
|
2018-05-14 13:19:53 -07:00
|
|
|
err: "expected label value, got \"INVALID\"",
|
2017-07-07 01:29:38 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a{b\x00=\"hiih\"} 1",
|
2018-05-14 13:19:53 -07:00
|
|
|
err: "expected equal, got \"INVALID\"",
|
2017-07-07 01:29:38 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a\x00{b=\"ddd\"} 1",
|
2018-05-14 13:19:53 -07:00
|
|
|
err: "expected value after metric, got \"MNAME\"",
|
2017-07-07 01:29:38 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-05-14 13:19:53 -07:00
|
|
|
for i, c := range cases {
|
2018-10-04 06:52:03 -07:00
|
|
|
p := NewPromParser([]byte(c.input))
|
2018-05-14 13:19:53 -07:00
|
|
|
var err error
|
|
|
|
for err == nil {
|
|
|
|
_, err = p.Next()
|
2017-07-07 01:29:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.err == "" {
|
2018-05-14 13:19:53 -07:00
|
|
|
require.Equal(t, io.EOF, err, "test %d", i)
|
2017-07-07 01:29:38 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-05-14 13:19:53 -07:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, c.err, err.Error(), "test %d", i)
|
2017-07-07 01:29:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-15 04:55:53 -08:00
|
|
|
const (
|
2018-10-04 06:52:03 -07:00
|
|
|
promtestdataSampleCount = 410
|
2017-01-15 04:55:53 -08:00
|
|
|
)
|
|
|
|
|
2018-10-04 09:57:47 -07:00
|
|
|
func BenchmarkParse(b *testing.B) {
|
|
|
|
for parserName, parser := range map[string]func([]byte) Parser{
|
|
|
|
"prometheus": NewPromParser,
|
2018-10-09 06:09:44 -07:00
|
|
|
"openmetrics": NewOpenMetricsParser,
|
2018-10-04 09:57:47 -07:00
|
|
|
} {
|
2017-01-15 04:55:53 -08:00
|
|
|
|
2018-10-04 09:57:47 -07:00
|
|
|
for _, fn := range []string{"promtestdata.txt", "promtestdata.nometa.txt"} {
|
|
|
|
f, err := os.Open(fn)
|
|
|
|
require.NoError(b, err)
|
|
|
|
defer f.Close()
|
2017-01-15 04:55:53 -08:00
|
|
|
|
2018-10-04 09:57:47 -07:00
|
|
|
buf, err := ioutil.ReadAll(f)
|
|
|
|
require.NoError(b, err)
|
2017-01-15 04:55:53 -08:00
|
|
|
|
2018-10-04 09:57:47 -07:00
|
|
|
b.Run(parserName+"/no-decode-metric/"+fn, func(b *testing.B) {
|
|
|
|
total := 0
|
|
|
|
|
|
|
|
b.SetBytes(int64(len(buf) * (b.N / promtestdataSampleCount)))
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i += promtestdataSampleCount {
|
|
|
|
p := parser(buf)
|
|
|
|
|
|
|
|
Outer:
|
|
|
|
for i < b.N {
|
|
|
|
t, err := p.Next()
|
|
|
|
switch t {
|
|
|
|
case EntryInvalid:
|
|
|
|
if err == io.EOF {
|
|
|
|
break Outer
|
|
|
|
}
|
|
|
|
b.Fatal(err)
|
|
|
|
case EntrySeries:
|
|
|
|
m, _, _ := p.Series()
|
|
|
|
total += len(m)
|
|
|
|
i++
|
2018-05-14 13:19:53 -07:00
|
|
|
}
|
|
|
|
}
|
2017-01-15 04:55:53 -08:00
|
|
|
}
|
2018-10-04 09:57:47 -07:00
|
|
|
_ = total
|
|
|
|
})
|
|
|
|
b.Run(parserName+"/decode-metric/"+fn, func(b *testing.B) {
|
|
|
|
total := 0
|
|
|
|
|
|
|
|
b.SetBytes(int64(len(buf) * (b.N / promtestdataSampleCount)))
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i += promtestdataSampleCount {
|
|
|
|
p := parser(buf)
|
|
|
|
|
|
|
|
Outer:
|
|
|
|
for i < b.N {
|
|
|
|
t, err := p.Next()
|
|
|
|
switch t {
|
|
|
|
case EntryInvalid:
|
|
|
|
if err == io.EOF {
|
|
|
|
break Outer
|
|
|
|
}
|
|
|
|
b.Fatal(err)
|
|
|
|
case EntrySeries:
|
|
|
|
m, _, _ := p.Series()
|
|
|
|
|
|
|
|
res := make(labels.Labels, 0, 5)
|
|
|
|
p.Metric(&res)
|
|
|
|
|
|
|
|
total += len(m)
|
|
|
|
i++
|
2018-05-14 13:19:53 -07:00
|
|
|
}
|
|
|
|
}
|
2017-01-15 04:55:53 -08:00
|
|
|
}
|
2018-10-04 09:57:47 -07:00
|
|
|
_ = total
|
|
|
|
})
|
|
|
|
b.Run(parserName+"/decode-metric-reuse/"+fn, func(b *testing.B) {
|
|
|
|
total := 0
|
|
|
|
res := make(labels.Labels, 0, 5)
|
|
|
|
|
|
|
|
b.SetBytes(int64(len(buf) * (b.N / promtestdataSampleCount)))
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i += promtestdataSampleCount {
|
|
|
|
p := parser(buf)
|
|
|
|
|
|
|
|
Outer:
|
|
|
|
for i < b.N {
|
|
|
|
t, err := p.Next()
|
|
|
|
switch t {
|
|
|
|
case EntryInvalid:
|
|
|
|
if err == io.EOF {
|
|
|
|
break Outer
|
|
|
|
}
|
|
|
|
b.Fatal(err)
|
|
|
|
case EntrySeries:
|
|
|
|
m, _, _ := p.Series()
|
|
|
|
|
|
|
|
p.Metric(&res)
|
|
|
|
|
|
|
|
total += len(m)
|
|
|
|
i++
|
|
|
|
res = res[:0]
|
2018-05-14 13:19:53 -07:00
|
|
|
}
|
|
|
|
}
|
2017-01-15 04:55:53 -08:00
|
|
|
}
|
2018-10-04 09:57:47 -07:00
|
|
|
_ = total
|
|
|
|
})
|
|
|
|
b.Run("expfmt-text/"+fn, func(b *testing.B) {
|
|
|
|
b.SetBytes(int64(len(buf) * (b.N / promtestdataSampleCount)))
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
total := 0
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i += promtestdataSampleCount {
|
|
|
|
var (
|
|
|
|
decSamples = make(model.Vector, 0, 50)
|
|
|
|
)
|
|
|
|
sdec := expfmt.SampleDecoder{
|
|
|
|
Dec: expfmt.NewDecoder(bytes.NewReader(buf), expfmt.FmtText),
|
|
|
|
Opts: &expfmt.DecodeOptions{
|
|
|
|
Timestamp: model.TimeFromUnixNano(0),
|
|
|
|
},
|
|
|
|
}
|
2017-01-15 04:55:53 -08:00
|
|
|
|
2018-10-04 09:57:47 -07:00
|
|
|
for {
|
|
|
|
if err = sdec.Decode(&decSamples); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
total += len(decSamples)
|
|
|
|
decSamples = decSamples[:0]
|
2017-01-15 04:55:53 -08:00
|
|
|
}
|
|
|
|
}
|
2018-10-04 09:57:47 -07:00
|
|
|
_ = total
|
|
|
|
})
|
|
|
|
}
|
2017-01-15 04:55:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
func BenchmarkGzip(b *testing.B) {
|
2018-10-04 06:52:03 -07:00
|
|
|
for _, fn := range []string{"promtestdata.txt", "promtestdata.nometa.txt"} {
|
2017-01-15 04:55:53 -08:00
|
|
|
b.Run(fn, func(b *testing.B) {
|
|
|
|
f, err := os.Open(fn)
|
|
|
|
require.NoError(b, err)
|
|
|
|
defer f.Close()
|
2017-01-14 07:39:04 -08:00
|
|
|
|
2017-01-15 04:55:53 -08:00
|
|
|
var buf bytes.Buffer
|
|
|
|
gw := gzip.NewWriter(&buf)
|
2017-01-14 07:39:04 -08:00
|
|
|
|
2017-01-15 04:55:53 -08:00
|
|
|
n, err := io.Copy(gw, f)
|
|
|
|
require.NoError(b, err)
|
|
|
|
require.NoError(b, gw.Close())
|
2017-01-14 07:39:04 -08:00
|
|
|
|
2017-01-15 04:55:53 -08:00
|
|
|
gbuf, err := ioutil.ReadAll(&buf)
|
|
|
|
require.NoError(b, err)
|
2017-01-14 07:39:04 -08:00
|
|
|
|
2018-10-04 06:52:03 -07:00
|
|
|
k := b.N / promtestdataSampleCount
|
2017-01-14 07:39:04 -08:00
|
|
|
|
2017-01-15 04:55:53 -08:00
|
|
|
b.ReportAllocs()
|
|
|
|
b.SetBytes(int64(k) * int64(n))
|
|
|
|
b.ResetTimer()
|
2017-01-14 07:39:04 -08:00
|
|
|
|
2017-01-15 04:55:53 -08:00
|
|
|
total := 0
|
2017-01-14 07:39:04 -08:00
|
|
|
|
2017-01-15 04:55:53 -08:00
|
|
|
for i := 0; i < k; i++ {
|
|
|
|
gr, err := gzip.NewReader(bytes.NewReader(gbuf))
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
d, err := ioutil.ReadAll(gr)
|
|
|
|
require.NoError(b, err)
|
|
|
|
require.NoError(b, gr.Close())
|
|
|
|
|
|
|
|
total += len(d)
|
|
|
|
}
|
|
|
|
_ = total
|
|
|
|
})
|
2017-01-14 07:39:04 -08:00
|
|
|
}
|
|
|
|
}
|