request_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "testing"
  6. var testReq *Request // used as a temporary variable to avoid compiler optimizations in tests/benchmarks
  7. func TestRequestSetContentType(t *testing.T) {
  8. req, err := NewRequest("GET", "/")
  9. if err != nil {
  10. t.Fatal(err)
  11. }
  12. if want, have := "application/json", req.Header.Get("Content-Type"); want != have {
  13. t.Fatalf("want %q, have %q", want, have)
  14. }
  15. req.Header.Set("Content-Type", "application/x-ndjson")
  16. if want, have := "application/x-ndjson", req.Header.Get("Content-Type"); want != have {
  17. t.Fatalf("want %q, have %q", want, have)
  18. }
  19. }
  20. func BenchmarkRequestSetBodyString(b *testing.B) {
  21. req, err := NewRequest("GET", "/")
  22. if err != nil {
  23. b.Fatal(err)
  24. }
  25. for i := 0; i < b.N; i++ {
  26. body := `{"query":{"match_all":{}}}`
  27. err = req.SetBody(body, false)
  28. if err != nil {
  29. b.Fatal(err)
  30. }
  31. }
  32. testReq = req
  33. }
  34. func BenchmarkRequestSetBodyStringGzip(b *testing.B) {
  35. req, err := NewRequest("GET", "/")
  36. if err != nil {
  37. b.Fatal(err)
  38. }
  39. for i := 0; i < b.N; i++ {
  40. body := `{"query":{"match_all":{}}}`
  41. err = req.SetBody(body, true)
  42. if err != nil {
  43. b.Fatal(err)
  44. }
  45. }
  46. testReq = req
  47. }
  48. func BenchmarkRequestSetBodyBytes(b *testing.B) {
  49. req, err := NewRequest("GET", "/")
  50. if err != nil {
  51. b.Fatal(err)
  52. }
  53. for i := 0; i < b.N; i++ {
  54. body := []byte(`{"query":{"match_all":{}}}`)
  55. err = req.SetBody(body, false)
  56. if err != nil {
  57. b.Fatal(err)
  58. }
  59. }
  60. testReq = req
  61. }
  62. func BenchmarkRequestSetBodyBytesGzip(b *testing.B) {
  63. req, err := NewRequest("GET", "/")
  64. if err != nil {
  65. b.Fatal(err)
  66. }
  67. for i := 0; i < b.N; i++ {
  68. body := []byte(`{"query":{"match_all":{}}}`)
  69. err = req.SetBody(body, true)
  70. if err != nil {
  71. b.Fatal(err)
  72. }
  73. }
  74. testReq = req
  75. }
  76. func BenchmarkRequestSetBodyMap(b *testing.B) {
  77. req, err := NewRequest("GET", "/")
  78. if err != nil {
  79. b.Fatal(err)
  80. }
  81. for i := 0; i < b.N; i++ {
  82. body := map[string]interface{}{
  83. "query": map[string]interface{}{
  84. "match_all": map[string]interface{}{},
  85. },
  86. }
  87. err = req.SetBody(body, false)
  88. if err != nil {
  89. b.Fatal(err)
  90. }
  91. }
  92. testReq = req
  93. }
  94. func BenchmarkRequestSetBodyMapGzip(b *testing.B) {
  95. req, err := NewRequest("GET", "/")
  96. if err != nil {
  97. b.Fatal(err)
  98. }
  99. for i := 0; i < b.N; i++ {
  100. body := map[string]interface{}{
  101. "query": map[string]interface{}{
  102. "match_all": map[string]interface{}{},
  103. },
  104. }
  105. err = req.SetBody(body, true)
  106. if err != nil {
  107. b.Fatal(err)
  108. }
  109. }
  110. testReq = req
  111. }