forked from mattbaird/elastigo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
corebulk_test.go
314 lines (269 loc) · 8.72 KB
/
corebulk_test.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright 2013 Matthew Baird
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package elastigo
import (
"bytes"
"crypto/rand"
"encoding/json"
"flag"
"fmt"
"log"
"strconv"
"testing"
"time"
"github.com/araddon/gou"
"github.com/bmizerany/assert"
)
// go test -bench=".*"
// go test -bench="Bulk"
func init() {
flag.Parse()
if testing.Verbose() {
gou.SetupLogging("debug")
}
}
// take two ints, compare, need to be within 5%
func closeInt(a, b int) bool {
c := float64(a) / float64(b)
if c >= .95 && c <= 1.05 {
return true
}
return false
}
func TestBulkIndexerBasic(t *testing.T) {
testIndex := "users"
var (
buffers = make([]*bytes.Buffer, 0)
totalBytesSent int
messageSets int
)
InitTests(true)
c := NewTestConn()
c.DeleteIndex(testIndex)
indexer := c.NewBulkIndexer(3)
indexer.Sender = func(buf *bytes.Buffer) error {
messageSets += 1
totalBytesSent += buf.Len()
buffers = append(buffers, buf)
// log.Printf("buffer:%s", string(buf.Bytes()))
return indexer.Send(buf)
}
indexer.Start()
date := time.Unix(1257894000, 0)
data := map[string]interface{}{
"name": "smurfs",
"age": 22,
"date": "yesterday",
}
err := indexer.Index(testIndex, "user", "1", "", &date, data, true)
waitFor(func() bool {
return len(buffers) > 0
}, 5)
// part of request is url, so lets factor that in
//totalBytesSent = totalBytesSent - len(*eshost)
assert.T(t, len(buffers) == 1, fmt.Sprintf("Should have sent one operation but was %d", len(buffers)))
assert.T(t, indexer.NumErrors() == 0 && err == nil, fmt.Sprintf("Should not have any errors. NumErrors: %v, err: %v", indexer.NumErrors(), err))
expectedBytes := 144
assert.T(t, totalBytesSent == expectedBytes, fmt.Sprintf("Should have sent %v bytes but was %v", expectedBytes, totalBytesSent))
err = indexer.Index(testIndex, "user", "2", "", nil, data, true)
<-time.After(time.Millisecond * 10) // we need to wait for doc to hit send channel
// this will test to ensure that Flush actually catches a doc
indexer.Flush()
totalBytesSent = totalBytesSent - len(*eshost)
assert.T(t, err == nil, fmt.Sprintf("Should have nil error =%v", err))
assert.T(t, len(buffers) == 2, fmt.Sprintf("Should have another buffer ct=%d", len(buffers)))
assert.T(t, indexer.NumErrors() == 0, fmt.Sprintf("Should not have any errors %d", indexer.NumErrors()))
expectedBytes = 250 // with refresh
assert.T(t, closeInt(totalBytesSent, expectedBytes), fmt.Sprintf("Should have sent %v bytes but was %v", expectedBytes, totalBytesSent))
indexer.Stop()
}
// currently broken in drone.io
func XXXTestBulkUpdate(t *testing.T) {
var (
buffers = make([]*bytes.Buffer, 0)
totalBytesSent int
messageSets int
)
InitTests(true)
c := NewTestConn()
c.Port = "9200"
indexer := c.NewBulkIndexer(3)
indexer.Sender = func(buf *bytes.Buffer) error {
messageSets += 1
totalBytesSent += buf.Len()
buffers = append(buffers, buf)
return indexer.Send(buf)
}
indexer.Start()
date := time.Unix(1257894000, 0)
user := map[string]interface{}{
"name": "smurfs", "age": 22, "date": date, "count": 1,
}
// Lets make sure the data is in the index ...
_, err := c.Index("users", "user", "5", nil, user)
// script and params
data := map[string]interface{}{
"script": "ctx._source.count += 2",
}
err = indexer.Update("users", "user", "5", "", &date, data, true)
// So here's the deal. Flushing does seem to work, you just have to give the
// channel a moment to recieve the message ...
// <- time.After(time.Millisecond * 20)
// indexer.Flush()
waitFor(func() bool {
return len(buffers) > 0
}, 5)
indexer.Stop()
assert.T(t, indexer.NumErrors() == 0 && err == nil, fmt.Sprintf("Should not have any errors, bulkErrorCt:%v, err:%v", indexer.NumErrors(), err))
response, err := c.Get("users", "user", "5", nil)
assert.T(t, err == nil, fmt.Sprintf("Should not have any errors %v", err))
m := make(map[string]interface{})
json.Unmarshal([]byte(*response.Source), &m)
newCount := m["count"]
assert.T(t, newCount.(float64) == 3,
fmt.Sprintf("Should have update count: %#v ... %#v", m["count"], response))
}
func TestBulkSmallBatch(t *testing.T) {
var (
messageSets int
)
InitTests(true)
c := NewTestConn()
date := time.Unix(1257894000, 0)
data := map[string]interface{}{"name": "smurfs", "age": 22, "date": date}
// Now tests small batches
indexer := c.NewBulkIndexer(1)
indexer.BufferDelayMax = 100 * time.Millisecond
indexer.BulkMaxDocs = 2
messageSets = 0
indexer.Sender = func(buf *bytes.Buffer) error {
messageSets += 1
return indexer.Send(buf)
}
indexer.Start()
<-time.After(time.Millisecond * 20)
indexer.Index("users", "user", "2", "", &date, data, true)
indexer.Index("users", "user", "3", "", &date, data, true)
indexer.Index("users", "user", "4", "", &date, data, true)
<-time.After(time.Millisecond * 200)
// indexer.Flush()
indexer.Stop()
assert.T(t, messageSets == 2, fmt.Sprintf("Should have sent 2 message sets %d", messageSets))
}
func TestBulkDelete(t *testing.T) {
InitTests(true)
c := NewTestConn()
indexer := c.NewBulkIndexer(1)
sentBytes := []byte{}
indexer.Sender = func(buf *bytes.Buffer) error {
sentBytes = append(sentBytes, buf.Bytes()...)
return nil
}
indexer.Start()
indexer.Delete("fake", "fake_type", "1", true)
indexer.Flush()
indexer.Stop()
sent := string(sentBytes)
expected := `{"delete":{"_index":"fake","_type":"fake_type","_id":"1","refresh":true}}
`
asExpected := sent == expected
assert.T(t, asExpected, fmt.Sprintf("Should have sent '%s' but actually sent '%s'", expected, sent))
}
func XXXTestBulkErrors(t *testing.T) {
// lets set a bad port, and hope we get a conn refused error?
c := NewTestConn()
c.Port = "27845"
defer func() {
c.Port = "9200"
}()
indexer := c.NewBulkIndexerErrors(10, 1)
indexer.Start()
errorCt := 0
go func() {
for i := 0; i < 20; i++ {
date := time.Unix(1257894000, 0)
data := map[string]interface{}{"name": "smurfs", "age": 22, "date": date}
indexer.Index("users", "user", strconv.Itoa(i), "", &date, data, true)
}
}()
var errBuf *ErrorBuffer
for errBuf = range indexer.ErrorChannel {
errorCt++
break
}
if errBuf.Buf.Len() > 0 {
gou.Debug(errBuf.Err)
}
assert.T(t, errorCt > 0, fmt.Sprintf("ErrorCt should be > 0 %d", errorCt))
indexer.Stop()
}
/*
BenchmarkSend 18:33:00 bulk_test.go:131: Sent 1 messages in 0 sets totaling 0 bytes
18:33:00 bulk_test.go:131: Sent 100 messages in 1 sets totaling 145889 bytes
18:33:01 bulk_test.go:131: Sent 10000 messages in 100 sets totaling 14608888 bytes
18:33:05 bulk_test.go:131: Sent 20000 messages in 99 sets totaling 14462790 bytes
20000 234526 ns/op
*/
func BenchmarkSend(b *testing.B) {
InitTests(true)
c := NewTestConn()
b.StartTimer()
totalBytes := 0
sets := 0
indexer := c.NewBulkIndexer(1)
indexer.Sender = func(buf *bytes.Buffer) error {
totalBytes += buf.Len()
sets += 1
//log.Println("got bulk")
return indexer.Send(buf)
}
for i := 0; i < b.N; i++ {
about := make([]byte, 1000)
rand.Read(about)
data := map[string]interface{}{"name": "smurfs", "age": 22, "date": time.Unix(1257894000, 0), "about": about}
indexer.Index("users", "user", strconv.Itoa(i), "", nil, data, true)
}
log.Printf("Sent %d messages in %d sets totaling %d bytes \n", b.N, sets, totalBytes)
if indexer.NumErrors() != 0 {
b.Fail()
}
}
/*
TODO: this should be faster than above
BenchmarkSendBytes 18:33:05 bulk_test.go:169: Sent 1 messages in 0 sets totaling 0 bytes
18:33:05 bulk_test.go:169: Sent 100 messages in 2 sets totaling 292299 bytes
18:33:09 bulk_test.go:169: Sent 10000 messages in 99 sets totaling 14473800 bytes
10000 373529 ns/op
*/
func BenchmarkSendBytes(b *testing.B) {
InitTests(true)
c := NewTestConn()
about := make([]byte, 1000)
rand.Read(about)
data := map[string]interface{}{"name": "smurfs", "age": 22, "date": time.Unix(1257894000, 0), "about": about}
body, _ := json.Marshal(data)
b.StartTimer()
totalBytes := 0
sets := 0
indexer := c.NewBulkIndexer(1)
indexer.Sender = func(buf *bytes.Buffer) error {
totalBytes += buf.Len()
sets += 1
return indexer.Send(buf)
}
for i := 0; i < b.N; i++ {
indexer.Index("users", "user", strconv.Itoa(i), "", nil, body, true)
}
log.Printf("Sent %d messages in %d sets totaling %d bytes \n", b.N, sets, totalBytes)
if indexer.NumErrors() != 0 {
b.Fail()
}
}