github.com/aavshr/aws-sdk-go@v1.41.3/aws/request/timeout_read_closer_benchmark_test.go (about)

     1  package request_test
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/aavshr/aws-sdk-go/aws/client"
    11  	"github.com/aavshr/aws-sdk-go/aws/client/metadata"
    12  	"github.com/aavshr/aws-sdk-go/aws/request"
    13  	"github.com/aavshr/aws-sdk-go/aws/signer/v4"
    14  	"github.com/aavshr/aws-sdk-go/awstesting/unit"
    15  	"github.com/aavshr/aws-sdk-go/private/protocol/jsonrpc"
    16  )
    17  
    18  func BenchmarkTimeoutReadCloser(b *testing.B) {
    19  	resp := `
    20  	{
    21  		"Bar": "qux"
    22  	}
    23  	`
    24  
    25  	handlers := request.Handlers{}
    26  
    27  	handlers.Send.PushBack(func(r *request.Request) {
    28  		r.HTTPResponse = &http.Response{
    29  			StatusCode: http.StatusOK,
    30  			Body:       ioutil.NopCloser(bytes.NewBuffer([]byte(resp))),
    31  		}
    32  	})
    33  	handlers.Sign.PushBackNamed(v4.SignRequestHandler)
    34  	handlers.Build.PushBackNamed(jsonrpc.BuildHandler)
    35  	handlers.Unmarshal.PushBackNamed(jsonrpc.UnmarshalHandler)
    36  	handlers.UnmarshalMeta.PushBackNamed(jsonrpc.UnmarshalMetaHandler)
    37  	handlers.UnmarshalError.PushBackNamed(jsonrpc.UnmarshalErrorHandler)
    38  
    39  	op := &request.Operation{
    40  		Name:       "op",
    41  		HTTPMethod: "POST",
    42  		HTTPPath:   "/",
    43  	}
    44  
    45  	meta := metadata.ClientInfo{
    46  		ServiceName:   "fooService",
    47  		SigningName:   "foo",
    48  		SigningRegion: "foo",
    49  		Endpoint:      "localhost",
    50  		APIVersion:    "2001-01-01",
    51  		JSONVersion:   "1.1",
    52  		TargetPrefix:  "Foo",
    53  	}
    54  
    55  	req := request.New(
    56  		*unit.Session.Config,
    57  		meta,
    58  		handlers,
    59  		client.DefaultRetryer{NumMaxRetries: 5},
    60  		op,
    61  		&struct {
    62  			Foo *string
    63  		}{},
    64  		&struct {
    65  			Bar *string
    66  		}{},
    67  	)
    68  
    69  	req.ApplyOptions(request.WithResponseReadTimeout(15 * time.Second))
    70  	for i := 0; i < b.N; i++ {
    71  		err := req.Send()
    72  		if err != nil {
    73  			b.Errorf("Expected no error, but received %v", err)
    74  		}
    75  	}
    76  }