bulk_update_request_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. )
  8. func TestBulkUpdateRequestSerialization(t *testing.T) {
  9. tests := []struct {
  10. Request BulkableRequest
  11. Expected []string
  12. }{
  13. // #0
  14. {
  15. Request: NewBulkUpdateRequest().Index("index1").Type("tweet").Id("1").Doc(struct {
  16. Counter int64 `json:"counter"`
  17. }{
  18. Counter: 42,
  19. }),
  20. Expected: []string{
  21. `{"update":{"_id":"1","_index":"index1","_type":"tweet"}}`,
  22. `{"doc":{"counter":42}}`,
  23. },
  24. },
  25. // #1
  26. {
  27. Request: NewBulkUpdateRequest().Index("index1").Type("tweet").Id("1").
  28. RetryOnConflict(3).
  29. DocAsUpsert(true).
  30. Doc(struct {
  31. Counter int64 `json:"counter"`
  32. }{
  33. Counter: 42,
  34. }),
  35. Expected: []string{
  36. `{"update":{"_id":"1","_index":"index1","_type":"tweet","_retry_on_conflict":3}}`,
  37. `{"doc":{"counter":42},"doc_as_upsert":true}`,
  38. },
  39. },
  40. // #2
  41. {
  42. Request: NewBulkUpdateRequest().Index("index1").Type("tweet").Id("1").
  43. RetryOnConflict(3).
  44. Script(NewScript(`ctx._source.retweets += param1`).Lang("javascript").Param("param1", 42)).
  45. Upsert(struct {
  46. Counter int64 `json:"counter"`
  47. }{
  48. Counter: 42,
  49. }),
  50. Expected: []string{
  51. `{"update":{"_id":"1","_index":"index1","_type":"tweet","_retry_on_conflict":3}}`,
  52. `{"upsert":{"counter":42},"script":{"inline":"ctx._source.retweets += param1","lang":"javascript","params":{"param1":42}}}`,
  53. },
  54. },
  55. // #3
  56. {
  57. Request: NewBulkUpdateRequest().Index("index1").Type("tweet").Id("1").DetectNoop(true).Doc(struct {
  58. Counter int64 `json:"counter"`
  59. }{
  60. Counter: 42,
  61. }),
  62. Expected: []string{
  63. `{"update":{"_id":"1","_index":"index1","_type":"tweet"}}`,
  64. `{"detect_noop":true,"doc":{"counter":42}}`,
  65. },
  66. },
  67. // #4
  68. {
  69. Request: NewBulkUpdateRequest().Index("index1").Type("tweet").Id("1").
  70. RetryOnConflict(3).
  71. ScriptedUpsert(true).
  72. Script(NewScript(`ctx._source.retweets += param1`).Lang("javascript").Param("param1", 42)).
  73. Upsert(struct {
  74. Counter int64 `json:"counter"`
  75. }{
  76. Counter: 42,
  77. }),
  78. Expected: []string{
  79. `{"update":{"_id":"1","_index":"index1","_type":"tweet","_retry_on_conflict":3}}`,
  80. `{"upsert":{"counter":42},"script":{"inline":"ctx._source.retweets += param1","lang":"javascript","params":{"param1":42}},"scripted_upsert":true}`,
  81. },
  82. },
  83. }
  84. for i, test := range tests {
  85. lines, err := test.Request.Source()
  86. if err != nil {
  87. t.Fatalf("#%d: expected no error, got: %v", i, err)
  88. }
  89. if lines == nil {
  90. t.Fatalf("#%d: expected lines, got nil", i)
  91. }
  92. if len(lines) != len(test.Expected) {
  93. t.Fatalf("#%d: expected %d lines, got %d", i, len(test.Expected), len(lines))
  94. }
  95. for j, line := range lines {
  96. if line != test.Expected[j] {
  97. t.Errorf("#%d: expected line #%d to be\n%s\nbut got:\n%s", i, j, test.Expected[j], line)
  98. }
  99. }
  100. }
  101. }
  102. var bulkUpdateRequestSerializationResult string
  103. func BenchmarkBulkUpdateRequestSerialization(b *testing.B) {
  104. b.Run("stdlib", func(b *testing.B) {
  105. r := NewBulkUpdateRequest().Index("index1").Type("doc").Id("1").Doc(struct {
  106. Counter int64 `json:"counter"`
  107. }{
  108. Counter: 42,
  109. })
  110. benchmarkBulkUpdateRequestSerialization(b, r.UseEasyJSON(false))
  111. })
  112. b.Run("easyjson", func(b *testing.B) {
  113. r := NewBulkUpdateRequest().Index("index1").Type("doc").Id("1").Doc(struct {
  114. Counter int64 `json:"counter"`
  115. }{
  116. Counter: 42,
  117. }).UseEasyJSON(false)
  118. benchmarkBulkUpdateRequestSerialization(b, r.UseEasyJSON(true))
  119. })
  120. }
  121. func benchmarkBulkUpdateRequestSerialization(b *testing.B, r *BulkUpdateRequest) {
  122. var s string
  123. for n := 0; n < b.N; n++ {
  124. s = r.String()
  125. r.source = nil // Don't let caching spoil the benchmark
  126. }
  127. bulkUpdateRequestSerializationResult = s // ensure the compiler doesn't optimize
  128. b.ReportAllocs()
  129. }