OSDN Git Service

delete miner
[bytom/vapor.git] / vendor / github.com / go-kit / kit / metrics / teststat / buffers.go
1 package teststat
2
3 import (
4         "bufio"
5         "bytes"
6         "io"
7         "regexp"
8         "strconv"
9
10         "github.com/go-kit/kit/metrics/generic"
11 )
12
13 // SumLines expects a regex whose first capture group can be parsed as a
14 // float64. It will dump the WriterTo and parse each line, expecting to find a
15 // match. It returns the sum of all captured floats.
16 func SumLines(w io.WriterTo, regex string) func() float64 {
17         return func() float64 {
18                 sum, _ := stats(w, regex, nil)
19                 return sum
20         }
21 }
22
23 // LastLine expects a regex whose first capture group can be parsed as a
24 // float64. It will dump the WriterTo and parse each line, expecting to find a
25 // match. It returns the final captured float.
26 func LastLine(w io.WriterTo, regex string) func() float64 {
27         return func() float64 {
28                 _, final := stats(w, regex, nil)
29                 return final
30         }
31 }
32
33 // Quantiles expects a regex whose first capture group can be parsed as a
34 // float64. It will dump the WriterTo and parse each line, expecting to find a
35 // match. It observes all captured floats into a generic.Histogram with the
36 // given number of buckets, and returns the 50th, 90th, 95th, and 99th quantiles
37 // from that histogram.
38 func Quantiles(w io.WriterTo, regex string, buckets int) func() (float64, float64, float64, float64) {
39         return func() (float64, float64, float64, float64) {
40                 h := generic.NewHistogram("quantile-test", buckets)
41                 stats(w, regex, h)
42                 return h.Quantile(0.50), h.Quantile(0.90), h.Quantile(0.95), h.Quantile(0.99)
43         }
44 }
45
46 func stats(w io.WriterTo, regex string, h *generic.Histogram) (sum, final float64) {
47         re := regexp.MustCompile(regex)
48         buf := &bytes.Buffer{}
49         w.WriteTo(buf)
50         //fmt.Fprintf(os.Stderr, "%s\n", buf.String())
51         s := bufio.NewScanner(buf)
52         for s.Scan() {
53                 match := re.FindStringSubmatch(s.Text())
54                 f, err := strconv.ParseFloat(match[1], 64)
55                 if err != nil {
56                         panic(err)
57                 }
58                 sum += f
59                 final = f
60                 if h != nil {
61                         h.Observe(f)
62                 }
63         }
64         return sum, final
65 }