...
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
14
15
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
24
25
26 func LastLine(w io.WriterTo, regex string) func() []float64 {
27 return func() []float64 {
28 _, final := stats(w, regex, nil)
29 return []float64{final}
30 }
31 }
32
33
34
35
36
37
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 s := bufio.NewScanner(buf)
51 for s.Scan() {
52 match := re.FindStringSubmatch(s.Text())
53 f, err := strconv.ParseFloat(match[1], 64)
54 if err != nil {
55 panic(err)
56 }
57 sum += f
58 final = f
59 if h != nil {
60 h.Observe(f)
61 }
62 }
63 return sum, final
64 }
65
View as plain text