forked from irohirokid/dbperf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperftest.go
177 lines (153 loc) · 3.42 KB
/
perftest.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
package main
import (
"context"
"errors"
"fmt"
"os"
"time"
"github.com/irohirokid/dbperf/db"
"github.com/irohirokid/dbperf/result"
"github.com/montanaflynn/stats"
"golang.org/x/sync/errgroup"
"golang.org/x/time/rate"
)
type statData struct {
start time.Time
reqChan chan struct{}
resTimes stats.Float64Data
numErr int
}
func (sd *statData) report() (*result.Stat, error) {
avr, err := sd.resTimes.Mean()
if err != nil {
return nil, fmt.Errorf("mean: %s", err.Error())
}
med, err := sd.resTimes.Median()
if err != nil {
return nil, fmt.Errorf("median: %s", err.Error())
}
p95, err := stats.Percentile(sd.resTimes, 95)
if err != nil {
return nil, fmt.Errorf("percentile 95: %s", err.Error())
}
p99, err := stats.Percentile(sd.resTimes, 99)
if err != nil {
return nil, fmt.Errorf("percentile 99: %s", err.Error())
}
wst, err := sd.resTimes.Max()
if err != nil {
return nil, fmt.Errorf("max: %s", err.Error())
}
stat := result.Stat{
Elapsed: time.Since(sd.start),
Count: len(sd.resTimes),
Err: sd.numErr,
Avr: avr,
Med: med,
P95: p95,
P99: p99,
Wst: wst,
Remain: len(sd.reqChan),
}
sd.resTimes = sd.resTimes[:0]
sd.numErr = 0
return &stat, nil
}
func (sd *statData) appendResTime(t time.Duration) {
sd.resTimes = append(sd.resTimes, float64(t.Microseconds())/1000)
}
func (sd *statData) empty() bool {
return len(sd.resTimes) == 0
}
func (sd *statData) incErr() {
sd.numErr++
}
func loader(appDb db.Client, reqChan chan struct{}, statChan chan result.Stat) error {
var err error
var stat *result.Stat
sd := &statData{
start: time.Now(),
reqChan: reqChan,
resTimes: make(stats.Float64Data, 0, *reqPerSec**interval),
numErr: 0,
}
statTicker := time.Tick(time.Duration(*interval * int(time.Second)))
Loop:
for {
select {
case _, ok := <-reqChan:
if !ok {
if !sd.empty() {
stat, err = sd.report()
if err != nil {
return err
}
statChan <- *stat
}
break Loop
}
_start := time.Now()
switch *operation {
case "r":
err = appDb.SimpleRead()
case "cr":
err = appDb.ConsistentRead()
case "tw":
err = appDb.TransactWrite()
}
resTime := time.Since(_start)
if err != nil {
fmt.Fprintf(os.Stderr, "On operation: %v\n", err.Error())
sd.incErr()
}
sd.appendResTime(resTime)
case <-statTicker:
if len(reqChan) > *reqPerSec {
return errors.New("*** Request overflow ***")
}
if sd.empty() {
break
}
stat, err = sd.report()
if err != nil {
return err
}
statChan <- *stat
}
}
return nil
}
func parentLoader(ctx context.Context, reqChan chan struct{}) {
limitter := rate.NewLimiter(rate.Limit(*reqPerSec), 1)
for i := 0; i < *reqPerSec**duration; i++ {
limitter.Wait(ctx)
reqChan <- struct{}{}
}
time.AfterFunc(time.Second, func() {
close(reqChan)
})
}
func perfTest(appDb db.Client) error {
statChan := make(chan result.Stat)
go statPrinter(statChan)
reqChan := make(chan struct{}, *reqPerSec**duration)
eg, ctx := errgroup.WithContext(context.Background())
for i := 0; i < *numLoaders; i++ {
eg.Go(func() error {
return loader(appDb, reqChan, statChan)
})
}
go parentLoader(ctx, reqChan)
err := eg.Wait()
return err
}
func queueRequests(reqChan chan struct{}) {
for i := 0; i < *reqPerSec; i++ {
reqChan <- struct{}{}
}
}
func statPrinter(statChan chan result.Stat) {
for {
fmt.Printf("%v\n", <-statChan)
}
}