Skip to content

Commit

Permalink
test: add benchtest
Browse files Browse the repository at this point in the history
  • Loading branch information
kyungeunni committed May 3, 2024
1 parent 05c2ba3 commit 7fba34a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
3 changes: 1 addition & 2 deletions bulk_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,7 @@ func (b *BulkIndexer) Flush(ctx context.Context) (BulkIndexerResponseStat, error
}
return resp, fmt.Errorf("flush failed: %s", res.String())
}
decoder := jsoniter.NewDecoder(res.Body)
if err := decoder.Decode(&resp); err != nil {
if err := jsoniter.NewDecoder(res.Body).Decode(&resp); err != nil {
return resp, fmt.Errorf("error decoding bulk response: %w", err)
}

Expand Down
43 changes: 42 additions & 1 deletion bulk_indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestBulkIndexer(t *testing.T) {
}
}

func TestLogErrorReason(t *testing.T) {
func TestRedactErrorReason(t *testing.T) {
errMsg := "error_reason_invalid. Preview of field's value: 'failed to parse value'"
tests := []struct {
name string
Expand Down Expand Up @@ -183,3 +183,44 @@ func TestLogErrorReason(t *testing.T) {
})
}
}

func BenchmarkRedaction(b *testing.B) {
errMsg := "error_reason_invalid. Preview of field's value: 'failed to parse value'"
client := docappendertest.NewMockElasticsearchClient(b, func(w http.ResponseWriter, r *http.Request) {
_, result := docappendertest.DecodeBulkRequest(r)
for _, itemsMap := range result.Items {
for k, item := range itemsMap {
result.HasErrors = true
item.Status = http.StatusBadRequest
item.Index = "an_index"
item.Error.Type = "error_type"
item.Error.Reason = errMsg
itemsMap[k] = item
}
}
json.NewEncoder(w).Encode(result)
})
indexer, err := docappender.NewBulkIndexer(docappender.BulkIndexerConfig{
Client: client,
CaptureFullErrorReason: false,
})
require.NoError(b, err)
generateLoad := func(count int) {
for i := 0; i < count; i++ {
require.NoError(b, indexer.Add(docappender.BulkIndexerItem{
Index: "testidx",
Body: newJSONReader(map[string]any{
"@timestamp": time.Now().Format(docappendertest.TimestampFormat),
}),
}))
}
}
for i := 0; i < b.N; i++ {
itemCount := 1_000
generateLoad(itemCount)
_, err := indexer.Flush(context.Background())
if err != nil {
b.FailNow()
}
}
}
35 changes: 35 additions & 0 deletions new.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
goos: darwin
goarch: arm64
pkg: github.com/elastic/go-docappender/v2
BenchmarkRedaction 297 3822273 ns/op
BenchmarkRedaction 308 3938680 ns/op
BenchmarkRedaction 312 3821946 ns/op
BenchmarkRedaction 312 3828728 ns/op
BenchmarkRedaction 312 3814236 ns/op
BenchmarkRedaction 308 3820958 ns/op
BenchmarkRedaction 312 3862118 ns/op
BenchmarkRedaction 310 3921311 ns/op
BenchmarkRedaction 292 4029690 ns/op
BenchmarkRedaction 285 4193818 ns/op
BenchmarkRedaction-2 290 4138245 ns/op
BenchmarkRedaction-2 301 4007444 ns/op
BenchmarkRedaction-2 300 4033109 ns/op
BenchmarkRedaction-2 298 4022833 ns/op
BenchmarkRedaction-2 285 4107814 ns/op
BenchmarkRedaction-2 296 4080051 ns/op
BenchmarkRedaction-2 296 4052033 ns/op
BenchmarkRedaction-2 296 4027817 ns/op
BenchmarkRedaction-2 295 3972116 ns/op
BenchmarkRedaction-2 314 3841261 ns/op
BenchmarkRedaction-4 322 3723084 ns/op
BenchmarkRedaction-4 322 3711382 ns/op
BenchmarkRedaction-4 319 3705052 ns/op
BenchmarkRedaction-4 324 3719619 ns/op
BenchmarkRedaction-4 324 3720388 ns/op
BenchmarkRedaction-4 324 3784756 ns/op
BenchmarkRedaction-4 324 3756972 ns/op
BenchmarkRedaction-4 297 3721637 ns/op
BenchmarkRedaction-4 321 3720482 ns/op
BenchmarkRedaction-4 322 3728986 ns/op
PASS
ok github.com/elastic/go-docappender/v2 48.142s

0 comments on commit 7fba34a

Please sign in to comment.