From e412c042d3af0a10fc7405d9f0b4627894182c67 Mon Sep 17 00:00:00 2001 From: Nan Deng Date: Tue, 17 Jun 2014 20:33:13 -0700 Subject: [PATCH] storage unit test percentiles --- storage/memory/memory_test.go | 8 ++++++- storage/test/storagetests.go | 41 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/storage/memory/memory_test.go b/storage/memory/memory_test.go index 98f9a6a8..ab75ad38 100644 --- a/storage/memory/memory_test.go +++ b/storage/memory/memory_test.go @@ -44,6 +44,12 @@ func TestSamplesWithoutSample(t *testing.T) { runStorageTest(test.StorageDriverTestSamplesWithoutSample, t) } -func TestPercentilessWithoutSample(t *testing.T) { +func TestPercentilesWithoutSample(t *testing.T) { runStorageTest(test.StorageDriverTestPercentilesWithoutSample, t) } + +func TestPercentiles(t *testing.T) { + N := 100 + driver := New(N, N) + test.StorageDriverTestPercentiles(driver, t) +} diff --git a/storage/test/storagetests.go b/storage/test/storagetests.go index e0f27f76..6253c482 100644 --- a/storage/test/storagetests.go +++ b/storage/test/storagetests.go @@ -168,3 +168,44 @@ func StorageDriverTestPercentilesWithoutSample(driver storage.StorageDriver, t * t.Errorf("There should be no percentiles") } } + +// The driver must be able to hold more than 100 samples +func StorageDriverTestPercentiles(driver storage.StorageDriver, t *testing.T) { + N := 100 + cpuTrace := make([]uint64, N) + memTrace := make([]uint64, N) + for i := 1; i < N+1; i++ { + cpuTrace[i-1] = uint64(i) + memTrace[i-1] = uint64(i) + } + + trace := buildTrace(cpuTrace, memTrace, 1*time.Second) + + ref := info.ContainerReference{ + Name: "container", + } + for _, stats := range trace { + driver.AddStats(ref, stats) + } + percentages := []int{ + 80, + 90, + 50, + } + percentiles, err := driver.Percentiles(ref.Name, percentages, percentages) + if err != nil { + t.Fatal(err) + } + for _, s := range percentiles.CpuUsagePercentiles { + // The value is out of the range of tolerance.. + if s.Value > uint64(s.Percentage+1) || s.Value < uint64(s.Percentage-1) { + t.Errorf("%v percentile data should be %v, but got %v", s.Percentage, s.Percentage, s.Value) + } + } + for _, s := range percentiles.MemoryUsagePercentiles { + // The value is out of the range of tolerance.. + if s.Value > uint64(s.Percentage+1) || s.Value < uint64(s.Percentage-1) { + t.Errorf("%v percentile data should be %v, but got %v", s.Percentage, s.Percentage, s.Value) + } + } +}