Browse Source

Merge pull request #1537 from avrodrigues5/avr/improve-efficiency

Taking into account Summary Allocation not run for entire duration of SAS set window for RAM and CPU efficiency calculation
Niko Kovacevic 3 years ago
parent
commit
10ed813536
2 changed files with 16 additions and 16 deletions
  1. 14 14
      pkg/kubecost/summaryallocation.go
  2. 2 2
      pkg/kubecost/summaryallocation_test.go

+ 14 - 14
pkg/kubecost/summaryallocation.go

@@ -1179,23 +1179,23 @@ func (sas *SummaryAllocationSet) RAMEfficiency() float64 {
 	sas.RLock()
 	defer sas.RUnlock()
 
-	totalRAMBytesUsage := 0.0
-	totalRAMBytesRequest := 0.0
+	totalRAMBytesMinutesUsage := 0.0
+	totalRAMBytesMinutesRequest := 0.0
 	totalRAMCost := 0.0
 	for _, sa := range sas.SummaryAllocations {
 		if sa.IsIdle() {
 			continue
 		}
-		totalRAMBytesUsage += sa.RAMBytesUsageAverage
-		totalRAMBytesRequest += sa.RAMBytesRequestAverage
+		totalRAMBytesMinutesUsage += sa.RAMBytesUsageAverage * sa.Minutes()
+		totalRAMBytesMinutesRequest += sa.RAMBytesRequestAverage * sa.Minutes()
 		totalRAMCost += sa.RAMCost
 	}
 
-	if totalRAMBytesRequest > 0 {
-		return totalRAMBytesUsage / totalRAMBytesRequest
+	if totalRAMBytesMinutesRequest > 0 {
+		return totalRAMBytesMinutesUsage / totalRAMBytesMinutesRequest
 	}
 
-	if totalRAMBytesUsage == 0.0 || totalRAMCost == 0.0 {
+	if totalRAMBytesMinutesUsage == 0.0 || totalRAMCost == 0.0 {
 		return 0.0
 	}
 
@@ -1211,23 +1211,23 @@ func (sas *SummaryAllocationSet) CPUEfficiency() float64 {
 	sas.RLock()
 	defer sas.RUnlock()
 
-	totalCPUCoreUsage := 0.0
-	totalCPUCoreRequest := 0.0
+	totalCPUCoreMinutesUsage := 0.0
+	totalCPUCoreMinutesRequest := 0.0
 	totalCPUCost := 0.0
 	for _, sa := range sas.SummaryAllocations {
 		if sa.IsIdle() {
 			continue
 		}
-		totalCPUCoreUsage += sa.CPUCoreUsageAverage
-		totalCPUCoreRequest += sa.CPUCoreRequestAverage
+		totalCPUCoreMinutesUsage += sa.CPUCoreUsageAverage * sa.Minutes()
+		totalCPUCoreMinutesRequest += sa.CPUCoreRequestAverage * sa.Minutes()
 		totalCPUCost += sa.CPUCost
 	}
 
-	if totalCPUCoreRequest > 0 {
-		return totalCPUCoreUsage / totalCPUCoreRequest
+	if totalCPUCoreMinutesRequest > 0 {
+		return totalCPUCoreMinutesUsage / totalCPUCoreMinutesRequest
 	}
 
-	if totalCPUCoreUsage == 0.0 || totalCPUCost == 0.0 {
+	if totalCPUCoreMinutesUsage == 0.0 || totalCPUCost == 0.0 {
 		return 0.0
 	}
 

+ 2 - 2
pkg/kubecost/summaryallocation_test.go

@@ -421,7 +421,7 @@ func TestSummaryAllocationSet_RAMEfficiency(t *testing.T) {
 			expectedEfficiency: 0.65,
 		},
 		{
-			name:               "Check RAMEfficiency in presense of n idle allocation",
+			name:               "Check RAMEfficiency in presense of an idle allocation",
 			testsas:            sas6,
 			expectedEfficiency: 0.25,
 		},
@@ -648,7 +648,7 @@ func TestSummaryAllocationSet_CPUEfficiency(t *testing.T) {
 			expectedEfficiency: 0.50,
 		},
 		{
-			name:               "Check CPUEfficiency in presence of idle allocation",
+			name:               "Check CPUEfficiency in presence of an idle allocation",
 			testsas:            sas6,
 			expectedEfficiency: 0.30,
 		},