forked from mesosphere/etcd-top
/
etcd-spewer.go
135 lines (125 loc) · 3.77 KB
/
etcd-spewer.go
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
128
129
130
131
132
133
134
135
package main
import (
"flag"
"fmt"
"log"
"math"
"math/rand"
"runtime"
"strings"
"time"
"github.com/coreos/go-etcd/etcd"
"github.com/spacejam/loghisto"
)
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
func RandString(n int32) string {
src := rand.NewSource(time.Now().UnixNano())
b := make([]byte, n)
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}
func reporter(metricStream chan *loghisto.ProcessedMetricSet) {
for m := range metricStream {
gets := int(m.Metrics["Get_rate"])
sets := int(m.Metrics["Put_rate"])
deletes := int(m.Metrics["Delete_rate"])
addchilds := int(m.Metrics["AddChild_rate"])
total := gets + sets + deletes + addchilds
fmt.Printf("total: %10.0d gets: %10.0d sets: %10.0d deletes: %10.0d addchilds: %10.0d\n",
total, gets, sets, deletes, addchilds)
fmt.Printf("50th lat GET: %10.0f PUT: %10.0f DELETE: %10.0f POST: %10.0f\n",
m.Metrics["GetLat_50"]/10e3,
m.Metrics["PutLat_50"]/10e3,
m.Metrics["DeleteLat_50"]/10e3,
m.Metrics["AddChildLat_50"]/10e3,
)
fmt.Printf("90th lat GET: %10.0f PUT: %10.0f DELETE: %10.0f POST: %10.0f\n",
m.Metrics["GetLat_90"]/10e3,
m.Metrics["PutLat_90"]/10e3,
m.Metrics["DeleteLat_90"]/10e3,
m.Metrics["AddChildLat_90"]/10e3,
)
fmt.Printf("99.9th lat GET: %10.0f PUT: %10.0f DELETE: %10.0f POST: %10.0f\n",
m.Metrics["GetLat_99.9"]/10e3,
m.Metrics["PutLat_99.9"]/10e3,
m.Metrics["DeleteLat_99.9"]/10e3,
m.Metrics["AddChildLat_99.9"]/10e3,
)
fmt.Printf("99.9th sz GET: %10.0f PUT: %10.0f\n",
m.Metrics["GetSz_99.9"],
m.Metrics["PutSz_99.9"],
)
}
}
func main() {
hosts := flag.String("hosts", "http://localhost:2379", "comma separated etcd hosts to spew at")
flag.Parse()
numCPU := runtime.NumCPU()
runtime.GOMAXPROCS(numCPU)
ms := loghisto.NewMetricSystem(time.Second, false)
ms.Start()
metricStream := make(chan *loghisto.ProcessedMetricSet, 2)
ms.SubscribeToProcessedMetrics(metricStream)
defer ms.UnsubscribeFromProcessedMetrics(metricStream)
machines := strings.Split(*hosts, ",")
// use zipfian distribution
r := rand.New(rand.NewSource(time.Now().UnixNano()))
zipf := rand.NewZipf(r, 3.14, 2.72, 500000)
go reporter(metricStream)
for i := 0; i < 5; i++ {
go func() {
client := etcd.NewClient(machines)
for i := 0; i < 3; i++ {
go func() {
for {
rando := rand.Float64()
valLen := int32(math.Max(float64(zipf.Uint64()), 1))
if rando > 0.8 {
t := ms.StartTimer("PutLat")
if _, err := client.Set("/"+RandString(1), RandString(500), 0); err != nil {
log.Fatal(err)
}
t.Stop()
ms.Histogram("PutSz", float64(valLen))
ms.Counter("Put", 1)
} else if rando > 0.7 {
t := ms.StartTimer("DeleteLat")
client.Delete("/"+RandString(1), true)
t.Stop()
ms.Counter("Delete", 1)
} else if rando > 0.65 {
t := ms.StartTimer("AddChildLat")
client.AddChild("/"+RandString(2), RandString(valLen), 0)
t.Stop()
ms.Counter("AddChild", 1)
} else {
t := ms.StartTimer("GetLat")
r, err := client.Get("/"+RandString(1), false, false)
if err == nil {
ms.Histogram("GetSz", float64(len(r.Node.Value)))
}
t.Stop()
ms.Counter("Get", 1)
}
}
}()
}
}()
}
<-make(chan struct{})
}