Skip to content

Commit b9bb804

Browse files
authored
Merge pull request #835 from alexandear/refactor/simplify-tests-with-min-max
refactor: replace math.Min/math.Max with min/max
2 parents cc8def2 + f78db71 commit b9bb804

File tree

6 files changed

+26
-29
lines changed

6 files changed

+26
-29
lines changed

pkg/trimaran/loadvariationriskbalancing/analysis.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ func computeScore(logger klog.Logger, rs *trimaran.ResourceStats, margin float64
3838
}
3939

4040
// make sure values are within bounds
41-
rs.Req = math.Max(rs.Req, 0)
42-
rs.UsedAvg = math.Max(math.Min(rs.UsedAvg, rs.Capacity), 0)
43-
rs.UsedStdev = math.Max(math.Min(rs.UsedStdev, rs.Capacity), 0)
41+
rs.Req = max(rs.Req, 0)
42+
rs.UsedAvg = max(min(rs.UsedAvg, rs.Capacity), 0)
43+
rs.UsedStdev = max(min(rs.UsedStdev, rs.Capacity), 0)
4444

4545
// calculate average and deviation factors
4646
mu, sigma := trimaran.GetMuSigma(rs)
@@ -51,7 +51,7 @@ func computeScore(logger klog.Logger, rs *trimaran.ResourceStats, margin float64
5151
}
5252
// apply multiplier
5353
sigma *= margin
54-
sigma = math.Max(math.Min(sigma, 1), 0)
54+
sigma = max(min(sigma, 1), 0)
5555

5656
// evaluate overall risk factor
5757
risk := (mu + sigma) / 2

