Skip to content

Commit

Permalink
Experiment with Content-Length
Browse files Browse the repository at this point in the history
This commit experiments with different encodings for the HTTP request
body.

See #1526
See #1441 (PR)
  • Loading branch information
olivere committed Jan 7, 2022
1 parent 6b76405 commit cbb10cd
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 2 deletions.
4 changes: 3 additions & 1 deletion request.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ func (r *Request) setBodyReader(body io.Reader) error {
switch v := body.(type) {
case *strings.Reader:
r.ContentLength = int64(v.Len())
case *bytes.Buffer:
// case *bytes.Buffer:
// r.ContentLength = int64(v.Len())
case *bytes.Reader:
r.ContentLength = int64(v.Len())
}
}
Expand Down
96 changes: 95 additions & 1 deletion request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

package elastic

import "testing"
import (
"bytes"
"strings"
"testing"
)

var testReq *Request // used as a temporary variable to avoid compiler optimizations in tests/benchmarks

Expand All @@ -25,6 +29,64 @@ func TestRequestSetContentType(t *testing.T) {
}
}

func TestRequestContentLength(t *testing.T) {
tests := []struct {
Body interface{}
Compress bool
ExpectContentLength bool
ContentLength int64
}{
{
Body: `{}`,
Compress: false,
ExpectContentLength: true,
ContentLength: 2,
},
{
Body: strings.NewReader(`{}`),
Compress: false,
ExpectContentLength: true,
ContentLength: 2,
},
{
Body: bytes.NewBuffer([]byte(`{}`)),
Compress: false,
ExpectContentLength: true,
ContentLength: 2,
},
{
Body: bytes.NewReader([]byte(`{}`)),
Compress: false,
ExpectContentLength: true,
ContentLength: 2,
},
}

for i, tt := range tests {
req, err := NewRequest("POST", "/")
if err != nil {
t.Fatal(err)
}
if err := req.SetBody(tt.Body, tt.Compress); err != nil {
t.Fatal(err)
}
if tt.ExpectContentLength {
contentLength := req.ContentLength
if want, have := tt.ContentLength, contentLength; want != have {
t.Fatalf("%d. want Content-Length=%d, have %d", i, want, have)
}
} else {
if want, have := int64(0), req.ContentLength; want != have {
t.Fatalf("%d. want Content-Length=%d, have %d", i, want, have)
}
hdrContentLength := req.Header.Get("Content-Length")
if hdrContentLength != "" {
t.Fatalf("%d. want no Content-Length, have %q", i, hdrContentLength)
}
}
}
}

func BenchmarkRequestSetBodyString(b *testing.B) {
req, err := NewRequest("GET", "/")
if err != nil {
Expand Down Expand Up @@ -128,3 +190,35 @@ func BenchmarkRequestSetBodyMapGzip(b *testing.B) {
testReq = req
b.ReportAllocs()
}

func BenchmarkRequestSetBodyStringReader(b *testing.B) {
req, err := NewRequest("GET", "/")
if err != nil {
b.Fatal(err)
}
for i := 0; i < b.N; i++ {
body := strings.NewReader(`{"query":{"match_all":{}}}`)
err = req.SetBody(body, false)
if err != nil {
b.Fatal(err)
}
}
testReq = req
b.ReportAllocs()
}

func BenchmarkRequestSetBodyStringReaderGzip(b *testing.B) {
req, err := NewRequest("GET", "/")
if err != nil {
b.Fatal(err)
}
for i := 0; i < b.N; i++ {
body := strings.NewReader(`{"query":{"match_all":{}}}`)
err = req.SetBody(body, true)
if err != nil {
b.Fatal(err)
}
}
testReq = req
b.ReportAllocs()
}

0 comments on commit cbb10cd

Please sign in to comment.