Skip to content

Instantly share code, notes, and snippets.

@evalphobia
Last active December 26, 2019 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evalphobia/c1b436ef15038bc9fc9c588ca0163c93 to your computer and use it in GitHub Desktop.
Save evalphobia/c1b436ef15038bc9fc9c588ca0163c93 to your computer and use it in GitHub Desktop.
Golang Benchmark for Slice Map Decoder of DynamoDB result on github.com/evalphobia/aws-sdk-go-wrapper
package dynamodb
import (
"fmt"
"testing"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)
func BenchmarkToSliceMap_old(b *testing.B) {
res, err := getQueryResultForBenchmark()
if err != nil {
b.Errorf("%s", err.Error())
return
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
v := res.ToSliceMap_old()
if len(v) == 0 {
b.Errorf("Error on ToSliceMap_old")
}
}
}
func BenchmarkToSliceMap_dynamodbattribute(b *testing.B) {
res, err := getQueryResultForBenchmark()
if err != nil {
b.Errorf("%s", err.Error())
return
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
v := res.ToSliceMap_dynamodbattribute()
if len(v) == 0 {
b.Errorf("Error on ToSliceMap_dynamodbattribute")
}
}
}
func BenchmarkToSliceMap_dynamodbattributeNumber(b *testing.B) {
res, err := getQueryResultForBenchmark()
if err != nil {
b.Errorf("%s", err.Error())
return
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
v := res.ToSliceMap_dynamodbattributeNumber()
if len(v) == 0 {
b.Errorf("Error on ToSliceMap_dynamodbattributeNumber")
}
}
}
func BenchmarkToSliceMap_new(b *testing.B) {
res, err := getQueryResultForBenchmark()
if err != nil {
b.Errorf("%s", err.Error())
return
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
v := res.ToSliceMap_new()
if len(v) == 0 {
b.Errorf("Error on ToSliceMap_new")
}
}
}
func getQueryResultForBenchmark() (QueryResult, error) {
res := QueryResult{}
for _, v := range testData {
attrs, err := dynamodbattribute.MarshalMap(&v)
if err != nil {
return res, fmt.Errorf("Initializing error on dynamodbattribute.MarshalMap: [%v]", err)
}
res.Items = append(res.Items, attrs)
}
return res, nil
}
var testData = []map[string]interface{}{
{
"A_int": 100,
"B_int64": int64(100),
"C_string": "abcdefgf1",
"D_bool": true,
},
{
"A_int": 200,
"B_int64": int64(200),
"C_string": "abcdefgf2",
"D_bool": true,
},
{
"A_int": 300,
"B_int64": int64(300),
"C_string": "abcdefgf3",
"D_bool": true,
},
{
"A_int": 400,
"B_int64": int64(400),
"C_string": "abcdefgf4",
"D_bool": true,
},
{
"A_int": 105,
"B_int64": int64(105),
"C_string": "abcdefgf5",
"D_bool": true,
},
}
package dynamodb
import (
SDK "github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)
func (r QueryResult) ToSliceMap_old() []map[string]interface{} {
m := make([]map[string]interface{}, len(r.Items))
for i, item := range r.Items {
m[i] = UnmarshalAttributeValue(item)
}
return m
}
func (r QueryResult) ToSliceMap_new() []map[string]interface{} {
m := make([]map[string]interface{}, len(r.Items))
for i, item := range r.Items {
var v customeValues
err := dynamodbattribute.UnmarshalMap(item, &v)
if err != nil {
continue
}
m[i] = v.ToMap()
}
return m
}
type customeValues map[string]customeValue
func (v customeValues) ToMap() map[string]interface{} {
m := make(map[string]interface{}, len(v))
for key, val := range v {
m[key] = val.Value
}
return m
}
func (r QueryResult) ToSliceMap_dynamodbattribute() []map[string]interface{} {
m := make([]map[string]interface{}, len(r.Items))
for i, item := range r.Items {
var v map[string]interface{}
err := dynamodbattribute.UnmarshalMap(item, &v)
if err != nil {
continue
}
m[i] = v
}
return m
}
func (r QueryResult) ToSliceMap_dynamodbattributeNumber() []map[string]interface{} {
decoder := dynamodbattribute.NewDecoder(func(d *dynamodbattribute.Decoder) {
d.UseNumber = true
})
m := make([]map[string]interface{}, len(r.Items))
for i, item := range r.Items {
var v map[string]interface{}
err := decoder.Decode(&SDK.AttributeValue{M: item}, &v)
if err != nil {
continue
}
m[i] = v
}
return m
}
@evalphobia
Copy link
Author

$ go test  -bench . -benchmem

goos: darwin
goarch: amd64
pkg: github.com/evalphobia/aws-sdk-go-wrapper/dynamodb

BenchmarkToSliceMap_old-8                       	  482179	      2505 ns/op	    1888 B/op	      26 allocs/op
BenchmarkToSliceMap_dynamodbattribute-8         	  111409	     10594 ns/op	    3688 B/op	      91 allocs/op
BenchmarkToSliceMap_dynamodbattributeNumber-8   	  121496	      9796 ns/op	    3640 B/op	      87 allocs/op
BenchmarkToSliceMap_new-8                       	   79880	     14838 ns/op	    5688 B/op	     111 allocs/op

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment