Merge pull request #103751 from y-tag/approx-float64
fix AsApproximateFloat64() for BinarySI
This commit is contained in:
commit
c0c7039f3a
@ -71,7 +71,7 @@ func Test_podResourceCollector_Handler(t *testing.T) {
|
|||||||
"custom": resource.MustParse("0"),
|
"custom": resource.MustParse("0"),
|
||||||
},
|
},
|
||||||
Limits: v1.ResourceList{
|
Limits: v1.ResourceList{
|
||||||
"memory": resource.MustParse("2G"),
|
"memory": resource.MustParse("2.5Gi"),
|
||||||
"custom": resource.MustParse("6"),
|
"custom": resource.MustParse("6"),
|
||||||
},
|
},
|
||||||
}},
|
}},
|
||||||
@ -95,7 +95,7 @@ func Test_podResourceCollector_Handler(t *testing.T) {
|
|||||||
expected := `# HELP kube_pod_resource_limit [ALPHA] Resources limit for workloads on the cluster, broken down by pod. This shows the resource usage the scheduler and kubelet expect per pod for resources along with the unit for the resource if any.
|
expected := `# HELP kube_pod_resource_limit [ALPHA] Resources limit for workloads on the cluster, broken down by pod. This shows the resource usage the scheduler and kubelet expect per pod for resources along with the unit for the resource if any.
|
||||||
# TYPE kube_pod_resource_limit gauge
|
# TYPE kube_pod_resource_limit gauge
|
||||||
kube_pod_resource_limit{namespace="test",node="node-one",pod="foo",priority="",resource="custom",scheduler="",unit=""} 6
|
kube_pod_resource_limit{namespace="test",node="node-one",pod="foo",priority="",resource="custom",scheduler="",unit=""} 6
|
||||||
kube_pod_resource_limit{namespace="test",node="node-one",pod="foo",priority="",resource="memory",scheduler="",unit="bytes"} 2e+09
|
kube_pod_resource_limit{namespace="test",node="node-one",pod="foo",priority="",resource="memory",scheduler="",unit="bytes"} 2.68435456e+09
|
||||||
# HELP kube_pod_resource_request [ALPHA] Resources requested by workloads on the cluster, broken down by pod. This shows the resource usage the scheduler and kubelet expect per pod for resources along with the unit for the resource if any.
|
# HELP kube_pod_resource_request [ALPHA] Resources requested by workloads on the cluster, broken down by pod. This shows the resource usage the scheduler and kubelet expect per pod for resources along with the unit for the resource if any.
|
||||||
# TYPE kube_pod_resource_request gauge
|
# TYPE kube_pod_resource_request gauge
|
||||||
kube_pod_resource_request{namespace="test",node="node-one",pod="foo",priority="",resource="cpu",scheduler="",unit="cores"} 2
|
kube_pod_resource_request{namespace="test",node="node-one",pod="foo",priority="",resource="cpu",scheduler="",unit="cores"} 2
|
||||||
|
@ -460,17 +460,7 @@ func (q *Quantity) AsApproximateFloat64() float64 {
|
|||||||
return base
|
return base
|
||||||
}
|
}
|
||||||
|
|
||||||
// multiply by the appropriate exponential scale
|
return base * math.Pow10(exponent)
|
||||||
switch q.Format {
|
|
||||||
case DecimalExponent, DecimalSI:
|
|
||||||
return base * math.Pow10(exponent)
|
|
||||||
default:
|
|
||||||
// fast path for exponents that can fit in 64 bits
|
|
||||||
if exponent > 0 && exponent < 7 {
|
|
||||||
return base * float64(int64(1)<<(exponent*10))
|
|
||||||
}
|
|
||||||
return base * math.Pow(2, float64(exponent*10))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AsInt64 returns a representation of the current value as an int64 if a fast conversion
|
// AsInt64 returns a representation of the current value as an int64 if a fast conversion
|
||||||
|
@ -1207,11 +1207,11 @@ func TestQuantityAsApproximateFloat64(t *testing.T) {
|
|||||||
{decQuantity(1024, 0, BinarySI), 1024},
|
{decQuantity(1024, 0, BinarySI), 1024},
|
||||||
{decQuantity(8*1024, 0, BinarySI), 8 * 1024},
|
{decQuantity(8*1024, 0, BinarySI), 8 * 1024},
|
||||||
{decQuantity(7*1024*1024, 0, BinarySI), 7 * 1024 * 1024},
|
{decQuantity(7*1024*1024, 0, BinarySI), 7 * 1024 * 1024},
|
||||||
{decQuantity(7*1024*1024, 1, BinarySI), (7 * 1024 * 1024) * 1024},
|
{decQuantity(7*1024*1024, 1, BinarySI), (7 * 1024 * 1024) * 10},
|
||||||
{decQuantity(7*1024*1024, 4, BinarySI), (7 * 1024 * 1024) * (1024 * 1024 * 1024 * 1024)},
|
{decQuantity(7*1024*1024, 4, BinarySI), (7 * 1024 * 1024) * 10000},
|
||||||
{decQuantity(7*1024*1024, 8, BinarySI), (7 * 1024 * 1024) * (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024)},
|
{decQuantity(7*1024*1024, 8, BinarySI), (7 * 1024 * 1024) * 100000000},
|
||||||
{decQuantity(7*1024*1024, -1, BinarySI), (7 * 1024 * 1024) / float64(1024)},
|
{decQuantity(7*1024*1024, -1, BinarySI), (7 * 1024 * 1024) * math.Pow10(-1)}, // '* Pow10' and '/ float(10)' do not round the same way
|
||||||
{decQuantity(7*1024*1024, -8, BinarySI), (7 * 1024 * 1024) / float64(1024*1024*1024*1024*1024*1024*1024*1024)},
|
{decQuantity(7*1024*1024, -8, BinarySI), (7 * 1024 * 1024) / float64(100000000)},
|
||||||
|
|
||||||
{decQuantity(1024, 0, DecimalSI), 1024},
|
{decQuantity(1024, 0, DecimalSI), 1024},
|
||||||
{decQuantity(8*1024, 0, DecimalSI), 8 * 1024},
|
{decQuantity(8*1024, 0, DecimalSI), 8 * 1024},
|
||||||
@ -1260,6 +1260,40 @@ func TestQuantityAsApproximateFloat64(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStringQuantityAsApproximateFloat64(t *testing.T) {
|
||||||
|
table := []struct {
|
||||||
|
in string
|
||||||
|
out float64
|
||||||
|
}{
|
||||||
|
{"2Ki", 2048},
|
||||||
|
{"1.1Ki", 1126.4e+0},
|
||||||
|
{"1Mi", 1.048576e+06},
|
||||||
|
{"2Gi", 2.147483648e+09},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, item := range table {
|
||||||
|
t.Run(item.in, func(t *testing.T) {
|
||||||
|
in, err := ParseQuantity(item.in)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
out := in.AsApproximateFloat64()
|
||||||
|
if out != item.out {
|
||||||
|
t.Fatalf("expected %v, got %v", item.out, out)
|
||||||
|
}
|
||||||
|
if in.d.Dec != nil {
|
||||||
|
if i, ok := in.AsInt64(); ok {
|
||||||
|
q := intQuantity(i, 0, in.Format)
|
||||||
|
out := q.AsApproximateFloat64()
|
||||||
|
if out != item.out {
|
||||||
|
t.Fatalf("as int quantity: expected %v, got %v", item.out, out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func benchmarkQuantities() []Quantity {
|
func benchmarkQuantities() []Quantity {
|
||||||
return []Quantity{
|
return []Quantity{
|
||||||
intQuantity(1024*1024*1024, 0, BinarySI),
|
intQuantity(1024*1024*1024, 0, BinarySI),
|
||||||
|
Loading…
Reference in New Issue
Block a user