timestats_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright 2020 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package client
  17. import (
  18. "math"
  19. "math/rand"
  20. "testing"
  21. "time"
  22. "github.com/ethereum/go-ethereum/les/utils"
  23. )
  24. func TestTransition(t *testing.T) {
  25. var epsilon = 0.01
  26. var cases = []time.Duration{
  27. time.Millisecond, minResponseTime,
  28. time.Second, time.Second * 5, maxResponseTime,
  29. }
  30. for _, c := range cases {
  31. got := StatScaleToTime(TimeToStatScale(c))
  32. if float64(got)*(1+epsilon) < float64(c) || float64(got)*(1-epsilon) > float64(c) {
  33. t.Fatalf("Failed to transition back")
  34. }
  35. }
  36. // If the time is too large(exceeds the max response time.
  37. got := StatScaleToTime(TimeToStatScale(2 * maxResponseTime))
  38. if float64(got)*(1+epsilon) < float64(maxResponseTime) || float64(got)*(1-epsilon) > float64(maxResponseTime) {
  39. t.Fatalf("Failed to transition back")
  40. }
  41. }
  42. var maxResponseWeights = TimeoutWeights(maxResponseTime)
  43. func TestValue(t *testing.T) {
  44. noexp := utils.ExpirationFactor{Factor: 1}
  45. for i := 0; i < 1000; i++ {
  46. max := minResponseTime + time.Duration(rand.Int63n(int64(maxResponseTime-minResponseTime)))
  47. min := minResponseTime + time.Duration(rand.Int63n(int64(max-minResponseTime)))
  48. timeout := max/2 + time.Duration(rand.Int63n(int64(maxResponseTime-max/2)))
  49. s := makeRangeStats(min, max, 1000, noexp)
  50. value := s.Value(TimeoutWeights(timeout), noexp)
  51. // calculate the average weight (the average of the given range of the half cosine
  52. // weight function).
  53. minx := math.Pi / 2 * float64(min) / float64(timeout)
  54. maxx := math.Pi / 2 * float64(max) / float64(timeout)
  55. avgWeight := (math.Sin(maxx) - math.Sin(minx)) / (maxx - minx)
  56. expv := 1000 * avgWeight
  57. if expv < 0 {
  58. expv = 0
  59. }
  60. if value < expv-10 || value > expv+10 {
  61. t.Errorf("Value failed (expected %v, got %v)", expv, value)
  62. }
  63. }
  64. }
  65. func TestAddSubExpire(t *testing.T) {
  66. var (
  67. sum1, sum2 ResponseTimeStats
  68. sum1ValueExp, sum2ValueExp float64
  69. logOffset utils.Fixed64
  70. )
  71. for i := 0; i < 1000; i++ {
  72. exp := utils.ExpFactor(logOffset)
  73. max := minResponseTime + time.Duration(rand.Int63n(int64(maxResponseTime-minResponseTime)))
  74. min := minResponseTime + time.Duration(rand.Int63n(int64(max-minResponseTime)))
  75. s := makeRangeStats(min, max, 1000, exp)
  76. value := s.Value(maxResponseWeights, exp)
  77. sum1.AddStats(&s)
  78. sum1ValueExp += value
  79. if rand.Intn(2) == 1 {
  80. sum2.AddStats(&s)
  81. sum2ValueExp += value
  82. }
  83. logOffset += utils.Float64ToFixed64(0.001 / math.Log(2))
  84. sum1ValueExp -= sum1ValueExp * 0.001
  85. sum2ValueExp -= sum2ValueExp * 0.001
  86. }
  87. exp := utils.ExpFactor(logOffset)
  88. sum1Value := sum1.Value(maxResponseWeights, exp)
  89. if sum1Value < sum1ValueExp*0.99 || sum1Value > sum1ValueExp*1.01 {
  90. t.Errorf("sum1Value failed (expected %v, got %v)", sum1ValueExp, sum1Value)
  91. }
  92. sum2Value := sum2.Value(maxResponseWeights, exp)
  93. if sum2Value < sum2ValueExp*0.99 || sum2Value > sum2ValueExp*1.01 {
  94. t.Errorf("sum2Value failed (expected %v, got %v)", sum2ValueExp, sum2Value)
  95. }
  96. diff := sum1
  97. diff.SubStats(&sum2)
  98. diffValue := diff.Value(maxResponseWeights, exp)
  99. diffValueExp := sum1ValueExp - sum2ValueExp
  100. if diffValue < diffValueExp*0.99 || diffValue > diffValueExp*1.01 {
  101. t.Errorf("diffValue failed (expected %v, got %v)", diffValueExp, diffValue)
  102. }
  103. }
  104. func TestTimeout(t *testing.T) {
  105. testTimeoutRange(t, 0, time.Second)
  106. testTimeoutRange(t, time.Second, time.Second*2)
  107. testTimeoutRange(t, time.Second, maxResponseTime)
  108. }
  109. func testTimeoutRange(t *testing.T, min, max time.Duration) {
  110. s := makeRangeStats(min, max, 1000, utils.ExpirationFactor{Factor: 1})
  111. for i := 2; i < 9; i++ {
  112. to := s.Timeout(float64(i) / 10)
  113. exp := max - (max-min)*time.Duration(i)/10
  114. tol := (max - min) / 50
  115. if to < exp-tol || to > exp+tol {
  116. t.Errorf("Timeout failed (expected %v, got %v)", exp, to)
  117. }
  118. }
  119. }
  120. func makeRangeStats(min, max time.Duration, amount float64, exp utils.ExpirationFactor) ResponseTimeStats {
  121. var s ResponseTimeStats
  122. amount /= 1000
  123. for i := 0; i < 1000; i++ {
  124. s.Add(min+(max-min)*time.Duration(i)/999, amount, exp)
  125. }
  126. return s
  127. }