Last active
December 26, 2019 11:40
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
}, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Author
evalphobia
commented
Dec 26, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment