Skip to content

Commit 6ea7bc3

Browse files
committed
asim: deflake TestThrashing
Previously, tsFromFunc generated 100 values using math.Sin(math.Pi * float64(tick) / 10) to produce oscillating numbers for thrashing tests. Small differences in the least significant digits (up to ~1e-10) can occur across architectures due to slight variations in sin computation, but these are negligible. This commit attempts to deflake this test by rounding the numbers generated to 6 digits.
1 parent 1905ae4 commit 6ea7bc3

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

pkg/kv/kvserver/asim/history/testdata/TestThrashing/oscillate

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
echo
22
----
33
An example that the thrashing percentage can arbitarily exceed 200%.
4-
input: [0 0.3090169943749474 0.5877852522924731 0.8090169943749475 0.9510565162951535 1 0.9510565162951536 0.8090169943749475 0.5877852522924732 0.3090169943749475 1.2246467991473515e-16 -0.3090169943749469 -0.587785252292473 -0.8090169943749473 -0.9510565162951535 -1 -0.9510565162951536 -0.8090169943749476 -0.5877852522924732 -0.3090169943749476 -2.449293598294703e-16 0.3090169943749472 0.5877852522924722 0.8090169943749472 0.9510565162951535 1 0.9510565162951538 0.8090169943749476 0.5877852522924734 0.3090169943749478 3.6739403974420544e-16 -0.30901699437494706 -0.5877852522924728 -0.8090169943749472 -0.9510565162951534 -1 -0.9510565162951538 -0.8090169943749477 -0.5877852522924735 -0.3090169943749479 -4.898587196589406e-16 0.30901699437494523 0.5877852522924728 0.8090169943749471 0.9510565162951529 1 0.9510565162951538 0.8090169943749468 0.5877852522924737 0.30901699437494967 6.123233995736757e-16 -0.3090169943749451 -0.5877852522924726 -0.8090169943749481 -0.9510565162951534 -1 -0.9510565162951539 -0.8090169943749489 -0.5877852522924737 -0.3090169943749498 -7.347880794884109e-16 0.3090169943749484 0.5877852522924725 0.809016994374948 0.9510565162951533 1 0.9510565162951539 0.809016994374949 0.5877852522924738 0.3090169943749465 8.57252759403146e-16 -0.3090169943749483 -0.5877852522924724 -0.8090169943749458 -0.9510565162951533 -1 -0.9510565162951539 -0.809016994374947 -0.5877852522924739 -0.3090169943749467 -9.797174393178812e-16 0.3090169943749448 0.5877852522924695 0.8090169943749479 0.9510565162951533 1 0.951056516295154 0.809016994374949 0.5877852522924769 0.3090169943749468 1.1021821192326163e-15 -0.3090169943749447 -0.5877852522924722 -0.8090169943749458 -0.9510565162951543 -1 -0.951056516295154 -0.8090169943749491 -0.587785252292477 -0.3090169943749469]
4+
input: [0 0.309017 0.587785 0.809017 0.951057 1 0.951057 0.809017 0.587785 0.309017 0 -0.309017 -0.587785 -0.809017 -0.951057 -1 -0.951057 -0.809017 -0.587785 -0.309017 -0 0.309017 0.587785 0.809017 0.951057 1 0.951057 0.809017 0.587785 0.309017 0 -0.309017 -0.587785 -0.809017 -0.951057 -1 -0.951057 -0.809017 -0.587785 -0.309017 -0 0.309017 0.587785 0.809017 0.951057 1 0.951057 0.809017 0.587785 0.309017 0 -0.309017 -0.587785 -0.809017 -0.951057 -1 -0.951057 -0.809017 -0.587785 -0.309017 -0 0.309017 0.587785 0.809017 0.951057 1 0.951057 0.809017 0.587785 0.309017 0 -0.309017 -0.587785 -0.809017 -0.951057 -1 -0.951057 -0.809017 -0.587785 -0.309017 -0 0.309017 0.587785 0.809017 0.951057 1 0.951057 0.809017 0.587785 0.309017 0 -0.309017 -0.587785 -0.809017 -0.951057 -1 -0.951057 -0.809017 -0.587785 -0.309017]
55
1.00 ┤ ╭╮ ╭╮ ╭╮ ╭╮ ╭─╮
66
0.80 ┤╭╯│ ╭╯│ ╭╯│ ╭╯│ │ │
77
0.60 ┤│ ╰╮ │ ╰╮ │ ╰╮ │ │ │ │

pkg/kv/kvserver/asim/history/thrashing_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package history
88
import (
99
"fmt"
1010
"math"
11+
"strconv"
1112
"strings"
1213
"testing"
1314
"testing/quick"
@@ -22,9 +23,14 @@ import (
2223
)
2324

2425
func tsFromFunc(ticks int, f func(tick int) float64) []float64 {
26+
roundToSixDigits := func(num float64) float64 {
27+
formatted := fmt.Sprintf("%.6f", num)
28+
result, _ := strconv.ParseFloat(formatted, 64)
29+
return result
30+
}
2531
vs := make([]float64, ticks)
2632
for i := 0; i < ticks; i++ {
27-
vs[i] = f(i)
33+
vs[i] = roundToSixDigits(f(i))
2834
}
2935
return vs
3036
}

0 commit comments

Comments
 (0)