This repository has been archived by the owner on Sep 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbenchmarks_test.go
109 lines (102 loc) · 2.86 KB
/
benchmarks_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package reflector_test
import (
. "."
"reflect"
"testing"
)
func BenchmarkStructToMap(b *testing.B) {
s := T{42, 8, 0xbadcafe, 3.14, "str", nil, 13}
m := make(map[string]interface{})
b.ResetTimer()
for i := 0; i < b.N; i++ {
StructToMap(&s, m, "json")
}
b.StopTimer()
expected := map[string]interface{}{
"Int": 42, "Uint8": uint8(8), "Uintptr": uintptr(0xbadcafe),
"f32": float32(3.14), "String": "str", "Pstring": nil,
}
if !reflect.DeepEqual(expected, m) {
b.Fatalf("%#v\n%#v", expected, m)
}
}
func BenchmarkStructValueToMap(b *testing.B) {
s := T{42, 8, 0xbadcafe, 3.14, "str", nil, 13}
m := make(map[string]interface{})
b.ResetTimer()
for i := 0; i < b.N; i++ {
StructValueToMap(s, m, "json")
}
b.StopTimer()
expected := map[string]interface{}{
"Int": 42, "Uint8": uint8(8), "Uintptr": uintptr(0xbadcafe),
"f32": float32(3.14), "String": "str", "Pstring": nil,
}
if !reflect.DeepEqual(expected, m) {
b.Fatalf("%#v\n%#v", expected, m)
}
}
func BenchmarkStructsToMaps(b *testing.B) {
s := []T{{42, 8, 0xbadcafe, 3.14, "str", nil, 13}}
var m []map[string]interface{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
StructsToMaps(s, &m, "json")
}
b.StopTimer()
expected := []map[string]interface{}{{
"Int": 42, "Uint8": uint8(8), "Uintptr": uintptr(0xbadcafe),
"f32": float32(3.14), "String": "str", "Pstring": nil,
}}
if !reflect.DeepEqual(expected, m) {
b.Fatalf("%#v\n%#v", expected, m)
}
}
func BenchmarkMapToStruct(b *testing.B) {
var s T
m := map[string]interface{}{
"Int": 42, "Uint8": uint8(8), "Uintptr": uintptr(0xbadcafe),
"f32": float32(3.14), "String": "str", "foo": 13,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
MapToStruct(m, &s, NoConvert, "json")
}
b.StopTimer()
expected := T{42, 8, 0xbadcafe, 3.14, "str", nil, 0}
if !reflect.DeepEqual(expected, s) {
b.Fatalf("Expected %#v, got %#v", expected, s)
}
}
func BenchmarkMapsToStructs(b *testing.B) {
var s []T
maps := []map[string]interface{}{
{"Int": 42, "Uint8": uint8(8), "Uintptr": uintptr(0xbadcafe)},
{"f32": float32(3.14), "String": "str", "foo": 13},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
MapsToStructs(maps, &s, NoConvert, "json")
}
b.StopTimer()
expected := []T{{42, 8, 0xbadcafe, 0, "", nil, 0}, {0, 0, 0, 3.14, "str", nil, 0}}
if !reflect.DeepEqual(expected, s) {
b.Fatalf("Expected %#v, got %#v", expected, s)
}
}
func BenchmarkMapsToStructs2(b *testing.B) {
var s []T
maps := []interface{}{
map[string]interface{}{"Int": 42, "Uint8": uint8(8), "Uintptr": uintptr(0xbadcafe)},
map[string]interface{}{"f32": float32(3.14), "String": "str", "foo": 13},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
MapsToStructs2(maps, &s, NoConvert, "json")
}
b.StopTimer()
expected := []T{{42, 8, 0xbadcafe, 0, "", nil, 0}, {0, 0, 0, 3.14, "str", nil, 0}}
if !reflect.DeepEqual(expected, s) {
b.Fatalf("Expected %#v, got %#v", expected, s)
}
}