2015-02-02 17:14:36 +03:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-06-08 13:48:16 +04:00
|
|
|
// 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.
|
|
|
|
|
2013-06-11 13:43:39 +04:00
|
|
|
package extraction
|
2013-06-08 13:48:16 +04:00
|
|
|
|
|
|
|
import (
|
2015-01-27 19:16:23 +03:00
|
|
|
"bytes"
|
2014-04-25 14:50:18 +04:00
|
|
|
"errors"
|
2015-01-27 19:16:23 +03:00
|
|
|
"io/ioutil"
|
2013-06-08 13:48:16 +04:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"runtime"
|
2013-08-10 16:07:39 +04:00
|
|
|
"sort"
|
2013-06-08 13:48:16 +04:00
|
|
|
"testing"
|
2013-06-11 13:43:39 +04:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/model"
|
2013-06-08 13:48:16 +04:00
|
|
|
)
|
|
|
|
|
2013-12-03 13:17:04 +04:00
|
|
|
var test002Time = model.Now()
|
2013-08-10 16:07:39 +04:00
|
|
|
|
|
|
|
type testProcessor002ProcessScenario struct {
|
|
|
|
in string
|
2014-12-31 15:53:17 +03:00
|
|
|
expected, actual []model.Samples
|
2013-08-10 16:07:39 +04:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2014-12-31 15:53:17 +03:00
|
|
|
func (s *testProcessor002ProcessScenario) Ingest(samples model.Samples) error {
|
|
|
|
s.actual = append(s.actual, samples)
|
2013-08-10 16:07:39 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
func (s *testProcessor002ProcessScenario) test(t testing.TB, set int) {
|
2013-08-10 16:07:39 +04:00
|
|
|
reader, err := os.Open(path.Join("fixtures", s.in))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%d. couldn't open scenario input file %s: %s", set, s.in, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
options := &ProcessOptions{
|
2013-08-10 16:46:19 +04:00
|
|
|
Timestamp: test002Time,
|
2013-08-10 16:07:39 +04:00
|
|
|
}
|
2015-02-26 02:36:30 +03:00
|
|
|
err = Processor002.ProcessSingle(reader, s, options)
|
|
|
|
if s.err != err && (s.err == nil || err == nil || err.Error() != s.err.Error()) {
|
2013-08-10 16:07:39 +04:00
|
|
|
t.Fatalf("%d. expected err of %s, got %s", set, s.err, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.actual) != len(s.expected) {
|
|
|
|
t.Fatalf("%d. expected output length of %d, got %d", set, len(s.expected), len(s.actual))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, expected := range s.expected {
|
2014-12-31 15:53:17 +03:00
|
|
|
sort.Sort(s.actual[i])
|
|
|
|
sort.Sort(expected)
|
2013-08-10 16:07:39 +04:00
|
|
|
|
2014-12-31 15:53:17 +03:00
|
|
|
if !expected.Equal(s.actual[i]) {
|
2013-08-10 16:07:39 +04:00
|
|
|
t.Fatalf("%d.%d. expected %s, got %s", set, i, expected, s.actual[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
func testProcessor002Process(t testing.TB) {
|
2013-08-10 16:07:39 +04:00
|
|
|
var scenarios = []testProcessor002ProcessScenario{
|
2013-06-08 13:48:16 +04:00
|
|
|
{
|
|
|
|
in: "empty.json",
|
2014-04-25 14:50:18 +04:00
|
|
|
err: errors.New("EOF"),
|
2013-06-08 13:48:16 +04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
in: "test0_0_1-0_0_2.json",
|
2014-12-31 15:53:17 +03:00
|
|
|
expected: []model.Samples{
|
|
|
|
model.Samples{
|
|
|
|
&model.Sample{
|
|
|
|
Metric: model.Metric{"service": "zed", model.MetricNameLabel: "rpc_calls_total", "job": "batch_job"},
|
|
|
|
Value: 25,
|
|
|
|
Timestamp: test002Time,
|
|
|
|
},
|
|
|
|
&model.Sample{
|
|
|
|
Metric: model.Metric{"service": "bar", model.MetricNameLabel: "rpc_calls_total", "job": "batch_job"},
|
|
|
|
Value: 25,
|
|
|
|
Timestamp: test002Time,
|
|
|
|
},
|
|
|
|
&model.Sample{
|
|
|
|
Metric: model.Metric{"service": "foo", model.MetricNameLabel: "rpc_calls_total", "job": "batch_job"},
|
|
|
|
Value: 25,
|
|
|
|
Timestamp: test002Time,
|
|
|
|
},
|
|
|
|
&model.Sample{
|
|
|
|
Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"},
|
|
|
|
Value: 0.0459814091918713,
|
|
|
|
Timestamp: test002Time,
|
|
|
|
},
|
|
|
|
&model.Sample{
|
|
|
|
Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"},
|
|
|
|
Value: 78.48563317257356,
|
|
|
|
Timestamp: test002Time,
|
|
|
|
},
|
|
|
|
&model.Sample{
|
|
|
|
Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"},
|
|
|
|
Value: 15.890724674774395,
|
|
|
|
Timestamp: test002Time,
|
|
|
|
},
|
|
|
|
&model.Sample{
|
|
|
|
|
|
|
|
Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"},
|
|
|
|
Value: 0.0459814091918713,
|
|
|
|
Timestamp: test002Time,
|
|
|
|
},
|
|
|
|
&model.Sample{
|
|
|
|
Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"},
|
|
|
|
Value: 78.48563317257356,
|
|
|
|
Timestamp: test002Time,
|
|
|
|
},
|
|
|
|
&model.Sample{
|
|
|
|
Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"},
|
|
|
|
Value: 15.890724674774395,
|
|
|
|
Timestamp: test002Time,
|
|
|
|
},
|
|
|
|
&model.Sample{
|
|
|
|
Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"},
|
|
|
|
Value: 0.6120456642749681,
|
|
|
|
Timestamp: test002Time,
|
|
|
|
},
|
|
|
|
&model.Sample{
|
|
|
|
Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"},
|
|
|
|
Value: 97.31798360385088,
|
|
|
|
Timestamp: test002Time,
|
|
|
|
},
|
|
|
|
&model.Sample{
|
|
|
|
Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"},
|
|
|
|
Value: 84.63044031436561,
|
|
|
|
Timestamp: test002Time,
|
|
|
|
},
|
|
|
|
&model.Sample{
|
|
|
|
Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"},
|
|
|
|
Value: 1.355915069887731,
|
|
|
|
Timestamp: test002Time,
|
|
|
|
},
|
|
|
|
&model.Sample{
|
|
|
|
Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"},
|
|
|
|
Value: 109.89202084295582,
|
|
|
|
Timestamp: test002Time,
|
|
|
|
},
|
|
|
|
&model.Sample{
|
|
|
|
Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"},
|
|
|
|
Value: 160.21100853053224,
|
|
|
|
Timestamp: test002Time,
|
|
|
|
},
|
|
|
|
&model.Sample{
|
|
|
|
Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"},
|
|
|
|
Value: 1.772733213161236,
|
|
|
|
Timestamp: test002Time,
|
|
|
|
},
|
|
|
|
&model.Sample{
|
|
|
|
Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"},
|
|
|
|
Value: 109.99626121011262,
|
|
|
|
Timestamp: test002Time,
|
|
|
|
},
|
|
|
|
&model.Sample{
|
|
|
|
Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"},
|
|
|
|
Value: 172.49828748957728,
|
|
|
|
Timestamp: test002Time,
|
2013-08-10 16:07:39 +04:00
|
|
|
},
|
2013-06-08 13:48:16 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, scenario := range scenarios {
|
2013-08-10 16:07:39 +04:00
|
|
|
scenario.test(t, i)
|
2013-06-08 13:48:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessor002Process(t *testing.T) {
|
|
|
|
testProcessor002Process(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkProcessor002Process(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
|
|
|
|
pre := runtime.MemStats{}
|
|
|
|
runtime.ReadMemStats(&pre)
|
|
|
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
testProcessor002Process(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
post := runtime.MemStats{}
|
|
|
|
runtime.ReadMemStats(&post)
|
|
|
|
|
|
|
|
allocated := post.TotalAlloc - pre.TotalAlloc
|
|
|
|
|
|
|
|
b.Logf("Allocated %d at %f per cycle with %d cycles.", allocated, float64(allocated)/float64(b.N), b.N)
|
|
|
|
}
|
2015-01-27 19:16:23 +03:00
|
|
|
|
|
|
|
func BenchmarkProcessor002ParseOnly(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
data, err := ioutil.ReadFile("fixtures/test0_0_1-0_0_2-large.json")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
ing := fakeIngester{}
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if err := Processor002.ProcessSingle(bytes.NewReader(data), ing, &ProcessOptions{}); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeIngester struct{}
|
|
|
|
|
|
|
|
func (i fakeIngester) Ingest(model.Samples) error {
|
|
|
|
return nil
|
|
|
|
}
|