Atlas › Test
TestUpdateHysteresis
Exact test identity: go.temporal.io/server/common/number/TestCompact8Suite/TestUpdateHysteresis
- Package
go.temporal.io/server/common/number
- Suite / test hierarchy
TestCompact8Suite/TestUpdateHysteresis
- Test
TestUpdateHysteresis
- Introduced at
- compact8.go ×1 Frontier kind: Joint frontier
- Covered ranges
- 13
- Covered lines
- 42
- Covered files
- 1
Covered source
Expand a file to inspect source; the > gutter marks covered lines.
go.temporal.io/server/common/number/compact8.go 42 covered LOC · 13 ranges
Open complete file
19
20
// DecodeCompact8 converts a Compact8 value to an int64.
21
>
func DecodeCompact8(b Compact8) int64 {
compact8.go
22
>
if b == 0 {
23
return 0
24
}
26
>
m := int(b % 12)
27
>
if e == 0 {
28
return int64(m) << (compact8offset + 1)
29
}
30
>
return int64(12+m) << (e + compact8offset)
compact8.go
31
}
32
34
// The value is rounded down to the nearest representable value.
35
// Negative values go to 0 and values above the maximum representable go to 255.
36
>
func EncodeCompact8(value int64) Compact8 {
compact8.go
37
>
if value <= 0 {
38
return 0
39
}
40
42
>
bitLen := bits.Len64(uval)
43
>
44
>
// Find shift such that uval >> shift is in [12, 23].
45
>
// This extracts the significand for the normalized representation.
46
>
shift := max(bitLen-5, 0)
47
>
sig := int(uval >> uint(shift))
48
>
if sig >= 24 {
49
>
shift++
50
>
sig = int(uval >> uint(shift))
51
>
}
52
54
>
55
>
if e >= 1 {
57
>
b := e*12 + m
58
>
if b > 255 {
59
return 255
60
}
62
}
63
76
// it sticks to prev unless the new code is significantly closer. This prevents
77
// oscillation when the underlying value fluctuates near a bucket boundary.
78
>
func UpdateCompact8(value int64, prev Compact8) Compact8 {
compact8.go
79
>
newCode := EncodeCompact8(value)
80
>
if newCode == prev {
82
>
}
83
>
newDist := value - DecodeCompact8(newCode) // always >= 0 (round-down)
compact8.go
84
>
oldDist := DecodeCompact8(prev) - value
85
>
if oldDist < 0 {
86
>
oldDist = -oldDist
87
>
}
88
// Require the new code to be closer by at least half a bucket width
89
// (at the smaller of the two exponent levels). This shifts the
90
// transition point from the midpoint to the 3/4 mark of the gap,
91
// creating a dead zone that prevents chatter.
92
>
e := max(1, min(int(prev/12), int(newCode/12)))
compact8.go
93
>
margin := int64(1) << (e + compact8offset - 1)
94
>
if newDist < oldDist-margin {
95
>
return newCode
96
>
}
98
}