...
1
2
3 package quantile_test
4
5 import (
6 "bufio"
7 "fmt"
8 "log"
9 "os"
10 "strconv"
11 "time"
12
13 "github.com/beorn7/perks/quantile"
14 )
15
16 func Example_simple() {
17 ch := make(chan float64)
18 go sendFloats(ch)
19
20
21 q := quantile.NewTargeted(map[float64]float64{
22 0.50: 0.005,
23 0.90: 0.001,
24 0.99: 0.0001,
25 })
26 for v := range ch {
27 q.Insert(v)
28 }
29
30 fmt.Println("perc50:", q.Query(0.50))
31 fmt.Println("perc90:", q.Query(0.90))
32 fmt.Println("perc99:", q.Query(0.99))
33 fmt.Println("count:", q.Count())
34
35
36
37
38
39 }
40
41 func Example_mergeMultipleStreams() {
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 ch := make(chan quantile.Samples)
59 getDBQuerySamples(ch)
60 q := quantile.NewTargeted(map[float64]float64{0.90: 0.001})
61 for samples := range ch {
62 q.Merge(samples)
63 }
64 fmt.Println("perc90:", q.Query(0.90))
65 }
66
67 func Example_window() {
68
69
70
71 ch := make(chan float64)
72 go sendStreamValues(ch)
73
74 tick := time.NewTicker(1 * time.Minute)
75 q := quantile.NewTargeted(map[float64]float64{
76 0.90: 0.001,
77 0.95: 0.0005,
78 0.99: 0.0001,
79 })
80 for {
81 select {
82 case t := <-tick.C:
83 flushToDB(t, q.Samples())
84 q.Reset()
85 case v := <-ch:
86 q.Insert(v)
87 }
88 }
89 }
90
91 func sendStreamValues(ch chan float64) {
92
93 }
94
95 func flushToDB(t time.Time, samples quantile.Samples) {
96
97 }
98
99
100
101 func getDBQuerySamples(ch chan quantile.Samples) {}
102
103 func sendFloats(ch chan<- float64) {
104 f, err := os.Open("exampledata.txt")
105 if err != nil {
106 log.Fatal(err)
107 }
108 sc := bufio.NewScanner(f)
109 for sc.Scan() {
110 b := sc.Bytes()
111 v, err := strconv.ParseFloat(string(b), 64)
112 if err != nil {
113 log.Fatal(err)
114 }
115 ch <- v
116 }
117 if sc.Err() != nil {
118 log.Fatal(sc.Err())
119 }
120 close(ch)
121 }
122
View as plain text