1package benchmark
2
3import (
4	"bytes"
5	"context"
6	"io/ioutil"
7	"net/http"
8	"testing"
9
10	"github.com/aws/aws-sdk-go-v2/internal/awstesting/unit"
11	smithyClient "github.com/aws/aws-sdk-go-v2/service/lexruntimeservice"
12	"github.com/aws/aws-sdk-go-v2/service/lexruntimeservice/types"
13	v1Aws "github.com/aws/aws-sdk-go/aws"
14	"github.com/aws/aws-sdk-go/aws/corehandlers"
15	v1Creds "github.com/aws/aws-sdk-go/aws/credentials"
16	v1Request "github.com/aws/aws-sdk-go/aws/request"
17	v1Unit "github.com/aws/aws-sdk-go/awstesting/unit"
18	v1Client "github.com/aws/aws-sdk-go/service/lexruntimeservice"
19	"github.com/aws/smithy-go/ptr"
20	smithyhttp "github.com/aws/smithy-go/transport/http"
21)
22
23func BenchmarkPutSession(b *testing.B) {
24	b.Run("old", func(b *testing.B) {
25		benchPutSessionOld(b)
26	})
27
28	b.Run("smithy", func(b *testing.B) {
29		benchPutSessionSmithy(b)
30	})
31}
32
33func benchPutSessionOld(b *testing.B) {
34	sess := v1Unit.Session.Copy(&v1Aws.Config{
35		Credentials: v1Creds.NewStaticCredentials("AKID", "SECRET", ""),
36		Region:      ptr.String("us-west-2"),
37	})
38	sess.Handlers.Send.SwapNamed(v1Request.NamedHandler{
39		Name: corehandlers.SendHandler.Name,
40		Fn: func(r *v1Request.Request) {
41			r.HTTPResponse = newPutSessionHTTPResponse()
42		},
43	})
44
45	client := v1Client.New(sess)
46
47	ctx := context.Background()
48	params := v1Client.PutSessionInput{
49		Accept:   ptr.String("text/plain"),
50		BotAlias: ptr.String("fooAlias"),
51		BotName:  ptr.String("fooName"),
52		DialogAction: &v1Client.DialogAction{
53			FulfillmentState: ptr.String(v1Client.FulfillmentStateFulfilled),
54			IntentName:       ptr.String("fooIntent"),
55			Message:          ptr.String("fooMessage"),
56			MessageFormat:    ptr.String(v1Client.MessageFormatTypePlainText),
57			SlotToElicit:     ptr.String("fooSlot"),
58			Slots: ptr.StringMap(map[string]string{
59				"fooSlot": "fooValue",
60				"barSlot": "barValue",
61			}),
62			Type: ptr.String(v1Client.DialogActionTypeElicitSlot),
63		},
64		RecentIntentSummaryView: []*v1Client.IntentSummary{
65			{
66				CheckpointLabel:    ptr.String("fooLabel"),
67				ConfirmationStatus: ptr.String(v1Client.ConfirmationStatusConfirmed),
68				DialogActionType:   ptr.String(v1Client.DialogActionTypeElicitSlot),
69				FulfillmentState:   ptr.String(v1Client.FulfillmentStateFulfilled),
70				IntentName:         ptr.String("fooIntent"),
71				SlotToElicit:       ptr.String("fooSlot"),
72				Slots: ptr.StringMap(map[string]string{
73					"fooSlot": "fooValue",
74					"barSlot": "barValue",
75				}),
76			},
77		},
78		SessionAttributes: ptr.StringMap(map[string]string{
79			"fooAttr": "fooValue",
80		}),
81		UserId: ptr.String("fooUser"),
82	}
83
84	b.ResetTimer()
85	b.RunParallel(func(pb *testing.PB) {
86		for pb.Next() {
87			_, err := client.PutSessionWithContext(ctx, &params)
88			if err != nil {
89				b.Errorf("failed to send request: %v", err)
90			}
91		}
92	})
93}
94
95func benchPutSessionSmithy(b *testing.B) {
96	var args []func(*smithyClient.Options)
97	if disableSmithySigning {
98		args = append(args, removeSmithySigner)
99	}
100
101	client := smithyClient.New(smithyClient.Options{
102		Region:      "us-west-2",
103		Credentials: unit.StubCredentialsProvider{},
104		HTTPClient: smithyhttp.ClientDoFunc(
105			func(r *http.Request) (*http.Response, error) {
106				return newPutSessionHTTPResponse(), nil
107			}),
108	}, args...)
109
110	ctx := context.Background()
111	params := smithyClient.PutSessionInput{
112		Accept:   ptr.String("text/plain"),
113		BotAlias: ptr.String("fooAlias"),
114		BotName:  ptr.String("fooName"),
115		DialogAction: &types.DialogAction{
116			FulfillmentState: types.FulfillmentStateFulfilled,
117			IntentName:       ptr.String("fooIntent"),
118			Message:          ptr.String("fooMessage"),
119			MessageFormat:    types.MessageFormatTypePlainText,
120			SlotToElicit:     ptr.String("fooSlot"),
121			Slots: map[string]string{
122				"fooSlot": "fooValue",
123				"barSlot": "barValue",
124			},
125			Type: types.DialogActionTypeElicitSlot,
126		},
127		RecentIntentSummaryView: []types.IntentSummary{
128			{
129				CheckpointLabel:    ptr.String("fooLabel"),
130				ConfirmationStatus: types.ConfirmationStatusConfirmed,
131				DialogActionType:   types.DialogActionTypeElicitSlot,
132				FulfillmentState:   types.FulfillmentStateFulfilled,
133				IntentName:         ptr.String("fooIntent"),
134				SlotToElicit:       ptr.String("fooSlot"),
135				Slots: map[string]string{
136					"fooSlot": "fooValue",
137					"barSlot": "barValue",
138				},
139			},
140		},
141		SessionAttributes: map[string]string{
142			"fooAttr": "fooValue",
143		},
144		UserId: ptr.String("fooUser"),
145	}
146
147	b.ResetTimer()
148	b.RunParallel(func(pb *testing.PB) {
149		for pb.Next() {
150			_, err := client.PutSession(ctx, &params)
151			if err != nil {
152				b.Errorf("failed to send request: %v", err)
153			}
154		}
155	})
156}
157
158var putSessionBody = []byte("fooAudioStream")
159
160func newPutSessionHTTPResponse() *http.Response {
161	return &http.Response{
162		StatusCode:    200,
163		ContentLength: int64(len(putSessionBody)),
164		Header: map[string][]string{
165			"Content-Type":                 {"application/octet-stream"},
166			"x-amz-lex-dialog-state":       {"Fulfilled"},
167			"x-amz-lex-intent-name":        {"fooIntent"},
168			"x-amz-lex-message":            {"fooMessage"},
169			"x-amz-lex-message-format":     {"PlainText"},
170			"x-amz-lex-session-attributes": {"eyJmb29LZXkiOiAiZm9vVmFsdWUifQ=="},
171			"x-amz-lex-session-id":         {"fooSession"},
172			"x-amz-lex-slot-to-elicit":     {"fooSlot"},
173			"x-amz-lex-slots":              {"eyJmb29LZXkiOiAiZm9vVmFsdWUifQ=="},
174		},
175		Body: ioutil.NopCloser(bytes.NewReader(putSessionBody)),
176	}
177}
178