init.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // +build integration
  2. package performance
  3. import (
  4. "bytes"
  5. "errors"
  6. "fmt"
  7. "runtime"
  8. "github.com/gucumber/gucumber"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/aws/aws-sdk-go/aws"
  11. "github.com/aws/aws-sdk-go/aws/awserr"
  12. "github.com/aws/aws-sdk-go/awstesting/mock"
  13. "github.com/aws/aws-sdk-go/service/s3"
  14. )
  15. func init() {
  16. // Go loads all of its dependecies on compile
  17. gucumber.Given(`^I have loaded my SDK and its dependencies$`, func() {
  18. })
  19. // Performance
  20. gucumber.When(`^I create and discard (\d+) clients for each service$`, func(i1 int) {
  21. services := gucumber.World["services"].([]func())
  22. err := benchmarkTask(fmt.Sprintf("%d_create_and_discard_clients", i1), services, i1)
  23. gucumber.World["error"] = err
  24. })
  25. gucumber.Then(`^I should not have leaked any resources$`, func() {
  26. runtime.GC()
  27. err, ok := gucumber.World["error"].(awserr.Error)
  28. assert.False(gucumber.T, ok, "error returned")
  29. assert.NoError(gucumber.T, err)
  30. })
  31. gucumber.And(`^I have a list of services$`, func() {
  32. mapCreateClients()
  33. })
  34. gucumber.And(`^I take a snapshot of my resources$`, func() {
  35. // Can't take a memory snapshot here, because gucumber does some
  36. // allocation between each instruction leading to unreliable numbers
  37. })
  38. gucumber.When(`^I create a client for each service$`, func() {
  39. buildAnArrayOfClients()
  40. })
  41. gucumber.And("^I execute (\\d+) command\\(s\\) on each client$", func(i1 int) {
  42. clientFns := gucumber.World["clientFns"].([]func())
  43. err := benchmarkTask(fmt.Sprintf("%d_commands_on_clients", i1), clientFns, i1)
  44. gucumber.World["error"] = err
  45. })
  46. gucumber.And(`^I destroy all the clients$`, func() {
  47. delete(gucumber.World, "clientFns")
  48. runtime.GC()
  49. })
  50. gucumber.Given(`^I have a (\d+) byte file$`, func(i1 int) {
  51. gucumber.World["file"] = make([]byte, i1)
  52. })
  53. gucumber.When(`^I upload the file$`, func() {
  54. svc := s3.New(mock.Session)
  55. memStatStart := &runtime.MemStats{}
  56. runtime.ReadMemStats(memStatStart)
  57. gucumber.World["start"] = memStatStart
  58. svc.PutObjectRequest(&s3.PutObjectInput{
  59. Bucket: aws.String("bucketmesilly"),
  60. Key: aws.String("testKey"),
  61. Body: bytes.NewReader(gucumber.World["file"].([]byte)),
  62. })
  63. })
  64. gucumber.And(`then download the file$`, func() {
  65. svc := s3.New(mock.Session)
  66. svc.GetObjectRequest(&s3.GetObjectInput{
  67. Bucket: aws.String("bucketmesilly"),
  68. Key: aws.String("testKey"),
  69. })
  70. memStatEnd := &runtime.MemStats{}
  71. runtime.ReadMemStats(memStatEnd)
  72. memStatStart := gucumber.World["start"].(*runtime.MemStats)
  73. if memStatStart.Alloc < memStatEnd.Alloc {
  74. gucumber.World["error"] = errors.New("Leaked memory")
  75. }
  76. })
  77. }