From 9f4bdaab50eaeb09adefe14dd31d4c1c68f5b398 Mon Sep 17 00:00:00 2001 From: "Matt T. Proud" Date: Wed, 28 Nov 2012 22:48:28 +0100 Subject: [PATCH] Major LevelDB metric end-to-end test improvements. Performance is enhanced through better range selection. --- storage/metric/leveldb/leveldb.go | 4 -- storage/metric/leveldb/leveldb_test.go | 68 ++++++++++++++++---------- 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/storage/metric/leveldb/leveldb.go b/storage/metric/leveldb/leveldb.go index c86bb2ff6..38aba87cc 100644 --- a/storage/metric/leveldb/leveldb.go +++ b/storage/metric/leveldb/leveldb.go @@ -16,7 +16,6 @@ package leveldb import ( "code.google.com/p/goprotobuf/proto" "errors" - "fmt" "github.com/matttproud/prometheus/coding" "github.com/matttproud/prometheus/coding/indexable" "github.com/matttproud/prometheus/model" @@ -447,8 +446,6 @@ func (l *LevelDBMetricPersistence) appendFingerprints(ddo *data.MetricDDO) error } func (l *LevelDBMetricPersistence) AppendSample(sample *model.Sample) error { - fmt.Printf("Sample: %q\n", sample) - metricDDO := ddoFromSample(sample) if indexHas, indexHasError := l.hasIndexMetric(metricDDO); indexHasError == nil { @@ -628,7 +625,6 @@ func (l *LevelDBMetricPersistence) GetWatermarksForMetric(metric model.Metric) ( return emission, foundEntries, nil } foundEntries++ - log.Printf("b foundEntries++ %d\n", foundEntries) emission.NewestInclusive = indexable.DecodeTime(found.Timestamp) } else { log.Printf("Could not de-serialize subsequent key: %q\n", subsequentUnmarshalErr) diff --git a/storage/metric/leveldb/leveldb_test.go b/storage/metric/leveldb/leveldb_test.go index 771bcfd9e..f7b937b77 100644 --- a/storage/metric/leveldb/leveldb_test.go +++ b/storage/metric/leveldb/leveldb_test.go @@ -27,6 +27,10 @@ import ( "time" ) +const ( + stochasticMaximumVariance = 64 +) + func TestBasicLifecycle(t *testing.T) { temporaryDirectory, temporaryDirectoryErr := ioutil.TempDir("", "leveldb_metric_persistence_test") @@ -325,13 +329,15 @@ func TestAppendSampleAsPureSingleEntityAppend(t *testing.T) { func TestStochastic(t *testing.T) { stochastic := func(x int) bool { + s := time.Now() seed := rand.NewSource(int64(x)) random := rand.New(seed) - numberOfMetrics := random.Intn(5) + 1 - numberOfSharedLabels := random.Intn(5) - numberOfUnsharedLabels := random.Intn(5) - numberOfSamples := random.Intn(1024) + 2 - numberOfRangeScans := random.Intn(3) + + numberOfMetrics := random.Intn(stochasticMaximumVariance) + 1 + numberOfSharedLabels := random.Intn(stochasticMaximumVariance) + numberOfUnsharedLabels := random.Intn(stochasticMaximumVariance) + numberOfSamples := random.Intn(stochasticMaximumVariance) + 2 + numberOfRangeScans := random.Intn(stochasticMaximumVariance) temporaryDirectory, _ := ioutil.TempDir("", "leveldb_metric_persistence_test") @@ -544,10 +550,6 @@ func TestStochastic(t *testing.T) { return false } - minimum := metricEarliestSample[metricIndex] - maximum := metricNewestSample[metricIndex] - spread := maximum - minimum - for i := 0; i < numberOfRangeScans; i++ { timestamps := metricTimestamps[metricIndex] @@ -555,34 +557,48 @@ func TestStochastic(t *testing.T) { var second int64 = 0 for { - first = minimum + random.Int63n(spread) - if _, has := timestamps[first]; has { - break + firstCandidate := random.Int63n(int64(len(timestamps))) + secondCandidate := random.Int63n(int64(len(timestamps))) + + smallest := int64(-1) + largest := int64(-1) + + if firstCandidate == secondCandidate { + continue + } else if firstCandidate > secondCandidate { + largest = firstCandidate + smallest = secondCandidate + } else { + largest = secondCandidate + smallest = firstCandidate } - } - for { - second = minimum + random.Int63n(spread) - if _, has := timestamps[second]; has && second != first { - break + j := int64(0) + for i := range timestamps { + if j == smallest { + first = i + } else if j == largest { + second = i + break + } + j++ } + + break } - var begin int64 = 0 - var end int64 = 0 + begin := first + end := second - if first > second { - begin = second - end = first - } else { - begin = first - end = second + if second < first { + begin, end = second, first } interval := model.Interval{ OldestInclusive: time.Unix(begin, 0), NewestInclusive: time.Unix(end, 0), } + rangeValues, rangeErr := persistence.GetSamplesForMetric(metric, interval) if rangeErr != nil { @@ -595,6 +611,8 @@ func TestStochastic(t *testing.T) { } } + fmt.Printf("Duration %q\n", time.Now().Sub(s)) + return true }