Merge example
Blake Mizerany
10 years ago
9 | 9 | "strconv" |
10 | 10 | ) |
11 | 11 | |
12 | func Example() { | |
12 | func Example_simple() { | |
13 | 13 | ch := make(chan float64) |
14 | 14 | go readFloats(ch) |
15 | 15 | |
30 | 30 | // count: 2388 |
31 | 31 | } |
32 | 32 | |
33 | func Example_mergeMultipleStreams() { | |
34 | // Scenario: | |
35 | // We have multiple database shards. On each shard, there is a process | |
36 | // collecting query response times from the database logs and inserting | |
37 | // them into a Stream (created via NewTargeted(0.90)), much like the | |
38 | // Simple example. These processes expose a network interface for us to | |
39 | // ask them to serialize and send us the results of their | |
40 | // Stream.Samples so we may Merge and Query them. | |
41 | // | |
42 | // NOTES: | |
43 | // * These sample sets are small, allowing us to get them | |
44 | // across the network much faster than sending the entire list of data | |
45 | // points. | |
46 | // | |
47 | // * For this to work correctly, we must supply the same quantiles | |
48 | // a priori the process collecting the samples supplied to NewTargeted, | |
49 | // even if we do not plan to query them all here. | |
50 | ch := make(chan quantile.Samples) | |
51 | getDBQuerySamples(ch) | |
52 | q := quantile.NewTargeted(0.90) | |
53 | for samples := range ch { | |
54 | q.Merge(samples) | |
55 | } | |
56 | fmt.Println("perc50:", q.Query(0.90)) | |
57 | } | |
58 | ||
59 | // This is a stub for the above example. In reality this would hit the remote | |
60 | // servers via http or something like it. | |
61 | func getDBQuerySamples(ch chan quantile.Samples) {} | |
62 | ||
33 | 63 | func readFloats(ch chan<- float64) { |
34 | 64 | f, err := os.Open("exampledata.txt") |
35 | 65 | if err != nil { |