Skip to content

Commit 3e75148

Browse files
authored
fixed vcpus-to-memory ratio calc to use ceiling (#18)
* fixed vcpus-to-memory ratio calc to use ceiling * fix ratio calc when vcpus is 0
1 parent eab69ea commit 3e75148

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

pkg/selector/comparators.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,12 @@ func supportSyntaxToBool(instanceTypeSupport *string) *bool {
141141
}
142142

143143
func calculateVCpusToMemoryRatio(vcpusVal *int64, memoryVal *int64) *float64 {
144-
if vcpusVal == nil || memoryVal == nil {
144+
if vcpusVal == nil || *vcpusVal == 0 || memoryVal == nil {
145145
return nil
146146
}
147147
// normalize vcpus to a mebivcpu value
148-
return aws.Float64(float64(*memoryVal) / float64(*vcpusVal*1024))
148+
result := math.Ceil(float64(*memoryVal) / float64(*vcpusVal*1024))
149+
return &result
149150
}
150151

151152
// Slice helper function

pkg/selector/comparators_internal_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,22 @@ func TestCalculateVCpusToMemoryRatio(t *testing.T) {
217217
vcpus := aws.Int64(4)
218218
memory := aws.Int64(4096)
219219
ratio := calculateVCpusToMemoryRatio(vcpus, memory)
220-
h.Assert(t, *ratio == 1.00, "nil should evaluate to nil")
220+
h.Assert(t, *ratio == 1.00, "ratio should equal 1:1")
221221

222222
vcpus = aws.Int64(2)
223223
memory = aws.Int64(4096)
224224
ratio = calculateVCpusToMemoryRatio(vcpus, memory)
225-
h.Assert(t, *ratio == 2.00, "nil should evaluate to nil")
225+
h.Assert(t, *ratio == 2.00, "ratio should equal 1:2")
226226

227227
vcpus = aws.Int64(1)
228228
memory = aws.Int64(512)
229229
ratio = calculateVCpusToMemoryRatio(vcpus, memory)
230-
h.Assert(t, *ratio == 0.50, "nil should evaluate to nil")
230+
h.Assert(t, *ratio == 1.0, "ratio should take the ceiling which equals 1:1")
231+
232+
vcpus = aws.Int64(0)
233+
memory = aws.Int64(512)
234+
ratio = calculateVCpusToMemoryRatio(vcpus, memory)
235+
h.Assert(t, ratio == nil, "ratio should be nil when vcpus is 0")
231236
}
232237

233238
func TestCalculateVCpusToMemoryRatio_Nil(t *testing.T) {

0 commit comments

Comments
 (0)