originPicker_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package loadbalance
  2. import (
  3. "fmt"
  4. "math"
  5. "math/rand"
  6. "testing"
  7. "time"
  8. )
  9. // func getRandomUpstreamByWeight(upstreams []*Upstream) (*Upstream, int, error) { ... }
  10. func TestRandomUpstreamSelection(t *testing.T) {
  11. rand.Seed(time.Now().UnixNano()) // Seed for randomness
  12. // Define some test upstreams
  13. upstreams := []*Upstream{
  14. {
  15. OriginIpOrDomain: "192.168.1.1:8080",
  16. RequireTLS: false,
  17. SkipCertValidations: false,
  18. SkipWebSocketOriginCheck: false,
  19. Weight: 1,
  20. MaxConn: 0, // No connection limit for now
  21. },
  22. {
  23. OriginIpOrDomain: "192.168.1.2:8080",
  24. RequireTLS: false,
  25. SkipCertValidations: false,
  26. SkipWebSocketOriginCheck: false,
  27. Weight: 1,
  28. MaxConn: 0,
  29. },
  30. {
  31. OriginIpOrDomain: "192.168.1.3:8080",
  32. RequireTLS: true,
  33. SkipCertValidations: true,
  34. SkipWebSocketOriginCheck: true,
  35. Weight: 1,
  36. MaxConn: 0,
  37. },
  38. {
  39. OriginIpOrDomain: "192.168.1.4:8080",
  40. RequireTLS: true,
  41. SkipCertValidations: true,
  42. SkipWebSocketOriginCheck: true,
  43. Weight: 1,
  44. MaxConn: 0,
  45. },
  46. }
  47. // Track how many times each upstream is selected
  48. selectionCount := make(map[string]int)
  49. totalPicks := 10000 // Number of times to call getRandomUpstreamByWeight
  50. //expectedPickCount := totalPicks / len(upstreams) // Ideal count for each upstream
  51. // Pick upstreams and record their selection count
  52. for i := 0; i < totalPicks; i++ {
  53. upstream, _, err := getRandomUpstreamByWeight(upstreams)
  54. if err != nil {
  55. t.Fatalf("Error getting random upstream: %v", err)
  56. }
  57. selectionCount[upstream.OriginIpOrDomain]++
  58. }
  59. // Condition 1: Ensure every upstream has been picked at least once
  60. for _, upstream := range upstreams {
  61. if selectionCount[upstream.OriginIpOrDomain] == 0 {
  62. t.Errorf("Upstream %s was never selected", upstream.OriginIpOrDomain)
  63. }
  64. }
  65. // Condition 2: Check that the distribution is within 1-2 standard deviations
  66. counts := make([]float64, len(upstreams))
  67. for i, upstream := range upstreams {
  68. counts[i] = float64(selectionCount[upstream.OriginIpOrDomain])
  69. }
  70. mean := float64(totalPicks) / float64(len(upstreams))
  71. stddev := calculateStdDev(counts, mean)
  72. tolerance := 2 * stddev // Allowing up to 2 standard deviations
  73. for i, count := range counts {
  74. if math.Abs(count-mean) > tolerance {
  75. t.Errorf("Selection of upstream %s is outside acceptable range: %v picks (mean: %v, stddev: %v)", upstreams[i].OriginIpOrDomain, count, mean, stddev)
  76. }
  77. }
  78. fmt.Println("Selection count:", selectionCount)
  79. fmt.Printf("Mean: %.2f, StdDev: %.2f\n", mean, stddev)
  80. }
  81. // Helper function to calculate standard deviation
  82. func calculateStdDev(data []float64, mean float64) float64 {
  83. var sumOfSquares float64
  84. for _, value := range data {
  85. sumOfSquares += (value - mean) * (value - mean)
  86. }
  87. variance := sumOfSquares / float64(len(data))
  88. return math.Sqrt(variance)
  89. }