1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
package graphite
import (
"bufio"
"net"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/rcrowley/go-metrics"
)
func floatEquals(a, b float64) bool {
return (a-b) < 0.000001 && (b-a) < 0.000001
}
func ExampleGraphite() {
addr, _ := net.ResolveTCPAddr("net", ":2003")
go Graphite(metrics.DefaultRegistry, 1*time.Second, "some.prefix", addr)
}
func ExampleGraphiteWithConfig() {
addr, _ := net.ResolveTCPAddr("net", ":2003")
go GraphiteWithConfig(GraphiteConfig{
Addr: addr,
Registry: metrics.DefaultRegistry,
FlushInterval: 1 * time.Second,
DurationUnit: time.Millisecond,
Percentiles: []float64{0.5, 0.75, 0.99, 0.999},
})
}
func NewTestServer(t *testing.T, prefix string) (map[string]float64, net.Listener, metrics.Registry, GraphiteConfig, *sync.WaitGroup) {
res := make(map[string]float64)
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal("could not start dummy server:", err)
}
var wg sync.WaitGroup
go func() {
for {
conn, err := ln.Accept()
if err != nil {
t.Fatal("dummy server error:", err)
}
r := bufio.NewReader(conn)
line, err := r.ReadString('\n')
for err == nil {
parts := strings.Split(line, " ")
i, _ := strconv.ParseFloat(parts[1], 0)
if testing.Verbose() {
t.Log("recv", parts[0], i)
}
res[parts[0]] = res[parts[0]] + i
line, err = r.ReadString('\n')
}
wg.Done()
conn.Close()
}
}()
r := metrics.NewRegistry()
c := GraphiteConfig{
Addr: ln.Addr().(*net.TCPAddr),
Registry: r,
FlushInterval: 10 * time.Millisecond,
DurationUnit: time.Millisecond,
Percentiles: []float64{0.5, 0.75, 0.99, 0.999},
Prefix: prefix,
}
return res, ln, r, c, &wg
}
func TestWrites(t *testing.T) {
t.Skip("Skip test until races are fixed,see https://github.com/cyberdelia/go-metrics-graphite/issues/11")
res, l, r, c, wg := NewTestServer(t, "foobar")
defer l.Close()
metrics.GetOrRegisterCounter("foo", r).Inc(2)
// TODO: Use a mock meter rather than wasting 10s to get a QPS.
for i := 0; i < 10*4; i++ {
metrics.GetOrRegisterMeter("bar", r).Mark(1)
time.Sleep(250 * time.Millisecond)
}
metrics.GetOrRegisterTimer("baz", r).Update(time.Second * 5)
metrics.GetOrRegisterTimer("baz", r).Update(time.Second * 4)
metrics.GetOrRegisterTimer("baz", r).Update(time.Second * 3)
metrics.GetOrRegisterTimer("baz", r).Update(time.Second * 2)
metrics.GetOrRegisterTimer("baz", r).Update(time.Second * 1)
wg.Add(1)
GraphiteOnce(c)
wg.Wait()
if expected, found := 2.0, res["foobar.foo.count"]; !floatEquals(found, expected) {
t.Fatal("bad value:", expected, found)
}
if expected, found := 40.0, res["foobar.bar.count"]; !floatEquals(found, expected) {
t.Fatal("bad value:", expected, found)
}
if expected, found := 4.0, res["foobar.bar.one-minute"]; !floatEquals(found, expected) {
t.Fatal("bad value:", expected, found)
}
if expected, found := 5.0, res["foobar.baz.count"]; !floatEquals(found, expected) {
t.Fatal("bad value:", expected, found)
}
if expected, found := 5000.0, res["foobar.baz.99-percentile"]; !floatEquals(found, expected) {
t.Fatal("bad value:", expected, found)
}
if expected, found := 3000.0, res["foobar.baz.50-percentile"]; !floatEquals(found, expected) {
t.Fatal("bad value:", expected, found)
}
}
|