pkg/trimaran/loadvariationriskbalancing/loadvariationriskbalancing.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ func (pl *LoadVariationRiskBalancing) Score(ctx context.Context, cycleState *fra
113113
// calculate total score
114114
var totalScore float64 = 0
115115
if memoryOK && cpuOK {
116-
totalScore = math.Min(memoryScore, cpuScore)
116+
totalScore = min(memoryScore, cpuScore)
117117
} else {
118-
totalScore = math.Max(memoryScore, cpuScore)
118+
totalScore = max(memoryScore, cpuScore)
119119
}
120120
score = int64(math.Round(totalScore))
121121
logger.V(6).Info("Calculating totalScore", "pod", klog.KObj(pod), "nodeName", nodeName, "totalScore", score)

pkg/trimaran/lowriskovercommitment/beta.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (b *BetaDistribution) MatchMoments(m1, m2 float64) bool {
110110
return false
111111
}
112112
temp := (m1 * (1 - m1) / variance) - 1
113-
temp = math.Max(temp, math.SmallestNonzeroFloat64)
113+
temp = max(temp, math.SmallestNonzeroFloat64)
114114
b.alpha = m1 * temp
115115
b.beta = (1 - m1) * temp
116116
return b.computeMoments()

pkg/trimaran/lowriskovercommitment/lowriskovercommitment.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (pl *LowRiskOverCommitment) computeRank(logger klog.Logger, metrics []watch
163163
nodeRequestsAndLimits := trimaran.GetNodeRequestsAndLimits(logger, nodeInfo.Pods, node, pod, podRequests, podLimits)
164164
riskCPU := pl.computeRisk(logger, metrics, v1.ResourceCPU, watcher.CPU, node, nodeRequestsAndLimits)
165165
riskMemory := pl.computeRisk(logger, metrics, v1.ResourceMemory, watcher.Memory, node, nodeRequestsAndLimits)
166-
rank := 1 - math.Max(riskCPU, riskMemory)
166+
rank := 1 - max(riskCPU, riskMemory)
167167

168168
logger.V(6).Info("Node rank", "nodeName", node.GetName(), "riskCPU", riskCPU, "riskMemory", riskMemory, "rank", rank)
169169

@@ -220,11 +220,11 @@ func (pl *LowRiskOverCommitment) computeRisk(logger klog.Logger, metrics []watch
220220
// adjust standard deviation due to data smoothing
221221
sigma *= math.Pow(float64(pl.args.SmoothingWindowSize), 0.5)
222222
// limit the standard deviation close to the allowed maximum for the beta distribution
223-
sigma = math.Min(sigma, math.Sqrt(GetMaxVariance(mu)*MaxVarianceAllowance))
223+
sigma = min(sigma, math.Sqrt(GetMaxVariance(mu)*MaxVarianceAllowance))
224224

225225
// calculate area under beta probability curve beyond total allocated, as overuse risk measure
226226
allocThreshold := float64(requestMinusPod) / float64(capacity)
227-
allocThreshold = math.Min(math.Max(allocThreshold, 0), 1)
227+
allocThreshold = min(max(allocThreshold, 0), 1)
228228
allocProb, fitDistribution := ComputeProbability(mu, sigma, allocThreshold)
229229
if fitDistribution != nil {
230230
klog.V(6).InfoS("FitDistribution", "node", klog.KObj(node), "resource", resourceName, "dist", fitDistribution.Print())
@@ -238,7 +238,7 @@ func (pl *LowRiskOverCommitment) computeRisk(logger klog.Logger, metrics []watch
238238
limitProb := fitDistribution.DistributionFunction(limitThreshold)
239239
if limitProb > 0 {
240240
allocProb /= limitProb
241-
allocProb = math.Min(math.Max(allocProb, 0), 1)
241+
allocProb = min(max(allocProb, 0), 1)
242242
}
243243
}
244244
}
@@ -252,7 +252,7 @@ func (pl *LowRiskOverCommitment) computeRisk(logger klog.Logger, metrics []watch
252252
// combine two components of risk into a total risk as a weighted sum
253253
w := pl.riskLimitWeightsMap[resourceName]
254254
totalRisk = w*riskLimit + (1-w)*riskLoad
255-
totalRisk = math.Min(math.Max(totalRisk, 0), 1)
255+
totalRisk = min(max(totalRisk, 0), 1)
256256
return totalRisk
257257
}
258258

pkg/trimaran/resourcestats.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ limitations under the License.
1717
package trimaran
1818

1919
import (
20-
"math"
21-
2220
"github.com/paypal/load-watcher/pkg/watcher"
2321
v1 "k8s.io/api/core/v1"
2422
"k8s.io/klog/v2"
@@ -80,9 +78,9 @@ func GetMuSigma(rs *ResourceStats) (float64, float64) {
8078
return 0, 0
8179
}
8280
mu := (rs.UsedAvg + rs.Req) / rs.Capacity
83-
mu = math.Max(math.Min(mu, 1), 0)
81+
mu = max(min(mu, 1), 0)
8482
sigma := rs.UsedStdev / rs.Capacity
85-
sigma = math.Max(math.Min(sigma, 1), 0)
83+
sigma = max(min(sigma, 1), 0)
8684
return mu, sigma
8785
}
8886

pkg/trimaran/resourcestats_test.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package trimaran
1818

1919
import (
2020
"context"
21-
"math"
2221
"reflect"
2322
"strconv"
2423
"testing"
@@ -522,12 +521,12 @@ func TestGetNodeRequestsAndLimits(t *testing.T) {
522521
},
523522
want: &NodeRequestsAndLimits{
524523
NodeRequest: &framework.Resource{
525-
MilliCPU: int64(math.Min(float64(GetResourceRequested(pod).MilliCPU), float64(capCpu))),
526-
Memory: int64(math.Min(float64(GetResourceRequested(pod).Memory), float64(capMem))),
524+
MilliCPU: min(GetResourceRequested(pod).MilliCPU, capCpu),
525+
Memory: min(GetResourceRequested(pod).Memory, capMem),
527526
},
528527
NodeLimit: &framework.Resource{
529-
MilliCPU: int64(math.Max(float64(GetResourceRequested(pod).MilliCPU), float64(GetResourceLimits(pod).MilliCPU))),
530-
Memory: int64(math.Max(float64(GetResourceRequested(pod).Memory), float64(GetResourceLimits(pod).Memory))),
528+
MilliCPU: max(GetResourceRequested(pod).MilliCPU, GetResourceLimits(pod).MilliCPU),
529+
Memory: max(GetResourceRequested(pod).Memory, GetResourceLimits(pod).Memory),
531530
},
532531
NodeRequestMinusPod: &framework.Resource{
533532
MilliCPU: 0,
@@ -555,16 +554,16 @@ func TestGetNodeRequestsAndLimits(t *testing.T) {
555554
},
556555
want: &NodeRequestsAndLimits{
557556
NodeRequest: &framework.Resource{
558-
MilliCPU: int64(math.Min(float64(GetResourceRequested(pod4).MilliCPU), float64(capCpu))),
559-
Memory: int64(math.Min(float64(GetResourceRequested(pod4).Memory), float64(capMem))),
557+
MilliCPU: min(GetResourceRequested(pod4).MilliCPU, capCpu),
558+
Memory: min(GetResourceRequested(pod4).Memory, capMem),
560559
},
561560
NodeLimit: &framework.Resource{
562-
MilliCPU: int64(math.Min(
563-
math.Max(float64(GetResourceRequested(pod4).MilliCPU), float64(GetResourceLimits(pod4).MilliCPU)),
564-
float64(capCpu))),
565-
Memory: int64(math.Min(
566-
math.Max(float64(GetResourceRequested(pod4).Memory), float64(GetResourceLimits(pod4).Memory)),
567-
float64(capMem))),
561+
MilliCPU: min(
562+
max(GetResourceRequested(pod4).MilliCPU, GetResourceLimits(pod4).MilliCPU),
563+
capCpu),
564+
Memory: min(
565+
max(GetResourceRequested(pod4).Memory, GetResourceLimits(pod4).Memory),
566+
capMem),
568567
},
569568
NodeRequestMinusPod: &framework.Resource{
570569
MilliCPU: 0,

0 commit comments

Comments
 (0)