1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
package benchmark
import (
"bytes"
"context"
"io/ioutil"
"net/http"
"path/filepath"
"testing"
"github.com/aws/aws-sdk-go-v2/internal/awstesting/unit"
smithyClient "github.com/aws/aws-sdk-go-v2/service/lexruntimeservice"
v1Aws "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/corehandlers"
v1Creds "github.com/aws/aws-sdk-go/aws/credentials"
v1Request "github.com/aws/aws-sdk-go/aws/request"
v1Unit "github.com/aws/aws-sdk-go/awstesting/unit"
v1Client "github.com/aws/aws-sdk-go/service/lexruntimeservice"
"github.com/aws/smithy-go/ptr"
smithyhttp "github.com/aws/smithy-go/transport/http"
)
func BenchmarkGetSession(b *testing.B) {
filename := filepath.Join("testdata", "get_session_resp.json")
bodyBytes, err := loadTestData(filename)
if err != nil {
b.Fatalf("failed to load test data, %s, %v", filename, err)
}
b.Run("old", func(b *testing.B) {
benchGetSessionOld(b, bodyBytes)
})
b.Run("smithy", func(b *testing.B) {
benchGetSessionSmithy(b, bodyBytes)
})
}
func benchGetSessionOld(b *testing.B, respBytes []byte) {
sess := v1Unit.Session.Copy(&v1Aws.Config{
Credentials: v1Creds.NewStaticCredentials("AKID", "SECRET", ""),
Region: ptr.String("us-west-2"),
})
sess.Handlers.Send.SwapNamed(v1Request.NamedHandler{
Name: corehandlers.SendHandler.Name,
Fn: func(r *v1Request.Request) {
r.HTTPResponse = newGetSessionHTTPResponse(respBytes)
},
})
client := v1Client.New(sess)
params := v1Client.GetSessionInput{
BotAlias: ptr.String("fooAlias"),
BotName: ptr.String("fooName"),
CheckpointLabelFilter: ptr.String("fooFilter"),
UserId: ptr.String("fooUser"),
}
ctx := context.Background()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := client.GetSessionWithContext(ctx, ¶ms)
if err != nil {
b.Fatalf("failed to send request: %v", err)
}
}
})
}
func benchGetSessionSmithy(b *testing.B, respBytes []byte) {
var args []func(*smithyClient.Options)
if disableSmithySigning {
args = append(args, removeSmithySigner)
}
client := smithyClient.New(smithyClient.Options{
Region: "us-west-2",
Credentials: unit.StubCredentialsProvider{},
HTTPClient: smithyhttp.ClientDoFunc(
func(r *http.Request) (*http.Response, error) {
return newGetSessionHTTPResponse(respBytes), nil
}),
}, args...)
ctx := context.Background()
params := smithyClient.GetSessionInput{
BotAlias: ptr.String("fooAlias"),
BotName: ptr.String("fooName"),
CheckpointLabelFilter: ptr.String("fooFilter"),
UserId: ptr.String("fooUser"),
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := client.GetSession(ctx, ¶ms)
if err != nil {
b.Fatalf("failed to send: %v", err)
}
}
})
}
func newGetSessionHTTPResponse(body []byte) *http.Response {
return &http.Response{
StatusCode: 200,
Header: map[string][]string{
"Content-Type": {"application/json"},
},
ContentLength: int64(len(body)),
Body: ioutil.NopCloser(bytes.NewReader(body)),
}
}
|