sample.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.Nanoseconds()
  48. tsNext := tsStart + rescaleThreshold
  49. var valuesCopy []int64
  50. for {
  51. select {
  52. case v := <-s.in:
  53. ts := time.Nanoseconds()
  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. if ts > tsNext {
  68. tsOldStart := tsStart
  69. tsStart = time.Nanoseconds()
  70. tsNext = ts + rescaleThreshold
  71. oldValues := values
  72. values = make(map[float64]int64, len(oldValues))
  73. for k, v := range oldValues {
  74. values[k * math.Exp(-s.alpha * float64(
  75. tsStart - tsOldStart))] = v
  76. }
  77. }
  78. i := 0
  79. for _, v := range values {
  80. valuesCopy[i] = v
  81. i++
  82. }
  83. case s.out <- valuesCopy: // TODO Might need to make another copy here.
  84. case <-s.reset:
  85. count = 0
  86. values = make(map[float64]int64)
  87. valuesCopy = make([]int64, 0)
  88. tsStart = time.Nanoseconds()
  89. tsNext = tsStart + 1e9 * 60 * 60
  90. }
  91. }
  92. }
  93. type uniformSample struct {
  94. reservoirSize int
  95. in chan int64
  96. out chan []int64
  97. reset chan bool
  98. }
  99. func NewUniformSample(reservoirSize int) Sample {
  100. s := &uniformSample{
  101. reservoirSize,
  102. make(chan int64),
  103. make(chan []int64),
  104. make(chan bool),
  105. }
  106. go s.arbiter()
  107. return s
  108. }
  109. func (s *uniformSample) Clear() {
  110. s.reset <- true
  111. }
  112. func (s *uniformSample) Size() int {
  113. return len(<-s.out)
  114. }
  115. func (s *uniformSample) Update(v int64) {
  116. s.in <- v
  117. }
  118. func (s *uniformSample) Values() []int64 {
  119. return <-s.out
  120. }
  121. func (s *uniformSample) arbiter() {
  122. count := 0
  123. values := make([]int64, s.reservoirSize)
  124. var valuesCopy []int64
  125. for {
  126. select {
  127. case v := <-s.in:
  128. count++
  129. if count < s.reservoirSize {
  130. values[count - 1] = v
  131. valuesCopy = make([]int64, count)
  132. } else {
  133. values[rand.Intn(s.reservoirSize)] = v
  134. valuesCopy = make([]int64, len(values))
  135. }
  136. for i := 0; i < len(valuesCopy); i++ { valuesCopy[i] = values[i] }
  137. case s.out <- valuesCopy: // TODO Might need to make another copy here.
  138. case <-s.reset:
  139. count = 0
  140. values = make([]int64, s.reservoirSize)
  141. valuesCopy = make([]int64, 0)
  142. }
  143. }
  144. }