window example
Blake Mizerany
10 years ago
7 | 7 | "log" |
8 | 8 | "os" |
9 | 9 | "strconv" |
10 | "time" | |
10 | 11 | ) |
11 | 12 | |
12 | 13 | func Example_simple() { |
56 | 57 | fmt.Println("perc50:", q.Query(0.90)) |
57 | 58 | } |
58 | 59 | |
60 | func Example_window() { | |
61 | // Scenario: We want the 90th, 95th, and 99th percentiles for each | |
62 | // minute. | |
63 | ||
64 | ch := make(chan float64) | |
65 | go readStreamValues(ch) | |
66 | ||
67 | tick := time.NewTicker(1 * time.Minute) | |
68 | q := quantile.NewTargeted(0.90, 0.95, 0.99) | |
69 | for { | |
70 | select { | |
71 | case t := <-tick.C: | |
72 | flushToDB(t, q.Samples()) | |
73 | q.Reset() | |
74 | case v := <-ch: | |
75 | q.Insert(v) | |
76 | } | |
77 | } | |
78 | } | |
79 | ||
80 | func readStreamValues(ch chan float64) { | |
81 | // Use your imagination | |
82 | } | |
83 | ||
84 | func flushToDB(t time.Time, samples quantile.Samples) { | |
85 | // Use your imagination | |
86 | } | |
87 | ||
59 | 88 | // This is a stub for the above example. In reality this would hit the remote |
60 | 89 | // servers via http or something like it. |
61 | 90 | func getDBQuerySamples(ch chan quantile.Samples) {} |