Skip to content

Commit

Permalink
Benchmark fixes/improvements
Browse files Browse the repository at this point in the history
    - Fix DecodeBasic benchmark
        - Same instance of the Basic struct can't be reused between benchmarks
    - Move Person struct to the package level
    - Add pure JSON unmarshal benchmark
    - Run benchmarks in TravisCI to prevent future breakages
  • Loading branch information
dhui committed Nov 9, 2018
1 parent 3536a92 commit 61912d8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -6,3 +6,4 @@ go:

script:
- go test
- go test -bench . -benchmem
70 changes: 38 additions & 32 deletions mapstructure_benchmark_test.go
Expand Up @@ -5,14 +5,14 @@ import (
"testing"
)

func Benchmark_Decode(b *testing.B) {
type Person struct {
Name string
Age int
Emails []string
Extra map[string]string
}
type Person struct {
Name string
Age int
Emails []string
Extra map[string]string
}

func Benchmark_Decode(b *testing.B) {
input := map[string]interface{}{
"name": "Mitchell",
"age": 91,
Expand Down Expand Up @@ -41,13 +41,22 @@ func decodeViaJSON(data interface{}, v interface{}) error {
}

func Benchmark_DecodeViaJSON(b *testing.B) {
type Person struct {
Name string
Age int
Emails []string
Extra map[string]string
input := map[string]interface{}{
"name": "Mitchell",
"age": 91,
"emails": []string{"one", "two", "three"},
"extra": map[string]string{
"twitter": "mitchellh",
},
}

var result Person
for i := 0; i < b.N; i++ {
decodeViaJSON(input, &result)
}
}

func Benchmark_JSONUnmarshal(b *testing.B) {
input := map[string]interface{}{
"name": "Mitchell",
"age": 91,
Expand All @@ -57,25 +66,33 @@ func Benchmark_DecodeViaJSON(b *testing.B) {
},
}

inputB, err := json.Marshal(input)
if err != nil {
b.Fatal("Failed to marshal test input:", err)
}

var result Person
for i := 0; i < b.N; i++ {
decodeViaJSON(input, &result)
json.Unmarshal(inputB, &result)
}
}

func Benchmark_DecodeBasic(b *testing.B) {
input := map[string]interface{}{
"vstring": "foo",
"vint": 42,
"Vuint": 42,
"vbool": true,
"Vfloat": 42.42,
"vsilent": true,
"vdata": 42,
"vstring": "foo",
"vint": 42,
"Vuint": 42,
"vbool": true,
"Vfloat": 42.42,
"vsilent": true,
"vdata": 42,
"vjsonInt": json.Number("1234"),
"vjsonFloat": json.Number("1234.5"),
"vjsonNumber": json.Number("1234.5"),
}

var result Basic
for i := 0; i < b.N; i++ {
var result Basic
Decode(input, &result)
}
}
Expand Down Expand Up @@ -183,12 +200,6 @@ func Benchmark_DecodeSliceOfStruct(b *testing.B) {
}

func Benchmark_DecodeWeaklyTypedInput(b *testing.B) {
type Person struct {
Name string
Age int
Emails []string
}

// This input can come from anywhere, but typically comes from
// something like decoding JSON, generated by a weakly typed language
// such as PHP.
Expand All @@ -215,11 +226,6 @@ func Benchmark_DecodeWeaklyTypedInput(b *testing.B) {
}

func Benchmark_DecodeMetadata(b *testing.B) {
type Person struct {
Name string
Age int
}

input := map[string]interface{}{
"name": "Mitchell",
"age": 91,
Expand Down

0 comments on commit 61912d8

Please sign in to comment.