github.com/aavshr/aws-sdk-go@v1.41.3/private/protocol/restxml/build_bench_test.go (about)

     1  //go:build bench
     2  // +build bench
     3  
     4  package restxml_test
     5  
     6  import (
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"os"
    10  	"testing"
    11  
    12  	"bytes"
    13  	"encoding/xml"
    14  
    15  	"github.com/aavshr/aws-sdk-go/aws"
    16  	"github.com/aavshr/aws-sdk-go/aws/credentials"
    17  	"github.com/aavshr/aws-sdk-go/aws/endpoints"
    18  	"github.com/aavshr/aws-sdk-go/aws/request"
    19  	"github.com/aavshr/aws-sdk-go/aws/session"
    20  	"github.com/aavshr/aws-sdk-go/private/protocol/restxml"
    21  	"github.com/aavshr/aws-sdk-go/service/cloudfront"
    22  	"github.com/aavshr/aws-sdk-go/service/s3"
    23  )
    24  
    25  var (
    26  	cloudfrontSvc *cloudfront.CloudFront
    27  	s3Svc         *s3.S3
    28  )
    29  
    30  func TestMain(m *testing.M) {
    31  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    32  		w.WriteHeader(http.StatusOK)
    33  	}))
    34  
    35  	sess := session.Must(session.NewSession(&aws.Config{
    36  		Credentials:      credentials.NewStaticCredentials("Key", "Secret", "Token"),
    37  		Endpoint:         aws.String(server.URL),
    38  		S3ForcePathStyle: aws.Bool(true),
    39  		DisableSSL:       aws.Bool(true),
    40  		Region:           aws.String(endpoints.UsWest2RegionID),
    41  	}))
    42  	cloudfrontSvc = cloudfront.New(sess)
    43  	s3Svc = s3.New(sess)
    44  
    45  	c := m.Run()
    46  	server.Close()
    47  	os.Exit(c)
    48  }
    49  
    50  func BenchmarkRESTXMLBuild_Complex_CFCreateDistro(b *testing.B) {
    51  	params := cloudfrontCreateDistributionInput()
    52  
    53  	benchRESTXMLBuild(b, func() *request.Request {
    54  		req, _ := cloudfrontSvc.CreateDistributionRequest(params)
    55  		return req
    56  	})
    57  }
    58  
    59  func BenchmarkRESTXMLBuild_Simple_CFDeleteDistro(b *testing.B) {
    60  	params := cloudfrontDeleteDistributionInput()
    61  
    62  	benchRESTXMLBuild(b, func() *request.Request {
    63  		req, _ := cloudfrontSvc.DeleteDistributionRequest(params)
    64  		return req
    65  	})
    66  }
    67  
    68  func BenchmarkRESTXMLBuild_REST_S3HeadObject(b *testing.B) {
    69  	params := s3HeadObjectInput()
    70  
    71  	benchRESTXMLBuild(b, func() *request.Request {
    72  		req, _ := s3Svc.HeadObjectRequest(params)
    73  		return req
    74  	})
    75  }
    76  
    77  func BenchmarkRESTXMLBuild_XML_S3PutObjectAcl(b *testing.B) {
    78  	params := s3PutObjectAclInput()
    79  
    80  	benchRESTXMLBuild(b, func() *request.Request {
    81  		req, _ := s3Svc.PutObjectAclRequest(params)
    82  		return req
    83  	})
    84  }
    85  
    86  func BenchmarkRESTXMLRequest_Complex_CFCreateDistro(b *testing.B) {
    87  	benchRESTXMLRequest(b, func() *request.Request {
    88  		req, _ := cloudfrontSvc.CreateDistributionRequest(cloudfrontCreateDistributionInput())
    89  		return req
    90  	})
    91  }
    92  
    93  func BenchmarkRESTXMLRequest_Simple_CFDeleteDistro(b *testing.B) {
    94  	benchRESTXMLRequest(b, func() *request.Request {
    95  		req, _ := cloudfrontSvc.DeleteDistributionRequest(cloudfrontDeleteDistributionInput())
    96  		return req
    97  	})
    98  }
    99  
   100  func BenchmarkRESTXMLRequest_REST_S3HeadObject(b *testing.B) {
   101  	benchRESTXMLRequest(b, func() *request.Request {
   102  		req, _ := s3Svc.HeadObjectRequest(s3HeadObjectInput())
   103  		return req
   104  	})
   105  }
   106  
   107  func BenchmarkRESTXMLRequest_XML_S3PutObjectAcl(b *testing.B) {
   108  	benchRESTXMLRequest(b, func() *request.Request {
   109  		req, _ := s3Svc.PutObjectAclRequest(s3PutObjectAclInput())
   110  		return req
   111  	})
   112  }
   113  
   114  func BenchmarkEncodingXML_Simple(b *testing.B) {
   115  	params := cloudfrontDeleteDistributionInput()
   116  
   117  	for i := 0; i < b.N; i++ {
   118  		buf := &bytes.Buffer{}
   119  		encoder := xml.NewEncoder(buf)
   120  		if err := encoder.Encode(params); err != nil {
   121  			b.Fatal("Unexpected error", err)
   122  		}
   123  	}
   124  }
   125  
   126  func benchRESTXMLBuild(b *testing.B, reqFn func() *request.Request) {
   127  	b.ResetTimer()
   128  
   129  	for i := 0; i < b.N; i++ {
   130  		req := reqFn()
   131  		restxml.Build(req)
   132  		if req.Error != nil {
   133  			b.Fatal("Unexpected error", req.Error)
   134  		}
   135  	}
   136  }
   137  
   138  func benchRESTXMLRequest(b *testing.B, reqFn func() *request.Request) {
   139  	b.ResetTimer()
   140  
   141  	for i := 0; i < b.N; i++ {
   142  		err := reqFn().Send()
   143  		if err != nil {
   144  			b.Fatal("Unexpected error", err)
   145  		}
   146  	}
   147  }
   148  
   149  func cloudfrontCreateDistributionInput() *cloudfront.CreateDistributionInput {
   150  	return &cloudfront.CreateDistributionInput{
   151  		DistributionConfig: &cloudfront.DistributionConfig{ // Required
   152  			CallerReference: aws.String("string"), // Required
   153  			Comment:         aws.String("string"), // Required
   154  			DefaultCacheBehavior: &cloudfront.DefaultCacheBehavior{ // Required
   155  				ForwardedValues: &cloudfront.ForwardedValues{ // Required
   156  					Cookies: &cloudfront.CookiePreference{ // Required
   157  						Forward: aws.String("ItemSelection"), // Required
   158  						WhitelistedNames: &cloudfront.CookieNames{
   159  							Quantity: aws.Int64(1), // Required
   160  							Items: []*string{
   161  								aws.String("string"), // Required
   162  								// More values...
   163  							},
   164  						},
   165  					},
   166  					QueryString: aws.Bool(true), // Required
   167  					Headers: &cloudfront.Headers{
   168  						Quantity: aws.Int64(1), // Required
   169  						Items: []*string{
   170  							aws.String("string"), // Required
   171  							// More values...
   172  						},
   173  					},
   174  				},
   175  				MinTTL:         aws.Int64(1),         // Required
   176  				TargetOriginId: aws.String("string"), // Required
   177  				TrustedSigners: &cloudfront.TrustedSigners{ // Required
   178  					Enabled:  aws.Bool(true), // Required
   179  					Quantity: aws.Int64(1),   // Required
   180  					Items: []*string{
   181  						aws.String("string"), // Required
   182  						// More values...
   183  					},
   184  				},
   185  				ViewerProtocolPolicy: aws.String("ViewerProtocolPolicy"), // Required
   186  				AllowedMethods: &cloudfront.AllowedMethods{
   187  					Items: []*string{ // Required
   188  						aws.String("Method"), // Required
   189  						// More values...
   190  					},
   191  					Quantity: aws.Int64(1), // Required
   192  					CachedMethods: &cloudfront.CachedMethods{
   193  						Items: []*string{ // Required
   194  							aws.String("Method"), // Required
   195  							// More values...
   196  						},
   197  						Quantity: aws.Int64(1), // Required
   198  					},
   199  				},
   200  				DefaultTTL:      aws.Int64(1),
   201  				MaxTTL:          aws.Int64(1),
   202  				SmoothStreaming: aws.Bool(true),
   203  			},
   204  			Enabled: aws.Bool(true), // Required
   205  			Origins: &cloudfront.Origins{ // Required
   206  				Quantity: aws.Int64(1), // Required
   207  				Items: []*cloudfront.Origin{
   208  					{ // Required
   209  						DomainName: aws.String("string"), // Required
   210  						Id:         aws.String("string"), // Required
   211  						CustomOriginConfig: &cloudfront.CustomOriginConfig{
   212  							HTTPPort:             aws.Int64(1),                       // Required
   213  							HTTPSPort:            aws.Int64(1),                       // Required
   214  							OriginProtocolPolicy: aws.String("OriginProtocolPolicy"), // Required
   215  						},
   216  						OriginPath: aws.String("string"),
   217  						S3OriginConfig: &cloudfront.S3OriginConfig{
   218  							OriginAccessIdentity: aws.String("string"), // Required
   219  						},
   220  					},
   221  					// More values...
   222  				},
   223  			},
   224  			Aliases: &cloudfront.Aliases{
   225  				Quantity: aws.Int64(1), // Required
   226  				Items: []*string{
   227  					aws.String("string"), // Required
   228  					// More values...
   229  				},
   230  			},
   231  			CacheBehaviors: &cloudfront.CacheBehaviors{
   232  				Quantity: aws.Int64(1), // Required
   233  				Items: []*cloudfront.CacheBehavior{
   234  					{ // Required
   235  						ForwardedValues: &cloudfront.ForwardedValues{ // Required
   236  							Cookies: &cloudfront.CookiePreference{ // Required
   237  								Forward: aws.String("ItemSelection"), // Required
   238  								WhitelistedNames: &cloudfront.CookieNames{
   239  									Quantity: aws.Int64(1), // Required
   240  									Items: []*string{
   241  										aws.String("string"), // Required
   242  										// More values...
   243  									},
   244  								},
   245  							},
   246  							QueryString: aws.Bool(true), // Required
   247  							Headers: &cloudfront.Headers{
   248  								Quantity: aws.Int64(1), // Required
   249  								Items: []*string{
   250  									aws.String("string"), // Required
   251  									// More values...
   252  								},
   253  							},
   254  						},
   255  						MinTTL:         aws.Int64(1),         // Required
   256  						PathPattern:    aws.String("string"), // Required
   257  						TargetOriginId: aws.String("string"), // Required
   258  						TrustedSigners: &cloudfront.TrustedSigners{ // Required
   259  							Enabled:  aws.Bool(true), // Required
   260  							Quantity: aws.Int64(1),   // Required
   261  							Items: []*string{
   262  								aws.String("string"), // Required
   263  								// More values...
   264  							},
   265  						},
   266  						ViewerProtocolPolicy: aws.String("ViewerProtocolPolicy"), // Required
   267  						AllowedMethods: &cloudfront.AllowedMethods{
   268  							Items: []*string{ // Required
   269  								aws.String("Method"), // Required
   270  								// More values...
   271  							},
   272  							Quantity: aws.Int64(1), // Required
   273  							CachedMethods: &cloudfront.CachedMethods{
   274  								Items: []*string{ // Required
   275  									aws.String("Method"), // Required
   276  									// More values...
   277  								},
   278  								Quantity: aws.Int64(1), // Required
   279  							},
   280  						},
   281  						DefaultTTL:      aws.Int64(1),
   282  						MaxTTL:          aws.Int64(1),
   283  						SmoothStreaming: aws.Bool(true),
   284  					},
   285  					// More values...
   286  				},
   287  			},
   288  			CustomErrorResponses: &cloudfront.CustomErrorResponses{
   289  				Quantity: aws.Int64(1), // Required
   290  				Items: []*cloudfront.CustomErrorResponse{
   291  					{ // Required
   292  						ErrorCode:          aws.Int64(1), // Required
   293  						ErrorCachingMinTTL: aws.Int64(1),
   294  						ResponseCode:       aws.String("string"),
   295  						ResponsePagePath:   aws.String("string"),
   296  					},
   297  					// More values...
   298  				},
   299  			},
   300  			DefaultRootObject: aws.String("string"),
   301  			Logging: &cloudfront.LoggingConfig{
   302  				Bucket:         aws.String("string"), // Required
   303  				Enabled:        aws.Bool(true),       // Required
   304  				IncludeCookies: aws.Bool(true),       // Required
   305  				Prefix:         aws.String("string"), // Required
   306  			},
   307  			PriceClass: aws.String("PriceClass"),
   308  			Restrictions: &cloudfront.Restrictions{
   309  				GeoRestriction: &cloudfront.GeoRestriction{ // Required
   310  					Quantity:        aws.Int64(1),                     // Required
   311  					RestrictionType: aws.String("GeoRestrictionType"), // Required
   312  					Items: []*string{
   313  						aws.String("string"), // Required
   314  						// More values...
   315  					},
   316  				},
   317  			},
   318  			ViewerCertificate: &cloudfront.ViewerCertificate{
   319  				CloudFrontDefaultCertificate: aws.Bool(true),
   320  				IAMCertificateId:             aws.String("string"),
   321  				MinimumProtocolVersion:       aws.String("MinimumProtocolVersion"),
   322  				SSLSupportMethod:             aws.String("SSLSupportMethod"),
   323  			},
   324  		},
   325  	}
   326  }
   327  
   328  func cloudfrontDeleteDistributionInput() *cloudfront.DeleteDistributionInput {
   329  	return &cloudfront.DeleteDistributionInput{
   330  		Id:      aws.String("string"), // Required
   331  		IfMatch: aws.String("string"),
   332  	}
   333  }
   334  
   335  func s3HeadObjectInput() *s3.HeadObjectInput {
   336  	return &s3.HeadObjectInput{
   337  		Bucket:    aws.String("somebucketname"),
   338  		Key:       aws.String("keyname"),
   339  		VersionId: aws.String("someVersion"),
   340  		IfMatch:   aws.String("IfMatch"),
   341  	}
   342  }
   343  
   344  func s3PutObjectAclInput() *s3.PutObjectAclInput {
   345  	return &s3.PutObjectAclInput{
   346  		Bucket: aws.String("somebucketname"),
   347  		Key:    aws.String("keyname"),
   348  		AccessControlPolicy: &s3.AccessControlPolicy{
   349  			Grants: []*s3.Grant{
   350  				{
   351  					Grantee: &s3.Grantee{
   352  						DisplayName:  aws.String("someName"),
   353  						EmailAddress: aws.String("someAddr"),
   354  						ID:           aws.String("someID"),
   355  						Type:         aws.String(s3.TypeCanonicalUser),
   356  						URI:          aws.String("someURI"),
   357  					},
   358  					Permission: aws.String(s3.PermissionWrite),
   359  				},
   360  			},
   361  			Owner: &s3.Owner{
   362  				DisplayName: aws.String("howdy"),
   363  				ID:          aws.String("someID"),
   364  			},
   365  		},
   366  	}
   367  }