summaryrefslogtreecommitdiff
path: root/tests/support
diff options
context:
space:
mode:
authorsundb <sundbcn@gmail.com>2021-04-01 13:20:15 +0800
committerGitHub <noreply@github.com>2021-04-01 08:20:15 +0300
commit569a3f4548cc525aac8bd246da213eb48a17f0e1 (patch)
treeca9e4173847afcb6aa9688c76c154d2a6f845a44 /tests/support
parent636aa8de76f07dd0f9b3c6d6e72f334a881a50f2 (diff)
downloadredis-569a3f4548cc525aac8bd246da213eb48a17f0e1.tar.gz
Use chi-square for random distributivity verification in test (#8709)
Problem: Currently, when performing random distribution verification, we determine the probability of each element occurring in the sum, but the probability is only an estimate, these tests had rare sporadic failures, and we cannot verify what the probability of failure will be. Solution: Using the chi-square distribution instead of the original random distribution validation makes the test more reasonable and easier to find problems.
Diffstat (limited to 'tests/support')
-rw-r--r--tests/support/util.tcl28
1 files changed, 21 insertions, 7 deletions
diff --git a/tests/support/util.tcl b/tests/support/util.tcl
index c35441ab0..5ea85c9e5 100644
--- a/tests/support/util.tcl
+++ b/tests/support/util.tcl
@@ -682,20 +682,34 @@ proc string2printable s {
return $res
}
-# Check that probability of each element are between {min_prop} and {max_prop}.
-proc check_histogram_distribution {res min_prop max_prop} {
+# Calculation value of Chi-Square Distribution. By this value
+# we can verify the random distribution sample confidence.
+# Based on the following wiki:
+# https://en.wikipedia.org/wiki/Chi-square_distribution
+#
+# param res Random sample list
+# return Value of Chi-Square Distribution
+#
+# x2_value: return of chi_square_value function
+# df: Degrees of freedom, Number of independent values minus 1
+#
+# By using x2_value and df to back check the cardinality table,
+# we can know the confidence of the random sample.
+proc chi_square_value {res} {
unset -nocomplain mydict
foreach key $res {
dict incr mydict $key 1
}
+ set x2_value 0
+ set p [expr [llength $res] / [dict size $mydict]]
foreach key [dict keys $mydict] {
set value [dict get $mydict $key]
- set probability [expr {double($value) / [llength $res]}]
- if {$probability < $min_prop || $probability > $max_prop} {
- return false
- }
+
+ # Aggregate the chi-square value of each element
+ set v [expr {pow($value - $p, 2) / $p}]
+ set x2_value [expr {$x2_value + $v}]
}
- return true
+ return $x2_value
}