-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathload_test.go
More file actions
190 lines (133 loc) · 3.85 KB
/
load_test.go
File metadata and controls
190 lines (133 loc) · 3.85 KB
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
package main
import (
"fmt"
"log"
"math/rand"
"net"
"os"
"reflect"
"testing"
"time"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttputil"
)
func TestInsertSpeed(t *testing.T) {
t.Skip("not working yet")
db := getDB("test")
numTags := 10000
// expectedWriteRate := runtime.NumCPU() * batchSize
expectedWriteRate := 200000
msgChan := db.startQueueConsumer()
totalInserted := 0
for i := 0; i < 40; i++ {
rate := 10000 + i*5000
start := time.Now()
for j := 0; j < rate; j++ {
msgChan <- randMsg(time.Now().UnixNano(), fmt.Sprintf("t%d", rand.Intn(numTags)))
}
for len(msgChan) > 0 {
time.Sleep(time.Millisecond * 100)
}
elapsed := time.Now().Sub(start)
totalInserted += rate
if elapsed < time.Millisecond*1250 {
continue
}
fmt.Println("> total inserted: ", totalInserted)
if rate > expectedWriteRate {
return
}
t.Fatalf("!> wanted wps: %d, got: %d\n", expectedWriteRate, rate)
}
}
func Test2sLag(t *testing.T) {
if os.Getenv("URL") == "" {
t.Skip("skipping test; URL not set")
}
rate, duration, url := envParams("RATE", "DURATION", "URL")
numTags := 10000
if os.Getenv("URL") == "" {
t.Skip("skipping test; URL not set")
}
loader := newHTTPLoader(url+"/save", numTags)
end := time.Now().UnixNano()
start := end - time.Duration(-24*time.Hour).Nanoseconds()
stats := map[string]int{"10": 0, "50": 0, "99": 0, "100+": 0}
rand.Seed(end)
for i := 0.0; i < duration.Seconds(); i++ {
elapsed := loader.add(rate)
m := loader.addOne()
go checkMessageReadable(url, m, t)
go apiResponseTimes(url, start, end, numTags, stats)
log.Printf("> waiting %v before generating next batch\n", time.Second-elapsed)
time.Sleep(time.Second - elapsed)
}
loader.stop()
time.Sleep(2 * time.Second)
time.Sleep(100 * time.Millisecond)
if len(loader.messages) > 0 {
t.Errorf("!> still %d messages in send chan", len(loader.messages))
}
fmt.Printf("\n> stats: %v\n\n", stats)
totalRequests := duration.Seconds() * 10
pct99 := (totalRequests - float64(stats["100+"])) / totalRequests * 100
// log.Printf("> totalrequets: %d, pct99: %d\n", totalRequests, pct99)
if pct99 < 99 {
t.Errorf("!> wanted 99%% under 100ms, got %v", pct99)
}
fmt.Println("finished 1K 2second lag test")
}
func checkMessageReadable(url string, m Msg, t *testing.T) {
// fmt.Println("> asking for written message: ", url, m)
<-time.After(time.Second * 2)
start := m.Time - time.Duration(10*time.Minute).Nanoseconds()
res, _ := getData(url+"/api", start, m.Time, m.Tag)
// fmt.Println("> got samples: ", len(res.Samples))
if len(res.Samples) == 0 {
t.Fatal("!> expected samples in response!")
}
if reflect.DeepEqual(res.Samples[len(res.Samples)-1].Values, m.Values) {
return
}
log.Printf("!> requested: %s/api?tag=%v&start=%d&end=%d\n", url, m.Tag, start, m.Time)
// log.Printf("!> resp: %v\n", res)
t.Errorf("> \nwant: %v\ngot : %v\n", m, res.Samples[len(res.Samples)-1])
}
type testData struct {
ts *fasthttp.Server
ln net.Listener
c *fasthttp.Client
db Database
in chan Msg
}
func (td testData) stop() {
// td.db.stopTicker()
td.ln.Close()
close(td.in)
log.Println("> stopped test server, db ingester ticker and closed messages channel")
}
func (td testData) saveURL() string {
return fmt.Sprintf("%s://%s/save", td.ln.Addr().Network, td.ln.Addr().String())
}
func (td testData) apiURL() string {
return fmt.Sprintf("%s://%s/api", td.ln.Addr().Network, td.ln.Addr().String())
}
func getTestServer() testData {
db := getDB("test")
in := db.startQueueConsumer()
s := &fasthttp.Server{
Handler: fhMux(db, in),
}
ln := fasthttputil.NewInmemoryListener()
go func() {
if err := s.Serve(ln); err != nil {
log.Fatalf("unexpected error: %s", err)
}
}()
c := &fasthttp.Client{
Dial: func(addr string) (net.Conn, error) {
return ln.Dial()
},
}
return testData{s, ln, c, db, in}
}