forked from tikv/pd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
223 lines (186 loc) · 4.25 KB
/
main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/pingcap/pd/pd-client"
"github.com/pkg/errors"
)
var (
pdAddrs = flag.String("pd", "127.0.0.1:2379", "pd address")
concurrency = flag.Int("C", 1000, "concurrency")
interval = flag.Duration("interval", time.Second, "interval to output the statistics")
caPath = flag.String("cacert", "", "path of file that contains list of trusted SSL CAs.")
certPath = flag.String("cert", "", "path of file that contains X509 certificate in PEM format..")
keyPath = flag.String("key", "", "path of file that contains X509 key in PEM format.")
wg sync.WaitGroup
)
func main() {
flag.Parse()
pdCli, err := pd.NewClient([]string{*pdAddrs}, pd.SecurityOption{
CAPath: *caPath,
CertPath: *certPath,
KeyPath: *keyPath,
})
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
// To avoid the first time high latency.
for i := 0; i < *concurrency; i++ {
_, _, err = pdCli.GetTS(ctx)
if err != nil {
log.Fatal(err)
}
}
durCh := make(chan time.Duration, *concurrency*2)
wg.Add(*concurrency)
for i := 0; i < *concurrency; i++ {
go reqWorker(ctx, pdCli, durCh)
}
wg.Add(1)
go showStats(ctx, durCh)
sc := make(chan os.Signal, 1)
signal.Notify(sc,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
go func() {
<-sc
cancel()
}()
wg.Wait()
pdCli.Close()
}
func showStats(ctx context.Context, durCh chan time.Duration) {
defer wg.Done()
statCtx, cancel := context.WithCancel(ctx)
defer cancel()
ticker := time.NewTicker(*interval)
s := newStats()
total := newStats()
for {
select {
case <-ticker.C:
println(s.String())
total.merge(s)
s = newStats()
case d := <-durCh:
s.update(d)
case <-statCtx.Done():
println("\nTotal:")
println(total.String())
return
}
}
}
const (
twoDur = time.Millisecond * 2
fiveDur = time.Millisecond * 5
tenDur = time.Millisecond * 10
thirtyDur = time.Millisecond * 30
)
type stats struct {
maxDur time.Duration
minDur time.Duration
count int
milliCnt int
twoMilliCnt int
fiveMilliCnt int
tenMSCnt int
thirtyCnt int
}
func newStats() *stats {
return &stats{
minDur: time.Hour,
maxDur: 0,
}
}
func (s *stats) update(dur time.Duration) {
s.count++
if dur > s.maxDur {
s.maxDur = dur
}
if dur < s.minDur {
s.minDur = dur
}
if dur > thirtyDur {
s.thirtyCnt++
return
}
if dur > tenDur {
s.tenMSCnt++
return
}
if dur > fiveDur {
s.fiveMilliCnt++
return
}
if dur > twoDur {
s.twoMilliCnt++
return
}
if dur > time.Millisecond {
s.milliCnt++
return
}
}
func (s *stats) merge(other *stats) {
if s.maxDur < other.maxDur {
s.maxDur = other.maxDur
}
if s.minDur > other.minDur {
s.minDur = other.minDur
}
s.count += other.count
s.milliCnt += other.milliCnt
s.twoMilliCnt += other.twoMilliCnt
s.fiveMilliCnt += other.fiveMilliCnt
s.tenMSCnt += other.tenMSCnt
s.thirtyCnt += other.thirtyCnt
}
func (s *stats) String() string {
return fmt.Sprintf("count:%d, max:%d, min:%d, >1ms:%d, >2ms:%d, >5ms:%d, >10ms:%d, >30ms:%d",
s.count, s.maxDur.Nanoseconds()/int64(time.Millisecond), s.minDur.Nanoseconds()/int64(time.Millisecond),
s.milliCnt, s.twoMilliCnt, s.fiveMilliCnt, s.tenMSCnt, s.thirtyCnt)
}
func reqWorker(ctx context.Context, pdCli pd.Client, durCh chan time.Duration) {
defer wg.Done()
reqCtx, cancel := context.WithCancel(ctx)
defer cancel()
for {
start := time.Now()
_, _, err := pdCli.GetTS(reqCtx)
if errors.Cause(err) == context.Canceled {
return
}
if err != nil {
log.Fatal(err)
}
dur := time.Since(start)
select {
case <-reqCtx.Done():
return
case durCh <- dur:
}
}
}