a simple go tool vanity url server.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

example_test.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // +build go1.1
  2. package quantile_test
  3. import (
  4. "bufio"
  5. "fmt"
  6. "log"
  7. "os"
  8. "strconv"
  9. "time"
  10. "github.com/beorn7/perks/quantile"
  11. )
  12. func Example_simple() {
  13. ch := make(chan float64)
  14. go sendFloats(ch)
  15. // Compute the 50th, 90th, and 99th percentile.
  16. q := quantile.NewTargeted(map[float64]float64{
  17. 0.50: 0.005,
  18. 0.90: 0.001,
  19. 0.99: 0.0001,
  20. })
  21. for v := range ch {
  22. q.Insert(v)
  23. }
  24. fmt.Println("perc50:", q.Query(0.50))
  25. fmt.Println("perc90:", q.Query(0.90))
  26. fmt.Println("perc99:", q.Query(0.99))
  27. fmt.Println("count:", q.Count())
  28. // Output:
  29. // perc50: 5
  30. // perc90: 16
  31. // perc99: 223
  32. // count: 2388
  33. }
  34. func Example_mergeMultipleStreams() {
  35. // Scenario:
  36. // We have multiple database shards. On each shard, there is a process
  37. // collecting query response times from the database logs and inserting
  38. // them into a Stream (created via NewTargeted(0.90)), much like the
  39. // Simple example. These processes expose a network interface for us to
  40. // ask them to serialize and send us the results of their
  41. // Stream.Samples so we may Merge and Query them.
  42. //
  43. // NOTES:
  44. // * These sample sets are small, allowing us to get them
  45. // across the network much faster than sending the entire list of data
  46. // points.
  47. //
  48. // * For this to work correctly, we must supply the same quantiles
  49. // a priori the process collecting the samples supplied to NewTargeted,
  50. // even if we do not plan to query them all here.
  51. ch := make(chan quantile.Samples)
  52. getDBQuerySamples(ch)
  53. q := quantile.NewTargeted(map[float64]float64{0.90: 0.001})
  54. for samples := range ch {
  55. q.Merge(samples)
  56. }
  57. fmt.Println("perc90:", q.Query(0.90))
  58. }
  59. func Example_window() {
  60. // Scenario: We want the 90th, 95th, and 99th percentiles for each
  61. // minute.
  62. ch := make(chan float64)
  63. go sendStreamValues(ch)
  64. tick := time.NewTicker(1 * time.Minute)
  65. q := quantile.NewTargeted(map[float64]float64{
  66. 0.90: 0.001,
  67. 0.95: 0.0005,
  68. 0.99: 0.0001,
  69. })
  70. for {
  71. select {
  72. case t := <-tick.C:
  73. flushToDB(t, q.Samples())
  74. q.Reset()
  75. case v := <-ch:
  76. q.Insert(v)
  77. }
  78. }
  79. }
  80. func sendStreamValues(ch chan float64) {
  81. // Use your imagination
  82. }
  83. func flushToDB(t time.Time, samples quantile.Samples) {
  84. // Use your imagination
  85. }
  86. // This is a stub for the above example. In reality this would hit the remote
  87. // servers via http or something like it.
  88. func getDBQuerySamples(ch chan quantile.Samples) {}
  89. func sendFloats(ch chan<- float64) {
  90. f, err := os.Open("exampledata.txt")
  91. if err != nil {
  92. log.Fatal(err)
  93. }
  94. sc := bufio.NewScanner(f)
  95. for sc.Scan() {
  96. b := sc.Bytes()
  97. v, err := strconv.ParseFloat(string(b), 64)
  98. if err != nil {
  99. log.Fatal(err)
  100. }
  101. ch <- v
  102. }
  103. if sc.Err() != nil {
  104. log.Fatal(sc.Err())
  105. }
  106. close(ch)
  107. }