logging.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // +build integration
  2. // Package performance contains shared step definitions that are used for performance testing
  3. package performance
  4. import (
  5. "errors"
  6. "fmt"
  7. "os"
  8. "time"
  9. "github.com/aws/aws-sdk-go/aws"
  10. "github.com/aws/aws-sdk-go/awstesting/unit"
  11. "github.com/aws/aws-sdk-go/service/dynamodb"
  12. "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
  13. )
  14. // benchmarkLogger handles all benchmark logging
  15. type benchmarkLogger struct {
  16. outputer
  17. }
  18. // logger interface that handles any logging to an output
  19. type logger interface {
  20. log(key string, data map[string]interface{}) error
  21. }
  22. // init initializes the logger and uses dependency injection for the
  23. // outputer
  24. func newBenchmarkLogger(output string) (*benchmarkLogger, error) {
  25. b := &benchmarkLogger{}
  26. switch output {
  27. case "dynamodb":
  28. region := os.Getenv("AWS_TESTING_REGION")
  29. if region == "" {
  30. return b, errors.New("No region specified. Please export AWS_TESTING_REGION")
  31. }
  32. table := os.Getenv("AWS_TESTING_DB_TABLE")
  33. if table == "" {
  34. return b, errors.New("No table specified. Please export AWS_TESTING_DB_TABLE")
  35. }
  36. b.outputer = newDynamodbOut(table, region)
  37. case "stdout":
  38. b.outputer = stdout{}
  39. default:
  40. return b, errors.New("Unsupported outputer")
  41. }
  42. return b, nil
  43. }
  44. type record struct {
  45. Key string
  46. Data interface{}
  47. }
  48. // log calls the output command and building a data structure
  49. // to pass into its output formatter
  50. func (b benchmarkLogger) log(key, data interface{}) error {
  51. formatData := record{
  52. Key: fmt.Sprintf("%d-%v", time.Now().Unix(), key.(string)),
  53. Data: data,
  54. }
  55. return b.output(formatData)
  56. }
  57. // outputer is a simple interface that'll handle output
  58. // to whatever system like dynamodb or stdout
  59. type outputer interface {
  60. output(record) error
  61. }
  62. // dyanmodbOut handles simple writes to dynamodb
  63. type dynamodbOut struct {
  64. table string // table to write to in dynamodb
  65. region string
  66. db *dynamodb.DynamoDB // the dynamodb
  67. }
  68. // init initializes dynamodbOut
  69. func newDynamodbOut(table, region string) *dynamodbOut {
  70. out := dynamodbOut{
  71. table: table,
  72. region: region,
  73. }
  74. out.db = dynamodb.New(
  75. unit.Session,
  76. &aws.Config{Region: &out.region},
  77. )
  78. return &out
  79. }
  80. // output just writes to dynamodb
  81. func (out dynamodbOut) output(data record) error {
  82. input := &dynamodb.PutItemInput{
  83. TableName: aws.String(out.table),
  84. }
  85. item, err := dynamodbattribute.ConvertToMap(data)
  86. if err != nil {
  87. return err
  88. }
  89. input.Item = item
  90. _, err = out.db.PutItem(input)
  91. return err
  92. }
  93. // stdout handles writes to stdout
  94. type stdout struct{}
  95. // output expects key value data to print to stdout
  96. func (out stdout) output(data record) error {
  97. item, err := dynamodbattribute.ConvertToMap(data.Data)
  98. if err != nil {
  99. return err
  100. }
  101. fmt.Println(item)
  102. return nil
  103. }