sample.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package metrics
  2. import (
  3. "math"
  4. "rand"
  5. "time"
  6. )
  7. const rescaleThreshold = 1e9 * 60 * 60
  8. type Sample interface {
  9. Clear()
  10. Size() int
  11. Update(int64)
  12. Values() []int64
  13. }
  14. type expDecaySample struct {
  15. reservoirSize int
  16. alpha float64
  17. in chan int64
  18. out chan []int64
  19. reset chan bool
  20. }
  21. func NewExpDecaySample(reservoirSize int, alpha float64) Sample {
  22. s := &expDecaySample{
  23. reservoirSize,
  24. alpha,
  25. make(chan int64),
  26. make(chan []int64),
  27. make(chan bool),
  28. }
  29. go s.arbiter()
  30. return s
  31. }
  32. func (s *expDecaySample) Clear() {
  33. s.reset <- true
  34. }
  35. func (s *expDecaySample) Size() int {
  36. return len(<-s.out)
  37. }
  38. func (s *expDecaySample) Update(v int64) {
  39. s.in <- v
  40. }
  41. func (s *expDecaySample) Values() []int64 {
  42. return <-s.out
  43. }
  44. func (s *expDecaySample) arbiter() {
  45. count := 0
  46. values := make(map[float64]int64)
  47. tsStart := time.Seconds()
  48. tsNext := time.Nanoseconds() + rescaleThreshold
  49. var valuesCopy []int64
  50. for {
  51. select {
  52. case v := <-s.in:
  53. ts := time.Seconds()
  54. k := math.Exp(float64(ts - tsStart) * s.alpha) / rand.Float64()
  55. count++
  56. values[k] = v
  57. if count > s.reservoirSize {
  58. min := math.MaxFloat64
  59. for k, _ := range values {
  60. if k < min { min = k }
  61. }
  62. values[min] = 0, false
  63. valuesCopy = make([]int64, s.reservoirSize)
  64. } else {
  65. valuesCopy = make([]int64, count)
  66. }
  67. tsNano := time.Nanoseconds()
  68. if tsNano > tsNext {
  69. tsOldStart := tsStart
  70. tsStart = time.Seconds()
  71. tsNext = tsNano + rescaleThreshold
  72. oldValues := values
  73. values = make(map[float64]int64, len(oldValues))
  74. for k, v := range oldValues {
  75. values[k * math.Exp(-s.alpha * float64(
  76. tsStart - tsOldStart))] = v
  77. }
  78. }
  79. i := 0
  80. for _, v := range values {
  81. valuesCopy[i] = v
  82. i++
  83. }
  84. case s.out <- valuesCopy: // TODO Might need to make another copy here.
  85. case <-s.reset:
  86. count = 0
  87. values = make(map[float64]int64)
  88. valuesCopy = make([]int64, 0)
  89. tsStart = time.Seconds()
  90. tsNext = tsStart + rescaleThreshold
  91. }
  92. }
  93. }
  94. type uniformSample struct {
  95. reservoirSize int
  96. in chan int64
  97. out chan []int64
  98. reset chan bool
  99. }
  100. func NewUniformSample(reservoirSize int) Sample {
  101. s := &uniformSample{
  102. reservoirSize,
  103. make(chan int64),
  104. make(chan []int64),
  105. make(chan bool),
  106. }
  107. go s.arbiter()
  108. return s
  109. }
  110. func (s *uniformSample) Clear() {
  111. s.reset <- true
  112. }
  113. func (s *uniformSample) Size() int {
  114. return len(<-s.out)
  115. }
  116. func (s *uniformSample) Update(v int64) {
  117. s.in <- v
  118. }
  119. func (s *uniformSample) Values() []int64 {
  120. return <-s.out
  121. }
  122. func (s *uniformSample) arbiter() {
  123. count := 0
  124. values := make([]int64, s.reservoirSize)
  125. var valuesCopy []int64
  126. for {
  127. select {
  128. case v := <-s.in:
  129. count++
  130. if count < s.reservoirSize {
  131. values[count - 1] = v
  132. valuesCopy = make([]int64, count)
  133. } else {
  134. values[rand.Intn(s.reservoirSize)] = v
  135. valuesCopy = make([]int64, len(values))
  136. }
  137. for i := 0; i < len(valuesCopy); i++ { valuesCopy[i] = values[i] }
  138. case s.out <- valuesCopy: // TODO Might need to make another copy here.
  139. case <-s.reset:
  140. count = 0
  141. values = make([]int64, s.reservoirSize)
  142. valuesCopy = make([]int64, 0)
  143. }
  144. }
  145. }