bulk_index_request_test.go 4.1 KB

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