create sample from a constructor
This commit is contained in:
parent
f1fa77adc3
commit
0625caffa3
@ -26,6 +26,7 @@ import (
|
|||||||
type statsSummaryContainerHandlerWrapper struct {
|
type statsSummaryContainerHandlerWrapper struct {
|
||||||
handler ContainerHandler
|
handler ContainerHandler
|
||||||
currentSummary *info.ContainerStatsSummary
|
currentSummary *info.ContainerStatsSummary
|
||||||
|
prevStats *info.ContainerStats
|
||||||
totalMemoryUsage *big.Int
|
totalMemoryUsage *big.Int
|
||||||
numStats uint64
|
numStats uint64
|
||||||
sampler sampling.Sampler
|
sampler sampling.Sampler
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
package info
|
package info
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -186,17 +187,17 @@ type ContainerStatsSummary struct {
|
|||||||
// cumulative.
|
// cumulative.
|
||||||
// prev should be an earlier observation than current.
|
// prev should be an earlier observation than current.
|
||||||
// This method is not thread/goroutine safe.
|
// This method is not thread/goroutine safe.
|
||||||
func (self *ContainerStatsSummary) AddSample(prev, current *ContainerStats) {
|
func NewSample(prev, current *ContainerStats) (*ContainerStatsSample, error) {
|
||||||
if prev == nil || current == nil {
|
if prev == nil || current == nil {
|
||||||
return
|
return nil, fmt.Errorf("empty stats")
|
||||||
}
|
}
|
||||||
// Ignore this sample if it is incomplete
|
// Ignore this sample if it is incomplete
|
||||||
if prev.Cpu == nil || prev.Memory == nil || current.Cpu == nil || current.Memory == nil {
|
if prev.Cpu == nil || prev.Memory == nil || current.Cpu == nil || current.Memory == nil {
|
||||||
return
|
return nil, fmt.Errorf("incomplete stats")
|
||||||
}
|
}
|
||||||
// prev must be an early observation
|
// prev must be an early observation
|
||||||
if !current.Timestamp.After(prev.Timestamp) {
|
if !current.Timestamp.After(prev.Timestamp) {
|
||||||
return
|
return nil, fmt.Errorf("wrong stats order")
|
||||||
}
|
}
|
||||||
sample := new(ContainerStatsSample)
|
sample := new(ContainerStatsSample)
|
||||||
// Caculate the diff to get the CPU usage within the time interval.
|
// Caculate the diff to get the CPU usage within the time interval.
|
||||||
@ -204,8 +205,7 @@ func (self *ContainerStatsSummary) AddSample(prev, current *ContainerStats) {
|
|||||||
// Memory usage is current memory usage
|
// Memory usage is current memory usage
|
||||||
sample.Memory.Usage = current.Memory.Usage
|
sample.Memory.Usage = current.Memory.Usage
|
||||||
|
|
||||||
self.Samples = append(self.Samples, sample)
|
return sample, nil
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type uint64Slice []uint64
|
type uint64Slice []uint64
|
||||||
|
@ -85,9 +85,7 @@ func TestPercentiles(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddSampleNilStats(t *testing.T) {
|
func TestNewSampleNilStats(t *testing.T) {
|
||||||
s := &ContainerStatsSummary{}
|
|
||||||
|
|
||||||
stats := &ContainerStats{
|
stats := &ContainerStats{
|
||||||
Cpu: &CpuStats{},
|
Cpu: &CpuStats{},
|
||||||
Memory: &MemoryStats{},
|
Memory: &MemoryStats{},
|
||||||
@ -97,23 +95,19 @@ func TestAddSampleNilStats(t *testing.T) {
|
|||||||
stats.Cpu.Usage.System = uint64(2)
|
stats.Cpu.Usage.System = uint64(2)
|
||||||
stats.Cpu.Usage.User = uint64(8)
|
stats.Cpu.Usage.User = uint64(8)
|
||||||
stats.Memory.Usage = uint64(200)
|
stats.Memory.Usage = uint64(200)
|
||||||
s.AddSample(nil, stats)
|
|
||||||
|
|
||||||
if len(s.Samples) != 0 {
|
sample, err := NewSample(nil, stats)
|
||||||
t.Errorf("added an unexpected sample: %+v", s.Samples)
|
if err == nil {
|
||||||
|
t.Errorf("generated an unexpected sample: %+v", sample)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Samples = nil
|
sample, err = NewSample(nil, stats)
|
||||||
s.AddSample(stats, nil)
|
if err == nil {
|
||||||
|
t.Errorf("generated an unexpected sample: %+v", sample)
|
||||||
if len(s.Samples) != 0 {
|
|
||||||
t.Errorf("added an unexpected sample: %+v", s.Samples)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddSample(t *testing.T) {
|
func TestAddSample(t *testing.T) {
|
||||||
s := &ContainerStatsSummary{}
|
|
||||||
|
|
||||||
cpuPrevUsage := uint64(10)
|
cpuPrevUsage := uint64(10)
|
||||||
cpuCurrentUsage := uint64(15)
|
cpuCurrentUsage := uint64(15)
|
||||||
memCurrentUsage := uint64(200)
|
memCurrentUsage := uint64(200)
|
||||||
@ -139,24 +133,24 @@ func TestAddSample(t *testing.T) {
|
|||||||
current.Memory.Usage = memCurrentUsage
|
current.Memory.Usage = memCurrentUsage
|
||||||
current.Timestamp = prev.Timestamp.Add(1 * time.Second)
|
current.Timestamp = prev.Timestamp.Add(1 * time.Second)
|
||||||
|
|
||||||
s.AddSample(prev, current)
|
sample, err := NewSample(prev, current)
|
||||||
|
if err != nil {
|
||||||
if len(s.Samples) != 1 {
|
t.Errorf("should be able to generate a sample. but received error: %v", err)
|
||||||
t.Fatalf("unexpected samples: %+v", s.Samples)
|
}
|
||||||
|
if sample == nil {
|
||||||
|
t.Fatalf("nil sample and nil error. unexpected result!")
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.Samples[0].Memory.Usage != memCurrentUsage {
|
if sample.Memory.Usage != memCurrentUsage {
|
||||||
t.Errorf("wrong memory usage: %v. should be %v", s.Samples[0].Memory.Usage, memCurrentUsage)
|
t.Errorf("wrong memory usage: %v. should be %v", sample.Memory.Usage, memCurrentUsage)
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.Samples[0].Cpu.Usage != cpuCurrentUsage-cpuPrevUsage {
|
if sample.Cpu.Usage != cpuCurrentUsage-cpuPrevUsage {
|
||||||
t.Errorf("wrong CPU usage: %v. should be %v", s.Samples[0].Cpu.Usage, cpuCurrentUsage-cpuPrevUsage)
|
t.Errorf("wrong CPU usage: %v. should be %v", sample.Cpu.Usage, cpuCurrentUsage-cpuPrevUsage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddSampleIncompleteStats(t *testing.T) {
|
func TestAddSampleIncompleteStats(t *testing.T) {
|
||||||
s := &ContainerStatsSummary{}
|
|
||||||
|
|
||||||
cpuPrevUsage := uint64(10)
|
cpuPrevUsage := uint64(10)
|
||||||
cpuCurrentUsage := uint64(15)
|
cpuCurrentUsage := uint64(15)
|
||||||
memCurrentUsage := uint64(200)
|
memCurrentUsage := uint64(200)
|
||||||
@ -186,38 +180,30 @@ func TestAddSampleIncompleteStats(t *testing.T) {
|
|||||||
Cpu: prev.Cpu,
|
Cpu: prev.Cpu,
|
||||||
Memory: nil,
|
Memory: nil,
|
||||||
}
|
}
|
||||||
s.AddSample(stats, current)
|
sample, err := NewSample(stats, current)
|
||||||
if len(s.Samples) != 0 {
|
if err == nil {
|
||||||
t.Errorf("added an unexpected sample: %+v", s.Samples)
|
t.Errorf("generated an unexpected sample: %+v", sample)
|
||||||
}
|
}
|
||||||
s.Samples = nil
|
sample, err = NewSample(prev, stats)
|
||||||
|
if err == nil {
|
||||||
s.AddSample(prev, stats)
|
t.Errorf("generated an unexpected sample: %+v", sample)
|
||||||
if len(s.Samples) != 0 {
|
|
||||||
t.Errorf("added an unexpected sample: %+v", s.Samples)
|
|
||||||
}
|
}
|
||||||
s.Samples = nil
|
|
||||||
|
|
||||||
stats = &ContainerStats{
|
stats = &ContainerStats{
|
||||||
Cpu: nil,
|
Cpu: nil,
|
||||||
Memory: prev.Memory,
|
Memory: prev.Memory,
|
||||||
}
|
}
|
||||||
s.AddSample(stats, current)
|
sample, err = NewSample(stats, current)
|
||||||
if len(s.Samples) != 0 {
|
if err == nil {
|
||||||
t.Errorf("added an unexpected sample: %+v", s.Samples)
|
t.Errorf("generated an unexpected sample: %+v", sample)
|
||||||
}
|
}
|
||||||
s.Samples = nil
|
sample, err = NewSample(prev, stats)
|
||||||
|
if err == nil {
|
||||||
s.AddSample(prev, stats)
|
t.Errorf("generated an unexpected sample: %+v", sample)
|
||||||
if len(s.Samples) != 0 {
|
|
||||||
t.Errorf("added an unexpected sample: %+v", s.Samples)
|
|
||||||
}
|
}
|
||||||
s.Samples = nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddSampleWrongOrder(t *testing.T) {
|
func TestAddSampleWrongOrder(t *testing.T) {
|
||||||
s := &ContainerStatsSummary{}
|
|
||||||
|
|
||||||
cpuPrevUsage := uint64(10)
|
cpuPrevUsage := uint64(10)
|
||||||
cpuCurrentUsage := uint64(15)
|
cpuCurrentUsage := uint64(15)
|
||||||
memCurrentUsage := uint64(200)
|
memCurrentUsage := uint64(200)
|
||||||
@ -243,8 +229,8 @@ func TestAddSampleWrongOrder(t *testing.T) {
|
|||||||
current.Memory.Usage = memCurrentUsage
|
current.Memory.Usage = memCurrentUsage
|
||||||
current.Timestamp = prev.Timestamp.Add(1 * time.Second)
|
current.Timestamp = prev.Timestamp.Add(1 * time.Second)
|
||||||
|
|
||||||
s.AddSample(current, prev)
|
sample, err := NewSample(current, prev)
|
||||||
if len(s.Samples) != 0 {
|
if err == nil {
|
||||||
t.Errorf("added an unexpected sample: %+v", s.Samples)
|
t.Errorf("generated an unexpected sample: %+v", sample)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user