MCPcopy
hub / github.com/grpc/grpc-go / pearsonsChiSquareTest

Function pearsonsChiSquareTest

internal/testutils/roundrobin/roundrobin.go:253–286  ·  view source on GitHub ↗

pearsonsChiSquareTest checks if the observed counts match the expected counts. Pearson's Chi-Squared Test Formula: χ² = ∑ (Oᵢ - Eᵢ)² / Eᵢ Where: - χ² is the chi-square statistic - Oᵢ is the observed count for category i - Eᵢ is the expected count for category i - The sum is over all categories (i

(t *testing.T, observedCounts, expectedCounts map[string]float64)

Source from the content-addressed store, hash-verified

251// See https://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test for more
252// details.
253func pearsonsChiSquareTest(t *testing.T, observedCounts, expectedCounts map[string]float64) error {
254 chiSquaredStat := 0.0
255 for addr, want := range expectedCounts {
256 got := observedCounts[addr]
257 chiSquaredStat += (got - want) * (got - want) / want
258 }
259 degreesOfFreedom := len(expectedCounts) - 1
260 const alpha = 1e-6
261 chiSquareDist := distuv.ChiSquared{K: float64(degreesOfFreedom)}
262 pValue := chiSquareDist.Survival(chiSquaredStat)
263 t.Logf("Observed ratio: %v", observedCounts)
264 t.Logf("Expected ratio: %v", expectedCounts)
265 t.Logf("χ² statistic: %.4f", chiSquaredStat)
266 t.Logf("Degrees of freedom: %d\n", degreesOfFreedom)
267 t.Logf("p-value: %.10f\n", pValue)
268 // Alpha (α) is the threshold we use to decide if the test "fails".
269 // It controls how sensitive the chi-square test is.
270 //
271 // A smaller alpha means we require stronger evidence to say the load
272 // balancing is wrong. A larger alpha makes the test more likely to fail,
273 // even for small random fluctuations.
274 //
275 // For CI, we set alpha = 1e-6 to avoid flaky test failures.
276 // That means:
277 // - There's only a 1-in-a-million chance the test fails due to random
278 // variation, assuming the load balancer is working correctly.
279 // - If the test *does* fail, it's very likely there's a real bug.
280 //
281 // TL;DR: smaller alpha = stricter test, fewer false alarms.
282 if pValue > alpha {
283 return nil
284 }
285 return fmt.Errorf("observed distribution significantly differs from expectations, observeredCounts: %v, expectedCounts: %v", observedCounts, expectedCounts)
286}

Callers 1

Calls 2

LogfMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected