1
0
Fork 0
mirror of https://github.com/Luzifer/share.git synced 2024-12-26 05:21:19 +00:00
share/vendor/github.com/aws/aws-sdk-go/awstesting/performance/logging.go

123 lines
2.7 KiB
Go
Raw Normal View History

2017-12-02 14:25:54 +00:00
// +build integration
// Package performance contains shared step definitions that are used for performance testing
package performance
import (
"errors"
"fmt"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/awstesting/unit"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)
// benchmarkLogger handles all benchmark logging
type benchmarkLogger struct {
outputer
}
// logger interface that handles any logging to an output
type logger interface {
log(key string, data map[string]interface{}) error
}
// init initializes the logger and uses dependency injection for the
// outputer
func newBenchmarkLogger(output string) (*benchmarkLogger, error) {
b := &benchmarkLogger{}
switch output {
case "dynamodb":
region := os.Getenv("AWS_TESTING_REGION")
if region == "" {
return b, errors.New("No region specified. Please export AWS_TESTING_REGION")
}
table := os.Getenv("AWS_TESTING_DB_TABLE")
if table == "" {
return b, errors.New("No table specified. Please export AWS_TESTING_DB_TABLE")
}
b.outputer = newDynamodbOut(table, region)
case "stdout":
b.outputer = stdout{}
default:
return b, errors.New("Unsupported outputer")
}
return b, nil
}
type record struct {
Key string
Data interface{}
}
// log calls the output command and building a data structure
// to pass into its output formatter
func (b benchmarkLogger) log(key, data interface{}) error {
formatData := record{
Key: fmt.Sprintf("%d-%v", time.Now().Unix(), key.(string)),
Data: data,
}
return b.output(formatData)
}
// outputer is a simple interface that'll handle output
// to whatever system like dynamodb or stdout
type outputer interface {
output(record) error
}
// dyanmodbOut handles simple writes to dynamodb
type dynamodbOut struct {
table string // table to write to in dynamodb
region string
db *dynamodb.DynamoDB // the dynamodb
}
// init initializes dynamodbOut
func newDynamodbOut(table, region string) *dynamodbOut {
out := dynamodbOut{
table: table,
region: region,
}
out.db = dynamodb.New(
unit.Session,
&aws.Config{Region: &out.region},
)
return &out
}
// output just writes to dynamodb
func (out dynamodbOut) output(data record) error {
input := &dynamodb.PutItemInput{
TableName: aws.String(out.table),
}
item, err := dynamodbattribute.ConvertToMap(data)
if err != nil {
return err
}
input.Item = item
_, err = out.db.PutItem(input)
return err
}
// stdout handles writes to stdout
type stdout struct{}
// output expects key value data to print to stdout
func (out stdout) output(data record) error {
item, err := dynamodbattribute.ConvertToMap(data.Data)
if err != nil {
return err
}
fmt.Println(item)
return nil
}