mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 05:57:25 +00:00
Merge pull request #22052 from mwielgus/hpa-average
Auto commit by PR queue bot
This commit is contained in:
commit
18d8e7b756
@ -74,7 +74,7 @@ type HeapsterMetricsClient struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var averageFunction = func(metrics heapster.MetricResultList) (intAndFloat, int, time.Time) {
|
var averageFunction = func(metrics heapster.MetricResultList) (intAndFloat, int, time.Time) {
|
||||||
sum, count, timestamp := calculateSumFromLatestSample(metrics)
|
sum, count, timestamp := calculateSumFromTimeSample(metrics, time.Minute)
|
||||||
result := intAndFloat{0, 0}
|
result := intAndFloat{0, 0}
|
||||||
if count > 0 {
|
if count > 0 {
|
||||||
result.intValue = sum.intValue / int64(count)
|
result.intValue = sum.intValue / int64(count)
|
||||||
@ -218,7 +218,7 @@ func (h *HeapsterMetricsClient) getForPods(metricSpec metricDefinition, namespac
|
|||||||
return &sum, timestamp, nil
|
return &sum, timestamp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func calculateSumFromLatestSample(metrics heapster.MetricResultList) (sum intAndFloat, count int, timestamp time.Time) {
|
func calculateSumFromTimeSample(metrics heapster.MetricResultList, duration time.Duration) (sum intAndFloat, count int, timestamp time.Time) {
|
||||||
sum = intAndFloat{0, 0}
|
sum = intAndFloat{0, 0}
|
||||||
count = 0
|
count = 0
|
||||||
timestamp = time.Time{}
|
timestamp = time.Time{}
|
||||||
@ -236,12 +236,29 @@ func calculateSumFromLatestSample(metrics heapster.MetricResultList) (sum intAnd
|
|||||||
if oldest == nil || newest.Timestamp.Before(*oldest) {
|
if oldest == nil || newest.Timestamp.Before(*oldest) {
|
||||||
oldest = &newest.Timestamp
|
oldest = &newest.Timestamp
|
||||||
}
|
}
|
||||||
|
intervalSum := intAndFloat{0, 0}
|
||||||
|
intSumCount := 0
|
||||||
|
floatSumCount := 0
|
||||||
|
for _, metricPoint := range metrics.Metrics {
|
||||||
|
if metricPoint.Timestamp.Add(duration).After(newest.Timestamp) {
|
||||||
|
intervalSum.intValue += int64(metricPoint.Value)
|
||||||
|
intSumCount++
|
||||||
|
if metricPoint.FloatValue != nil {
|
||||||
|
intervalSum.floatValue += *metricPoint.FloatValue
|
||||||
|
floatSumCount++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if newest.FloatValue == nil {
|
if newest.FloatValue == nil {
|
||||||
sum.intValue += int64(newest.Value)
|
if intSumCount > 0 {
|
||||||
sum.floatValue += float64(newest.Value)
|
sum.intValue += int64(intervalSum.intValue / int64(intSumCount))
|
||||||
|
sum.floatValue += float64(intervalSum.intValue / int64(intSumCount))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
sum.intValue += int64(*newest.FloatValue)
|
if floatSumCount > 0 {
|
||||||
sum.floatValue += *newest.FloatValue
|
sum.intValue += int64(intervalSum.floatValue / float64(floatSumCount))
|
||||||
|
sum.floatValue += intervalSum.floatValue / float64(floatSumCount)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
|
@ -413,4 +413,42 @@ func TestCPUEmptyMetricsForOnePod(t *testing.T) {
|
|||||||
tc.runTest(t)
|
tc.runTest(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAggregateSum(t *testing.T) {
|
||||||
|
//calculateSumFromTimeSample(metrics heapster.MetricResultList, duration time.Duration) (sum intAndFloat, count int, timestamp time.Time) {
|
||||||
|
now := time.Now()
|
||||||
|
result := heapster.MetricResultList{
|
||||||
|
Items: []heapster.MetricResult{
|
||||||
|
{
|
||||||
|
Metrics: []heapster.MetricPoint{
|
||||||
|
{now, 50, nil},
|
||||||
|
{now.Add(-15 * time.Second), 100, nil},
|
||||||
|
{now.Add(-60 * time.Second), 100000, nil}},
|
||||||
|
LatestTimestamp: now,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
sum, cnt, _ := calculateSumFromTimeSample(result, time.Minute)
|
||||||
|
assert.Equal(t, int64(75), sum.intValue)
|
||||||
|
assert.InEpsilon(t, 75.0, sum.floatValue, 0.1)
|
||||||
|
assert.Equal(t, 1, cnt)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAggregateSumSingle(t *testing.T) {
|
||||||
|
now := time.Now()
|
||||||
|
result := heapster.MetricResultList{
|
||||||
|
Items: []heapster.MetricResult{
|
||||||
|
{
|
||||||
|
Metrics: []heapster.MetricPoint{
|
||||||
|
{now, 50, nil},
|
||||||
|
{now.Add(-65 * time.Second), 100000, nil}},
|
||||||
|
LatestTimestamp: now,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
sum, cnt, _ := calculateSumFromTimeSample(result, time.Minute)
|
||||||
|
assert.Equal(t, int64(50), sum.intValue)
|
||||||
|
assert.InEpsilon(t, 50.0, sum.floatValue, 0.1)
|
||||||
|
assert.Equal(t, 1, cnt)
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: add proper tests for request
|
// TODO: add proper tests for request
|
||||||
|
Loading…
Reference in New Issue
Block a user