From a1cb5bf4efb0610f7e476cd204dad888a4f631da Mon Sep 17 00:00:00 2001 From: Nan Deng Date: Thu, 3 Jul 2014 20:55:56 +0000 Subject: [PATCH] allow the storage to store some inaccurate time --- storage/influxdb/influxdb_test.go | 12 ++++++ storage/test/storagetests.go | 65 ++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/storage/influxdb/influxdb_test.go b/storage/influxdb/influxdb_test.go index 515cb732..fb8a8b42 100644 --- a/storage/influxdb/influxdb_test.go +++ b/storage/influxdb/influxdb_test.go @@ -32,6 +32,18 @@ func runStorageTest(f func(storage.StorageDriver, *testing.T), t *testing.T) { password := "root" hostname := "localhost:8086" percentilesDuration := 10 * time.Minute + rootConfig := &influxdb.ClientConfig{ + Host: hostname, + Username: username, + Password: password, + IsSecure: false, + } + rootClient, err := influxdb.NewClient(rootConfig) + if err != nil { + t.Fatal(err) + } + // create the data base first. + rootClient.CreateDatabase(database) config := &influxdb.ClientConfig{ Host: hostname, Username: username, diff --git a/storage/test/storagetests.go b/storage/test/storagetests.go index d953f2b9..c18ceede 100644 --- a/storage/test/storagetests.go +++ b/storage/test/storagetests.go @@ -53,6 +53,67 @@ func buildTrace(cpu, mem []uint64, duration time.Duration) []*info.ContainerStat return ret } +func timeEq(t1, t2 time.Time, tolerance time.Duration) bool { + // t1 should not be later than t2 + if t1.After(t2) { + t1, t2 = t2, t1 + } + diff := t2.Sub(t1) + if diff < tolerance { + return true + } + return false +} + +func durationEq(a, b time.Duration, tolerance time.Duration) bool { + if a > b { + a, b = b, a + } + diff := a - b + if diff < tolerance { + return true + } + return false +} + +const ( + // 10ms, i.e. 0.01s + timePrecision time.Duration = 10000000 +) + +// This function is useful because we do not require precise time +// representation. +func statsEq(a, b *info.ContainerStats) bool { + if !timeEq(a.Timestamp, b.Timestamp, timePrecision) { + return false + } + if !reflect.DeepEqual(a.Cpu, b.Cpu) { + return false + } + if !reflect.DeepEqual(a.Memory, b.Memory) { + return false + } + return true +} + +// This function is useful because we do not require precise time +// representation. +func sampleEq(a, b *info.ContainerStatsSample) bool { + if !timeEq(a.Timestamp, b.Timestamp, timePrecision) { + return false + } + if !durationEq(a.Duration, b.Duration, timePrecision) { + return false + } + if !reflect.DeepEqual(a.Cpu, b.Cpu) { + return false + } + if !reflect.DeepEqual(a.Memory, b.Memory) { + return false + } + return true +} + func samplesInTrace(samples []*info.ContainerStatsSample, cpuTrace, memTrace []uint64, samplePeriod time.Duration, t *testing.T) { for _, sample := range samples { if sample.Duration != samplePeriod { @@ -334,7 +395,7 @@ func StorageDriverTestRetrievePartialRecentStats(driver storage.StorageDriver, t for _, r := range recentStats { found := false for _, s := range actualRecentStats { - if reflect.DeepEqual(s, r) { + if statsEq(s, r) { found = true } } @@ -380,7 +441,7 @@ func StorageDriverTestRetrieveAllRecentStats(driver storage.StorageDriver, t *te for _, r := range recentStats { found := false for _, s := range actualRecentStats { - if reflect.DeepEqual(s, r) { + if statsEq(s, r) { found = true } }