2013-02-08 09:03:26 -08:00
|
|
|
// Copyright 2013 Prometheus Team
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package metric
|
|
|
|
|
|
|
|
import (
|
2014-03-28 03:58:47 -07:00
|
|
|
"sort"
|
2013-02-08 09:03:26 -08:00
|
|
|
"testing"
|
|
|
|
"time"
|
2013-06-25 05:02:27 -07:00
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/utility/test"
|
2013-02-08 09:03:26 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetFingerprintsForLabelSetTests(p MetricPersistence, t test.Tester) {
|
2014-03-28 03:58:47 -07:00
|
|
|
metrics := []clientmodel.Metric{
|
|
|
|
{
|
|
|
|
clientmodel.MetricNameLabel: "test_metric",
|
|
|
|
"method": "get",
|
|
|
|
"result": "success",
|
2013-02-08 09:03:26 -08:00
|
|
|
},
|
2014-03-28 03:58:47 -07:00
|
|
|
{
|
|
|
|
clientmodel.MetricNameLabel: "test_metric",
|
|
|
|
"method": "get",
|
|
|
|
"result": "failure",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
clientmodel.MetricNameLabel: "test_metric",
|
|
|
|
"method": "post",
|
|
|
|
"result": "success",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
clientmodel.MetricNameLabel: "test_metric",
|
|
|
|
"method": "post",
|
|
|
|
"result": "failure",
|
2013-02-08 09:03:26 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-03-28 03:58:47 -07:00
|
|
|
newTestLabelMatcher := func(matchType MatchType, name clientmodel.LabelName, value clientmodel.LabelValue) *LabelMatcher {
|
|
|
|
m, err := NewLabelMatcher(matchType, name, value)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't create label matcher: %v", err)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []struct {
|
|
|
|
in LabelMatchers
|
|
|
|
outIndexes []int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
in: LabelMatchers{
|
|
|
|
newTestLabelMatcher(Equal, clientmodel.MetricNameLabel, "test_metric"),
|
|
|
|
},
|
|
|
|
outIndexes: []int{0, 1, 2, 3},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: LabelMatchers{
|
|
|
|
newTestLabelMatcher(Equal, clientmodel.MetricNameLabel, "non_existent_metric"),
|
|
|
|
},
|
|
|
|
outIndexes: []int{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: LabelMatchers{
|
|
|
|
newTestLabelMatcher(Equal, clientmodel.MetricNameLabel, "non_existent_metric"),
|
|
|
|
newTestLabelMatcher(Equal, "result", "success"),
|
|
|
|
},
|
|
|
|
outIndexes: []int{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: LabelMatchers{
|
|
|
|
newTestLabelMatcher(Equal, clientmodel.MetricNameLabel, "test_metric"),
|
|
|
|
newTestLabelMatcher(Equal, "result", "success"),
|
|
|
|
},
|
|
|
|
outIndexes: []int{0, 2},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: LabelMatchers{
|
|
|
|
newTestLabelMatcher(Equal, clientmodel.MetricNameLabel, "test_metric"),
|
|
|
|
newTestLabelMatcher(NotEqual, "result", "success"),
|
|
|
|
},
|
|
|
|
outIndexes: []int{1, 3},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: LabelMatchers{
|
|
|
|
newTestLabelMatcher(Equal, clientmodel.MetricNameLabel, "test_metric"),
|
|
|
|
newTestLabelMatcher(RegexMatch, "result", "foo|success|bar"),
|
|
|
|
},
|
|
|
|
outIndexes: []int{0, 2},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: LabelMatchers{
|
|
|
|
newTestLabelMatcher(Equal, clientmodel.MetricNameLabel, "test_metric"),
|
|
|
|
newTestLabelMatcher(RegexNoMatch, "result", "foo|success|bar"),
|
|
|
|
},
|
|
|
|
outIndexes: []int{1, 3},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: LabelMatchers{
|
|
|
|
newTestLabelMatcher(Equal, clientmodel.MetricNameLabel, "test_metric"),
|
|
|
|
newTestLabelMatcher(RegexNoMatch, "result", "foo|success|bar"),
|
|
|
|
newTestLabelMatcher(RegexMatch, "method", "os"),
|
|
|
|
},
|
|
|
|
outIndexes: []int{3},
|
|
|
|
},
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
|
2014-03-28 03:58:47 -07:00
|
|
|
for _, m := range metrics {
|
|
|
|
testAppendSamples(p, &clientmodel.Sample{
|
|
|
|
Value: 0,
|
|
|
|
Timestamp: 0,
|
|
|
|
Metric: m,
|
|
|
|
}, t)
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
|
2014-03-28 03:58:47 -07:00
|
|
|
for i, s := range scenarios {
|
|
|
|
actualFps, err := p.GetFingerprintsForLabelMatchers(s.in)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%d. Couldn't get fingerprints for label matchers: %v", i, err)
|
|
|
|
}
|
2013-02-08 09:03:26 -08:00
|
|
|
|
2014-03-28 03:58:47 -07:00
|
|
|
expectedFps := clientmodel.Fingerprints{}
|
|
|
|
for _, i := range s.outIndexes {
|
|
|
|
fp := &clientmodel.Fingerprint{}
|
|
|
|
fp.LoadFromMetric(metrics[i])
|
|
|
|
expectedFps = append(expectedFps, fp)
|
|
|
|
}
|
2013-02-08 09:03:26 -08:00
|
|
|
|
2014-03-28 03:58:47 -07:00
|
|
|
sort.Sort(actualFps)
|
|
|
|
sort.Sort(expectedFps)
|
2013-02-08 09:03:26 -08:00
|
|
|
|
2014-03-28 03:58:47 -07:00
|
|
|
if len(actualFps) != len(expectedFps) {
|
|
|
|
t.Fatalf("%d. Got %d fingerprints; want %d", i, len(actualFps), len(expectedFps))
|
|
|
|
}
|
|
|
|
|
|
|
|
for j, actualFp := range actualFps {
|
|
|
|
if !actualFp.Equal(expectedFps[j]) {
|
|
|
|
t.Fatalf("%d.%d. Got fingerprint %v; want %v", i, j, actualFp, expectedFps[j])
|
|
|
|
}
|
|
|
|
}
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-24 04:08:28 -07:00
|
|
|
func GetLabelValuesForLabelNameTests(p MetricPersistence, t test.Tester) {
|
|
|
|
testAppendSamples(p, &clientmodel.Sample{
|
|
|
|
Value: 0,
|
|
|
|
Timestamp: 0,
|
|
|
|
Metric: clientmodel.Metric{
|
|
|
|
clientmodel.MetricNameLabel: "my_metric",
|
|
|
|
"request_type": "create",
|
|
|
|
"result": "success",
|
|
|
|
},
|
|
|
|
}, t)
|
|
|
|
|
|
|
|
testAppendSamples(p, &clientmodel.Sample{
|
|
|
|
Value: 0,
|
|
|
|
Timestamp: 0,
|
|
|
|
Metric: clientmodel.Metric{
|
|
|
|
clientmodel.MetricNameLabel: "my_metric",
|
|
|
|
"request_type": "delete",
|
|
|
|
"outcome": "failure",
|
|
|
|
},
|
|
|
|
}, t)
|
|
|
|
|
|
|
|
expectedIndex := map[clientmodel.LabelName]clientmodel.LabelValues{
|
|
|
|
clientmodel.MetricNameLabel: {"my_metric"},
|
|
|
|
"request_type": {"create", "delete"},
|
|
|
|
"result": {"success"},
|
|
|
|
"outcome": {"failure"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, expected := range expectedIndex {
|
|
|
|
actual, err := p.GetLabelValuesForLabelName(name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error getting values for label %s: %v", name, err)
|
|
|
|
}
|
|
|
|
if len(actual) != len(expected) {
|
|
|
|
t.Fatalf("Number of values don't match for label %s: got %d; want %d", name, len(actual), len(expected))
|
|
|
|
}
|
|
|
|
for i := range expected {
|
|
|
|
if actual[i] != expected[i] {
|
|
|
|
t.Fatalf("%d. Got %s; want %s", i, actual[i], expected[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 09:03:26 -08:00
|
|
|
func GetMetricForFingerprintTests(p MetricPersistence, t test.Tester) {
|
2013-08-15 02:35:50 -07:00
|
|
|
testAppendSamples(p, &clientmodel.Sample{
|
2013-02-08 09:03:26 -08:00
|
|
|
Value: 0,
|
Use custom timestamp type for sample timestamps and related code.
So far we've been using Go's native time.Time for anything related to sample
timestamps. Since the range of time.Time is much bigger than what we need, this
has created two problems:
- there could be time.Time values which were out of the range/precision of the
time type that we persist to disk, therefore causing incorrectly ordered keys.
One bug caused by this was:
https://github.com/prometheus/prometheus/issues/367
It would be good to use a timestamp type that's more closely aligned with
what the underlying storage supports.
- sizeof(time.Time) is 192, while Prometheus should be ok with a single 64-bit
Unix timestamp (possibly even a 32-bit one). Since we store samples in large
numbers, this seriously affects memory usage. Furthermore, copying/working
with the data will be faster if it's smaller.
*MEMORY USAGE RESULTS*
Initial memory usage comparisons for a running Prometheus with 1 timeseries and
100,000 samples show roughly a 13% decrease in total (VIRT) memory usage. In my
tests, this advantage for some reason decreased a bit the more samples the
timeseries had (to 5-7% for millions of samples). This I can't fully explain,
but perhaps garbage collection issues were involved.
*WHEN TO USE THE NEW TIMESTAMP TYPE*
The new clientmodel.Timestamp type should be used whenever time
calculations are either directly or indirectly related to sample
timestamps.
For example:
- the timestamp of a sample itself
- all kinds of watermarks
- anything that may become or is compared to a sample timestamp (like the timestamp
passed into Target.Scrape()).
When to still use time.Time:
- for measuring durations/times not related to sample timestamps, like duration
telemetry exporting, timers that indicate how frequently to execute some
action, etc.
*NOTE ON OPERATOR OPTIMIZATION TESTS*
We don't use operator optimization code anymore, but it still lives in
the code as dead code. It still has tests, but I couldn't get all of them to
pass with the new timestamp format. I commented out the failing cases for now,
but we should probably remove the dead code soon. I just didn't want to do that
in the same change as this.
Change-Id: I821787414b0debe85c9fffaeb57abd453727af0f
2013-10-28 06:35:02 -07:00
|
|
|
Timestamp: 0,
|
2013-06-25 05:02:27 -07:00
|
|
|
Metric: clientmodel.Metric{
|
2013-02-08 09:03:26 -08:00
|
|
|
"request_type": "your_mom",
|
|
|
|
},
|
|
|
|
}, t)
|
|
|
|
|
2013-08-15 02:35:50 -07:00
|
|
|
testAppendSamples(p, &clientmodel.Sample{
|
2013-02-08 09:03:26 -08:00
|
|
|
Value: 0,
|
Use custom timestamp type for sample timestamps and related code.
So far we've been using Go's native time.Time for anything related to sample
timestamps. Since the range of time.Time is much bigger than what we need, this
has created two problems:
- there could be time.Time values which were out of the range/precision of the
time type that we persist to disk, therefore causing incorrectly ordered keys.
One bug caused by this was:
https://github.com/prometheus/prometheus/issues/367
It would be good to use a timestamp type that's more closely aligned with
what the underlying storage supports.
- sizeof(time.Time) is 192, while Prometheus should be ok with a single 64-bit
Unix timestamp (possibly even a 32-bit one). Since we store samples in large
numbers, this seriously affects memory usage. Furthermore, copying/working
with the data will be faster if it's smaller.
*MEMORY USAGE RESULTS*
Initial memory usage comparisons for a running Prometheus with 1 timeseries and
100,000 samples show roughly a 13% decrease in total (VIRT) memory usage. In my
tests, this advantage for some reason decreased a bit the more samples the
timeseries had (to 5-7% for millions of samples). This I can't fully explain,
but perhaps garbage collection issues were involved.
*WHEN TO USE THE NEW TIMESTAMP TYPE*
The new clientmodel.Timestamp type should be used whenever time
calculations are either directly or indirectly related to sample
timestamps.
For example:
- the timestamp of a sample itself
- all kinds of watermarks
- anything that may become or is compared to a sample timestamp (like the timestamp
passed into Target.Scrape()).
When to still use time.Time:
- for measuring durations/times not related to sample timestamps, like duration
telemetry exporting, timers that indicate how frequently to execute some
action, etc.
*NOTE ON OPERATOR OPTIMIZATION TESTS*
We don't use operator optimization code anymore, but it still lives in
the code as dead code. It still has tests, but I couldn't get all of them to
pass with the new timestamp format. I commented out the failing cases for now,
but we should probably remove the dead code soon. I just didn't want to do that
in the same change as this.
Change-Id: I821787414b0debe85c9fffaeb57abd453727af0f
2013-10-28 06:35:02 -07:00
|
|
|
Timestamp: 0,
|
2013-06-25 05:02:27 -07:00
|
|
|
Metric: clientmodel.Metric{
|
2013-02-08 09:03:26 -08:00
|
|
|
"request_type": "your_dad",
|
|
|
|
"one-off": "value",
|
|
|
|
},
|
|
|
|
}, t)
|
|
|
|
|
2014-03-28 03:58:47 -07:00
|
|
|
result, err := p.GetFingerprintsForLabelMatchers(LabelMatchers{{
|
|
|
|
Type: Equal,
|
|
|
|
Name: "request_type",
|
|
|
|
Value: "your_mom",
|
|
|
|
}})
|
2013-02-08 09:03:26 -08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result) != 1 {
|
|
|
|
t.Errorf("Expected one element.")
|
|
|
|
}
|
|
|
|
|
2013-05-14 07:25:06 -07:00
|
|
|
metric, err := p.GetMetricForFingerprint(result[0])
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
|
2013-05-14 07:25:06 -07:00
|
|
|
if metric == nil {
|
2013-02-08 09:03:26 -08:00
|
|
|
t.Fatal("Did not expect nil.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(metric) != 1 {
|
|
|
|
t.Errorf("Expected one-dimensional metric.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if metric["request_type"] != "your_mom" {
|
|
|
|
t.Errorf("Expected metric to match.")
|
|
|
|
}
|
|
|
|
|
2014-03-28 03:58:47 -07:00
|
|
|
result, err = p.GetFingerprintsForLabelMatchers(LabelMatchers{{
|
|
|
|
Type: Equal,
|
|
|
|
Name: "request_type",
|
|
|
|
Value: "your_dad",
|
|
|
|
}})
|
2013-02-08 09:03:26 -08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result) != 1 {
|
|
|
|
t.Errorf("Expected one element.")
|
|
|
|
}
|
|
|
|
|
2013-05-14 07:25:06 -07:00
|
|
|
metric, err = p.GetMetricForFingerprint(result[0])
|
2013-02-08 09:03:26 -08:00
|
|
|
|
2013-05-14 07:25:06 -07:00
|
|
|
if metric == nil {
|
2013-02-08 09:03:26 -08:00
|
|
|
t.Fatal("Did not expect nil.")
|
|
|
|
}
|
|
|
|
|
2013-05-14 07:25:06 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(metric) != 2 {
|
|
|
|
t.Errorf("Expected two-dimensional metric.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if metric["request_type"] != "your_dad" {
|
|
|
|
t.Errorf("Expected metric to match.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if metric["one-off"] != "value" {
|
|
|
|
t.Errorf("Expected metric to match.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that mutating a returned metric does not result in the mutated
|
|
|
|
// metric to be returned at the next GetMetricForFingerprint() call.
|
|
|
|
metric["one-off"] = "new value"
|
|
|
|
metric, err = p.GetMetricForFingerprint(result[0])
|
2013-02-08 09:03:26 -08:00
|
|
|
|
2013-05-14 07:25:06 -07:00
|
|
|
if metric == nil {
|
|
|
|
t.Fatal("Did not expect nil.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(metric) != 2 {
|
2013-05-14 07:25:06 -07:00
|
|
|
t.Errorf("Expected two-dimensional metric.")
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if metric["request_type"] != "your_dad" {
|
|
|
|
t.Errorf("Expected metric to match.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if metric["one-off"] != "value" {
|
|
|
|
t.Errorf("Expected metric to match.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func AppendRepeatingValuesTests(p MetricPersistence, t test.Tester) {
|
2013-06-25 05:02:27 -07:00
|
|
|
metric := clientmodel.Metric{
|
|
|
|
clientmodel.MetricNameLabel: "errors_total",
|
|
|
|
"controller": "foo",
|
|
|
|
"operation": "bar",
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
increments := 10
|
|
|
|
repetitions := 500
|
|
|
|
|
|
|
|
for i := 0; i < increments; i++ {
|
|
|
|
for j := 0; j < repetitions; j++ {
|
Use custom timestamp type for sample timestamps and related code.
So far we've been using Go's native time.Time for anything related to sample
timestamps. Since the range of time.Time is much bigger than what we need, this
has created two problems:
- there could be time.Time values which were out of the range/precision of the
time type that we persist to disk, therefore causing incorrectly ordered keys.
One bug caused by this was:
https://github.com/prometheus/prometheus/issues/367
It would be good to use a timestamp type that's more closely aligned with
what the underlying storage supports.
- sizeof(time.Time) is 192, while Prometheus should be ok with a single 64-bit
Unix timestamp (possibly even a 32-bit one). Since we store samples in large
numbers, this seriously affects memory usage. Furthermore, copying/working
with the data will be faster if it's smaller.
*MEMORY USAGE RESULTS*
Initial memory usage comparisons for a running Prometheus with 1 timeseries and
100,000 samples show roughly a 13% decrease in total (VIRT) memory usage. In my
tests, this advantage for some reason decreased a bit the more samples the
timeseries had (to 5-7% for millions of samples). This I can't fully explain,
but perhaps garbage collection issues were involved.
*WHEN TO USE THE NEW TIMESTAMP TYPE*
The new clientmodel.Timestamp type should be used whenever time
calculations are either directly or indirectly related to sample
timestamps.
For example:
- the timestamp of a sample itself
- all kinds of watermarks
- anything that may become or is compared to a sample timestamp (like the timestamp
passed into Target.Scrape()).
When to still use time.Time:
- for measuring durations/times not related to sample timestamps, like duration
telemetry exporting, timers that indicate how frequently to execute some
action, etc.
*NOTE ON OPERATOR OPTIMIZATION TESTS*
We don't use operator optimization code anymore, but it still lives in
the code as dead code. It still has tests, but I couldn't get all of them to
pass with the new timestamp format. I commented out the failing cases for now,
but we should probably remove the dead code soon. I just didn't want to do that
in the same change as this.
Change-Id: I821787414b0debe85c9fffaeb57abd453727af0f
2013-10-28 06:35:02 -07:00
|
|
|
time := clientmodel.Timestamp(0).Add(time.Duration(i) * time.Hour).Add(time.Duration(j) * time.Second)
|
2013-08-15 02:35:50 -07:00
|
|
|
testAppendSamples(p, &clientmodel.Sample{
|
2013-06-25 05:02:27 -07:00
|
|
|
Value: clientmodel.SampleValue(i),
|
2013-02-08 09:03:26 -08:00
|
|
|
Timestamp: time,
|
|
|
|
Metric: metric,
|
|
|
|
}, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
v, ok := p.(View)
|
|
|
|
if !ok {
|
|
|
|
// It's purely a benchmark for a MetricPersistence that is not viewable.
|
2013-02-08 09:03:26 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-28 03:58:47 -07:00
|
|
|
matchers := labelMatchersFromLabelSet(clientmodel.LabelSet{
|
2013-06-25 05:02:27 -07:00
|
|
|
clientmodel.MetricNameLabel: "errors_total",
|
|
|
|
"controller": "foo",
|
|
|
|
"operation": "bar",
|
2014-03-28 03:58:47 -07:00
|
|
|
})
|
2013-02-08 09:03:26 -08:00
|
|
|
|
|
|
|
for i := 0; i < increments; i++ {
|
|
|
|
for j := 0; j < repetitions; j++ {
|
2014-03-28 03:58:47 -07:00
|
|
|
fingerprints, err := p.GetFingerprintsForLabelMatchers(matchers)
|
2013-02-08 09:03:26 -08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(fingerprints) != 1 {
|
|
|
|
t.Fatalf("expected %d fingerprints, got %d", 1, len(fingerprints))
|
|
|
|
}
|
|
|
|
|
Use custom timestamp type for sample timestamps and related code.
So far we've been using Go's native time.Time for anything related to sample
timestamps. Since the range of time.Time is much bigger than what we need, this
has created two problems:
- there could be time.Time values which were out of the range/precision of the
time type that we persist to disk, therefore causing incorrectly ordered keys.
One bug caused by this was:
https://github.com/prometheus/prometheus/issues/367
It would be good to use a timestamp type that's more closely aligned with
what the underlying storage supports.
- sizeof(time.Time) is 192, while Prometheus should be ok with a single 64-bit
Unix timestamp (possibly even a 32-bit one). Since we store samples in large
numbers, this seriously affects memory usage. Furthermore, copying/working
with the data will be faster if it's smaller.
*MEMORY USAGE RESULTS*
Initial memory usage comparisons for a running Prometheus with 1 timeseries and
100,000 samples show roughly a 13% decrease in total (VIRT) memory usage. In my
tests, this advantage for some reason decreased a bit the more samples the
timeseries had (to 5-7% for millions of samples). This I can't fully explain,
but perhaps garbage collection issues were involved.
*WHEN TO USE THE NEW TIMESTAMP TYPE*
The new clientmodel.Timestamp type should be used whenever time
calculations are either directly or indirectly related to sample
timestamps.
For example:
- the timestamp of a sample itself
- all kinds of watermarks
- anything that may become or is compared to a sample timestamp (like the timestamp
passed into Target.Scrape()).
When to still use time.Time:
- for measuring durations/times not related to sample timestamps, like duration
telemetry exporting, timers that indicate how frequently to execute some
action, etc.
*NOTE ON OPERATOR OPTIMIZATION TESTS*
We don't use operator optimization code anymore, but it still lives in
the code as dead code. It still has tests, but I couldn't get all of them to
pass with the new timestamp format. I commented out the failing cases for now,
but we should probably remove the dead code soon. I just didn't want to do that
in the same change as this.
Change-Id: I821787414b0debe85c9fffaeb57abd453727af0f
2013-10-28 06:35:02 -07:00
|
|
|
time := clientmodel.Timestamp(0).Add(time.Duration(i) * time.Hour).Add(time.Duration(j) * time.Second)
|
2014-02-14 10:36:27 -08:00
|
|
|
samples := v.GetValueAtTime(fingerprints[0], time)
|
2013-04-18 07:10:52 -07:00
|
|
|
if len(samples) == 0 {
|
|
|
|
t.Fatal("expected at least one sample.")
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
expected := clientmodel.SampleValue(i)
|
2013-02-08 09:03:26 -08:00
|
|
|
|
2013-04-18 07:10:52 -07:00
|
|
|
for _, sample := range samples {
|
|
|
|
if sample.Value != expected {
|
2014-02-14 10:36:27 -08:00
|
|
|
t.Fatalf("expected %v value, got %v", expected, sample.Value)
|
2013-04-18 07:10:52 -07:00
|
|
|
}
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func AppendsRepeatingValuesTests(p MetricPersistence, t test.Tester) {
|
2013-06-25 05:02:27 -07:00
|
|
|
metric := clientmodel.Metric{
|
|
|
|
clientmodel.MetricNameLabel: "errors_total",
|
|
|
|
"controller": "foo",
|
|
|
|
"operation": "bar",
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
increments := 10
|
|
|
|
repetitions := 500
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
s := clientmodel.Samples{}
|
2013-02-08 09:03:26 -08:00
|
|
|
for i := 0; i < increments; i++ {
|
|
|
|
for j := 0; j < repetitions; j++ {
|
Use custom timestamp type for sample timestamps and related code.
So far we've been using Go's native time.Time for anything related to sample
timestamps. Since the range of time.Time is much bigger than what we need, this
has created two problems:
- there could be time.Time values which were out of the range/precision of the
time type that we persist to disk, therefore causing incorrectly ordered keys.
One bug caused by this was:
https://github.com/prometheus/prometheus/issues/367
It would be good to use a timestamp type that's more closely aligned with
what the underlying storage supports.
- sizeof(time.Time) is 192, while Prometheus should be ok with a single 64-bit
Unix timestamp (possibly even a 32-bit one). Since we store samples in large
numbers, this seriously affects memory usage. Furthermore, copying/working
with the data will be faster if it's smaller.
*MEMORY USAGE RESULTS*
Initial memory usage comparisons for a running Prometheus with 1 timeseries and
100,000 samples show roughly a 13% decrease in total (VIRT) memory usage. In my
tests, this advantage for some reason decreased a bit the more samples the
timeseries had (to 5-7% for millions of samples). This I can't fully explain,
but perhaps garbage collection issues were involved.
*WHEN TO USE THE NEW TIMESTAMP TYPE*
The new clientmodel.Timestamp type should be used whenever time
calculations are either directly or indirectly related to sample
timestamps.
For example:
- the timestamp of a sample itself
- all kinds of watermarks
- anything that may become or is compared to a sample timestamp (like the timestamp
passed into Target.Scrape()).
When to still use time.Time:
- for measuring durations/times not related to sample timestamps, like duration
telemetry exporting, timers that indicate how frequently to execute some
action, etc.
*NOTE ON OPERATOR OPTIMIZATION TESTS*
We don't use operator optimization code anymore, but it still lives in
the code as dead code. It still has tests, but I couldn't get all of them to
pass with the new timestamp format. I commented out the failing cases for now,
but we should probably remove the dead code soon. I just didn't want to do that
in the same change as this.
Change-Id: I821787414b0debe85c9fffaeb57abd453727af0f
2013-10-28 06:35:02 -07:00
|
|
|
time := clientmodel.Timestamp(0).Add(time.Duration(i) * time.Hour).Add(time.Duration(j) * time.Second)
|
2013-06-25 05:02:27 -07:00
|
|
|
s = append(s, &clientmodel.Sample{
|
|
|
|
Value: clientmodel.SampleValue(i),
|
2013-02-08 09:03:26 -08:00
|
|
|
Timestamp: time,
|
|
|
|
Metric: metric,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p.AppendSamples(s)
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
v, ok := p.(View)
|
|
|
|
if !ok {
|
|
|
|
// It's purely a benchmark for a MetricPersistance that is not viewable.
|
2013-02-08 09:03:26 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-28 03:58:47 -07:00
|
|
|
matchers := labelMatchersFromLabelSet(clientmodel.LabelSet{
|
2013-06-25 05:02:27 -07:00
|
|
|
clientmodel.MetricNameLabel: "errors_total",
|
|
|
|
"controller": "foo",
|
|
|
|
"operation": "bar",
|
2014-03-28 03:58:47 -07:00
|
|
|
})
|
2013-02-08 09:03:26 -08:00
|
|
|
|
|
|
|
for i := 0; i < increments; i++ {
|
|
|
|
for j := 0; j < repetitions; j++ {
|
2014-03-28 03:58:47 -07:00
|
|
|
fingerprints, err := p.GetFingerprintsForLabelMatchers(matchers)
|
2013-02-08 09:03:26 -08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(fingerprints) != 1 {
|
|
|
|
t.Fatalf("expected %d fingerprints, got %d", 1, len(fingerprints))
|
|
|
|
}
|
|
|
|
|
Use custom timestamp type for sample timestamps and related code.
So far we've been using Go's native time.Time for anything related to sample
timestamps. Since the range of time.Time is much bigger than what we need, this
has created two problems:
- there could be time.Time values which were out of the range/precision of the
time type that we persist to disk, therefore causing incorrectly ordered keys.
One bug caused by this was:
https://github.com/prometheus/prometheus/issues/367
It would be good to use a timestamp type that's more closely aligned with
what the underlying storage supports.
- sizeof(time.Time) is 192, while Prometheus should be ok with a single 64-bit
Unix timestamp (possibly even a 32-bit one). Since we store samples in large
numbers, this seriously affects memory usage. Furthermore, copying/working
with the data will be faster if it's smaller.
*MEMORY USAGE RESULTS*
Initial memory usage comparisons for a running Prometheus with 1 timeseries and
100,000 samples show roughly a 13% decrease in total (VIRT) memory usage. In my
tests, this advantage for some reason decreased a bit the more samples the
timeseries had (to 5-7% for millions of samples). This I can't fully explain,
but perhaps garbage collection issues were involved.
*WHEN TO USE THE NEW TIMESTAMP TYPE*
The new clientmodel.Timestamp type should be used whenever time
calculations are either directly or indirectly related to sample
timestamps.
For example:
- the timestamp of a sample itself
- all kinds of watermarks
- anything that may become or is compared to a sample timestamp (like the timestamp
passed into Target.Scrape()).
When to still use time.Time:
- for measuring durations/times not related to sample timestamps, like duration
telemetry exporting, timers that indicate how frequently to execute some
action, etc.
*NOTE ON OPERATOR OPTIMIZATION TESTS*
We don't use operator optimization code anymore, but it still lives in
the code as dead code. It still has tests, but I couldn't get all of them to
pass with the new timestamp format. I commented out the failing cases for now,
but we should probably remove the dead code soon. I just didn't want to do that
in the same change as this.
Change-Id: I821787414b0debe85c9fffaeb57abd453727af0f
2013-10-28 06:35:02 -07:00
|
|
|
time := clientmodel.Timestamp(0).Add(time.Duration(i) * time.Hour).Add(time.Duration(j) * time.Second)
|
2014-02-14 10:36:27 -08:00
|
|
|
samples := v.GetValueAtTime(fingerprints[0], time)
|
2013-04-18 07:10:52 -07:00
|
|
|
if len(samples) == 0 {
|
|
|
|
t.Fatal("expected at least one sample.")
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
expected := clientmodel.SampleValue(i)
|
2013-02-08 09:03:26 -08:00
|
|
|
|
2013-04-18 07:10:52 -07:00
|
|
|
for _, sample := range samples {
|
|
|
|
if sample.Value != expected {
|
2014-02-14 10:36:27 -08:00
|
|
|
t.Fatalf("expected %v value, got %v", expected, sample.Value)
|
2013-04-18 07:10:52 -07:00
|
|
|
}
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Definitions Below
|
|
|
|
|
|
|
|
var testLevelDBGetFingerprintsForLabelSet = buildLevelDBTestPersistence("get_fingerprints_for_labelset", GetFingerprintsForLabelSetTests)
|
|
|
|
|
|
|
|
func TestLevelDBGetFingerprintsForLabelSet(t *testing.T) {
|
|
|
|
testLevelDBGetFingerprintsForLabelSet(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLevelDBGetFingerprintsForLabelSet(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
testLevelDBGetFingerprintsForLabelSet(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-24 04:08:28 -07:00
|
|
|
var testLevelDBGetLabelValuesForLabelName = buildLevelDBTestPersistence("get_label_values_for_labelname", GetLabelValuesForLabelNameTests)
|
|
|
|
|
|
|
|
func TestLevelDBGetFingerprintsForLabelName(t *testing.T) {
|
|
|
|
testLevelDBGetLabelValuesForLabelName(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLevelDBGetLabelValuesForLabelName(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
testLevelDBGetLabelValuesForLabelName(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 09:03:26 -08:00
|
|
|
var testLevelDBGetMetricForFingerprint = buildLevelDBTestPersistence("get_metric_for_fingerprint", GetMetricForFingerprintTests)
|
|
|
|
|
|
|
|
func TestLevelDBGetMetricForFingerprint(t *testing.T) {
|
|
|
|
testLevelDBGetMetricForFingerprint(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLevelDBGetMetricForFingerprint(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
testLevelDBGetMetricForFingerprint(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var testLevelDBAppendRepeatingValues = buildLevelDBTestPersistence("append_repeating_values", AppendRepeatingValuesTests)
|
|
|
|
|
|
|
|
func TestLevelDBAppendRepeatingValues(t *testing.T) {
|
|
|
|
testLevelDBAppendRepeatingValues(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLevelDBAppendRepeatingValues(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
testLevelDBAppendRepeatingValues(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var testLevelDBAppendsRepeatingValues = buildLevelDBTestPersistence("appends_repeating_values", AppendsRepeatingValuesTests)
|
|
|
|
|
|
|
|
func TestLevelDBAppendsRepeatingValues(t *testing.T) {
|
|
|
|
testLevelDBAppendsRepeatingValues(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLevelDBAppendsRepeatingValues(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
testLevelDBAppendsRepeatingValues(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var testMemoryGetFingerprintsForLabelSet = buildMemoryTestPersistence(GetFingerprintsForLabelSetTests)
|
|
|
|
|
|
|
|
func TestMemoryGetFingerprintsForLabelSet(t *testing.T) {
|
|
|
|
testMemoryGetFingerprintsForLabelSet(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMemoryGetFingerprintsForLabelSet(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
testMemoryGetFingerprintsForLabelSet(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-24 04:08:28 -07:00
|
|
|
var testMemoryGetLabelValuesForLabelName = buildMemoryTestPersistence(GetLabelValuesForLabelNameTests)
|
|
|
|
|
|
|
|
func TestMemoryGetLabelValuesForLabelName(t *testing.T) {
|
|
|
|
testMemoryGetLabelValuesForLabelName(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMemoryGetLabelValuesForLabelName(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
testMemoryGetLabelValuesForLabelName(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 09:03:26 -08:00
|
|
|
var testMemoryGetMetricForFingerprint = buildMemoryTestPersistence(GetMetricForFingerprintTests)
|
|
|
|
|
|
|
|
func TestMemoryGetMetricForFingerprint(t *testing.T) {
|
|
|
|
testMemoryGetMetricForFingerprint(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMemoryGetMetricForFingerprint(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
testMemoryGetMetricForFingerprint(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var testMemoryAppendRepeatingValues = buildMemoryTestPersistence(AppendRepeatingValuesTests)
|
|
|
|
|
|
|
|
func TestMemoryAppendRepeatingValues(t *testing.T) {
|
|
|
|
testMemoryAppendRepeatingValues(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMemoryAppendRepeatingValues(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
testMemoryAppendRepeatingValues(b)
|
|
|
|
}
|
|
|
|
}
|