コード例 #1
0
ファイル: db_test.go プロジェクト: chzyer-dev/cockroach
func (tm *testModel) storeInModel(r Resolution, data TimeSeriesData) {
	// Note the source, used to construct keys for model queries.
	tm.seenSources[data.Source] = struct{}{}

	// Process and store data in the model.
	internalData, err := data.ToInternal(r.KeyDuration(), r.SampleDuration())
	if err != nil {
		tm.t.Fatalf("test could not convert time series to internal format: %s", err.Error())
	}

	for _, idata := range internalData {
		key := MakeDataKey(data.Name, data.Source, r, idata.StartTimestampNanos)
		keyStr := string(key)

		existing, ok := tm.modelData[keyStr]
		var newTs roachpb.InternalTimeSeriesData
		if ok {
			existingTs, err := existing.GetTimeseries()
			if err != nil {
				tm.t.Fatalf("test could not extract time series from existing model value: %s", err.Error())
			}
			newTs, err = engine.MergeInternalTimeSeriesData(existingTs, idata)
			if err != nil {
				tm.t.Fatalf("test could not merge time series into model value: %s", err.Error())
			}
		} else {
			newTs, err = engine.MergeInternalTimeSeriesData(idata)
			if err != nil {
				tm.t.Fatalf("test could not merge time series into model value: %s", err.Error())
			}
		}
		var val roachpb.Value
		if err := val.SetProto(&newTs); err != nil {
			tm.t.Fatal(err)
		}
		tm.modelData[keyStr] = val
	}
}
コード例 #2
0
// TestNegativeMax is a regression test for github issue #5414; the max value
// could be improperly set on disk if the real maximum was a negative number.
func TestNegativeMax(t *testing.T) {
	defer leaktest.AfterTest(t)()
	ts := ts("test.series",
		tsdp((time.Hour*5)+(time.Minute*5), -1.0),
		tsdp((time.Hour*5)+(time.Minute*5), -2.0),
	)
	internal, err := ts.ToInternal(Resolution10s.KeyDuration(), Resolution10s.SampleDuration())
	if err != nil {
		t.Fatal(err)
	}

	out, err := engine.MergeInternalTimeSeriesData(internal...)
	if err != nil {
		t.Fatal(err)
	}

	if maxVal := out.Samples[0].Max; maxVal == nil {
		t.Fatal("Expected maximum of sample 0 to be non-nil after initial merge.")
	} else if *maxVal != -1.0 {
		t.Fatalf("Expected maximum of sample 0 to be -1.0 after initial merge, was %f", *maxVal)
	}

	// Prevent regression of #5414: The negative number was only improperly set
	// if a sample with an explicitly set negative max value is merged (it did
	// not occur when individual samples were merged, because a sample with
	// count=1 does not have an explicitly set max). Therefore, it could only
	// happen if a sample is merged twice.
	out, err = engine.MergeInternalTimeSeriesData(out)
	if err != nil {
		t.Fatal(err)
	}

	if maxVal := out.Samples[0].Max; maxVal == nil {
		t.Fatal("Expected maximum of sample 0 to be non-nil after initial merge.")
	} else if *maxVal != -1.0 {
		t.Fatalf("Expected maximum of sample 0 to be -1.0 after initial merge, was %f", *maxVal)
	}
}
コード例 #3
0
// TestDiscardEarlierSamples verifies that only a single sample is kept for each
// sample period; earlier samples are discarded.
func TestDiscardEarlierSamples(t *testing.T) {
	defer leaktest.AfterTest(t)()
	ts := ts("test.series",
		tsdp(5*time.Hour+5*time.Minute, -1.0),
		tsdp(5*time.Hour+5*time.Minute, -2.0),
	)
	internal, err := ts.ToInternal(Resolution10s.KeyDuration(), Resolution10s.SampleDuration())
	if err != nil {
		t.Fatal(err)
	}

	out, err := engine.MergeInternalTimeSeriesData(internal...)
	if err != nil {
		t.Fatal(err)
	}

	if maxVal := out.Samples[0].Max; maxVal != nil {
		t.Fatal("Expected maximum of sample 0 to be nil; samples are no longer merged.")
	}
	if a, e := out.Samples[0].Sum, -2.0; a != e {
		t.Fatalf("Expected sum of sample 0 to be %f after initial merge, was %f", e, a)
	}
}