Debian Codegraph @

bulk_index_request_test.go

raw
1 // Copyright 2012-present Oliver Eilhard. All rights reserved.
2 // Use of this source code is governed by a MIT-license.
3 // See http://olivere.mit-license.org/license.txt for details.
4
5 package elastic
6
7 import (
8 "testing"
9 "time"
10 )
11
12 func TestBulkIndexRequestSerialization(t *testing.T) {
13 tests := []struct {
14 Request BulkableRequest
15 Expected []string
16 }{
17 // #0
18 {
19 Request: NewBulkIndexRequest().Index("index1").Type("tweet").Id("1").
20 Doc(tweet{User: "olivere", Created: time.Date(2014, 1, 18, 23, 59, 58, 0, time.UTC)}),
21 Expected: []string{
22 `{"index":{"_id":"1","_index":"index1","_type":"tweet"}}`,
23 `{"user":"olivere","message":"","retweets":0,"created":"2014-01-18T23:59:58Z"}`,
24 },
25 },
26 // #1
27 {
28 Request: NewBulkIndexRequest().OpType("create").Index("index1").Type("tweet").Id("1").
29 Doc(tweet{User: "olivere", Created: time.Date(2014, 1, 18, 23, 59, 58, 0, time.UTC)}),
30 Expected: []string{
31 `{"create":{"_id":"1","_index":"index1","_type":"tweet"}}`,
32 `{"user":"olivere","message":"","retweets":0,"created":"2014-01-18T23:59:58Z"}`,
33 },
34 },
35 // #2
36 {
37 Request: NewBulkIndexRequest().OpType("index").Index("index1").Type("tweet").Id("1").
38 Doc(tweet{User: "olivere", Created: time.Date(2014, 1, 18, 23, 59, 58, 0, time.UTC)}),
39 Expected: []string{
40 `{"index":{"_id":"1","_index":"index1","_type":"tweet"}}`,
41 `{"user":"olivere","message":"","retweets":0,"created":"2014-01-18T23:59:58Z"}`,
42 },
43 },
44 // #3
45 {
46 Request: NewBulkIndexRequest().OpType("index").Index("index1").Type("tweet").Id("1").RetryOnConflict(42).
47 Doc(tweet{User: "olivere", Created: time.Date(2014, 1, 18, 23, 59, 58, 0, time.UTC)}),
48 Expected: []string{
49 `{"index":{"_id":"1","_index":"index1","_type":"tweet","_retry_on_conflict":42}}`,
50 `{"user":"olivere","message":"","retweets":0,"created":"2014-01-18T23:59:58Z"}`,
51 },
52 },
53 // #4
54 {
55 Request: NewBulkIndexRequest().OpType("index").Index("index1").Type("tweet").Id("1").Pipeline("my_pipeline").
56 Doc(tweet{User: "olivere", Created: time.Date(2014, 1, 18, 23, 59, 58, 0, time.UTC)}),
57 Expected: []string{
58 `{"index":{"_id":"1","_index":"index1","_type":"tweet","pipeline":"my_pipeline"}}`,
59 `{"user":"olivere","message":"","retweets":0,"created":"2014-01-18T23:59:58Z"}`,
60 },
61 },
62 // #5
63 {
64 Request: NewBulkIndexRequest().OpType("index").Index("index1").Type("tweet").Id("1").TTL("1m").
65 Doc(tweet{User: "olivere", Created: time.Date(2014, 1, 18, 23, 59, 58, 0, time.UTC)}),
66 Expected: []string{
67 `{"index":{"_id":"1","_index":"index1","_ttl":"1m","_type":"tweet"}}`,
68 `{"user":"olivere","message":"","retweets":0,"created":"2014-01-18T23:59:58Z"}`,
69 },
70 },
71 }
72
73 for i, test := range tests {
74 lines, err := test.Request.Source()
75 if err != nil {
76 t.Fatalf("case #%d: expected no error, got: %v", i, err)
77 }
78 if lines == nil {
79 t.Fatalf("case #%d: expected lines, got nil", i)
80 }
81 if len(lines) != len(test.Expected) {
82 t.Fatalf("case #%d: expected %d lines, got %d", i, len(test.Expected), len(lines))
83 }
84 for j, line := range lines {
85 if line != test.Expected[j] {
86 t.Errorf("case #%d: expected line #%d to be %s, got: %s", i, j, test.Expected[j], line)
87 }
88 }
89 }
90 }
91
92 var bulkIndexRequestSerializationResult string
93
94 func BenchmarkBulkIndexRequestSerialization(b *testing.B) {
95 b.Run("stdlib", func(b *testing.B) {
96 r := NewBulkIndexRequest().Index(testIndexName).Type("doc").Id("1").
97 Doc(tweet{User: "olivere", Created: time.Date(2014, 1, 18, 23, 59, 58, 0, time.UTC)})
98 benchmarkBulkIndexRequestSerialization(b, r.UseEasyJSON(false))
99 })
100 b.Run("easyjson", func(b *testing.B) {
101 r := NewBulkIndexRequest().Index(testIndexName).Type("doc").Id("1").
102 Doc(tweet{User: "olivere", Created: time.Date(2014, 1, 18, 23, 59, 58, 0, time.UTC)})
103 benchmarkBulkIndexRequestSerialization(b, r.UseEasyJSON(true))
104 })
105 }
106
107 func benchmarkBulkIndexRequestSerialization(b *testing.B, r *BulkIndexRequest) {
108 var s string
109 for n := 0; n < b.N; n++ {
110 s = r.String()
111 r.source = nil // Don't let caching spoil the benchmark
112 }
113 bulkIndexRequestSerializationResult = s // ensure the compiler doesn't optimize
114 b.ReportAllocs()
115 }