OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / mitchellh / mapstructure / mapstructure_benchmark_test.go
1 package mapstructure
2
3 import (
4         "encoding/json"
5         "testing"
6 )
7
8 func Benchmark_Decode(b *testing.B) {
9         type Person struct {
10                 Name   string
11                 Age    int
12                 Emails []string
13                 Extra  map[string]string
14         }
15
16         input := map[string]interface{}{
17                 "name":   "Mitchell",
18                 "age":    91,
19                 "emails": []string{"one", "two", "three"},
20                 "extra": map[string]string{
21                         "twitter": "mitchellh",
22                 },
23         }
24
25         var result Person
26         for i := 0; i < b.N; i++ {
27                 Decode(input, &result)
28         }
29 }
30
31 // decodeViaJSON takes the map data and passes it through encoding/json to convert it into the
32 // given Go native structure pointed to by v. v must be a pointer to a struct.
33 func decodeViaJSON(data interface{}, v interface{}) error {
34         // Perform the task by simply marshalling the input into JSON,
35         // then unmarshalling it into target native Go struct.
36         b, err := json.Marshal(data)
37         if err != nil {
38                 return err
39         }
40         return json.Unmarshal(b, v)
41 }
42
43 func Benchmark_DecodeViaJSON(b *testing.B) {
44         type Person struct {
45                 Name   string
46                 Age    int
47                 Emails []string
48                 Extra  map[string]string
49         }
50
51         input := map[string]interface{}{
52                 "name":   "Mitchell",
53                 "age":    91,
54                 "emails": []string{"one", "two", "three"},
55                 "extra": map[string]string{
56                         "twitter": "mitchellh",
57                 },
58         }
59
60         var result Person
61         for i := 0; i < b.N; i++ {
62                 decodeViaJSON(input, &result)
63         }
64 }
65
66 func Benchmark_DecodeBasic(b *testing.B) {
67         input := map[string]interface{}{
68                 "vstring": "foo",
69                 "vint":    42,
70                 "Vuint":   42,
71                 "vbool":   true,
72                 "Vfloat":  42.42,
73                 "vsilent": true,
74                 "vdata":   42,
75         }
76
77         var result Basic
78         for i := 0; i < b.N; i++ {
79                 Decode(input, &result)
80         }
81 }
82
83 func Benchmark_DecodeEmbedded(b *testing.B) {
84         input := map[string]interface{}{
85                 "vstring": "foo",
86                 "Basic": map[string]interface{}{
87                         "vstring": "innerfoo",
88                 },
89                 "vunique": "bar",
90         }
91
92         var result Embedded
93         for i := 0; i < b.N; i++ {
94                 Decode(input, &result)
95         }
96 }
97
98 func Benchmark_DecodeTypeConversion(b *testing.B) {
99         input := map[string]interface{}{
100                 "IntToFloat":    42,
101                 "IntToUint":     42,
102                 "IntToBool":     1,
103                 "IntToString":   42,
104                 "UintToInt":     42,
105                 "UintToFloat":   42,
106                 "UintToBool":    42,
107                 "UintToString":  42,
108                 "BoolToInt":     true,
109                 "BoolToUint":    true,
110                 "BoolToFloat":   true,
111                 "BoolToString":  true,
112                 "FloatToInt":    42.42,
113                 "FloatToUint":   42.42,
114                 "FloatToBool":   42.42,
115                 "FloatToString": 42.42,
116                 "StringToInt":   "42",
117                 "StringToUint":  "42",
118                 "StringToBool":  "1",
119                 "StringToFloat": "42.42",
120                 "SliceToMap":    []interface{}{},
121                 "MapToSlice":    map[string]interface{}{},
122         }
123
124         var resultStrict TypeConversionResult
125         for i := 0; i < b.N; i++ {
126                 Decode(input, &resultStrict)
127         }
128 }
129
130 func Benchmark_DecodeMap(b *testing.B) {
131         input := map[string]interface{}{
132                 "vfoo": "foo",
133                 "vother": map[interface{}]interface{}{
134                         "foo": "foo",
135                         "bar": "bar",
136                 },
137         }
138
139         var result Map
140         for i := 0; i < b.N; i++ {
141                 Decode(input, &result)
142         }
143 }
144
145 func Benchmark_DecodeMapOfStruct(b *testing.B) {
146         input := map[string]interface{}{
147                 "value": map[string]interface{}{
148                         "foo": map[string]string{"vstring": "one"},
149                         "bar": map[string]string{"vstring": "two"},
150                 },
151         }
152
153         var result MapOfStruct
154         for i := 0; i < b.N; i++ {
155                 Decode(input, &result)
156         }
157 }
158
159 func Benchmark_DecodeSlice(b *testing.B) {
160         input := map[string]interface{}{
161                 "vfoo": "foo",
162                 "vbar": []string{"foo", "bar", "baz"},
163         }
164
165         var result Slice
166         for i := 0; i < b.N; i++ {
167                 Decode(input, &result)
168         }
169 }
170
171 func Benchmark_DecodeSliceOfStruct(b *testing.B) {
172         input := map[string]interface{}{
173                 "value": []map[string]interface{}{
174                         {"vstring": "one"},
175                         {"vstring": "two"},
176                 },
177         }
178
179         var result SliceOfStruct
180         for i := 0; i < b.N; i++ {
181                 Decode(input, &result)
182         }
183 }
184
185 func Benchmark_DecodeWeaklyTypedInput(b *testing.B) {
186         type Person struct {
187                 Name   string
188                 Age    int
189                 Emails []string
190         }
191
192         // This input can come from anywhere, but typically comes from
193         // something like decoding JSON, generated by a weakly typed language
194         // such as PHP.
195         input := map[string]interface{}{
196                 "name":   123,                      // number => string
197                 "age":    "42",                     // string => number
198                 "emails": map[string]interface{}{}, // empty map => empty array
199         }
200
201         var result Person
202         config := &DecoderConfig{
203                 WeaklyTypedInput: true,
204                 Result:           &result,
205         }
206
207         decoder, err := NewDecoder(config)
208         if err != nil {
209                 panic(err)
210         }
211
212         for i := 0; i < b.N; i++ {
213                 decoder.Decode(input)
214         }
215 }
216
217 func Benchmark_DecodeMetadata(b *testing.B) {
218         type Person struct {
219                 Name string
220                 Age  int
221         }
222
223         input := map[string]interface{}{
224                 "name":  "Mitchell",
225                 "age":   91,
226                 "email": "foo@bar.com",
227         }
228
229         var md Metadata
230         var result Person
231         config := &DecoderConfig{
232                 Metadata: &md,
233                 Result:   &result,
234         }
235
236         decoder, err := NewDecoder(config)
237         if err != nil {
238                 panic(err)
239         }
240
241         for i := 0; i < b.N; i++ {
242                 decoder.Decode(input)
243         }
244 }
245
246 func Benchmark_DecodeMetadataEmbedded(b *testing.B) {
247         input := map[string]interface{}{
248                 "vstring": "foo",
249                 "vunique": "bar",
250         }
251
252         var md Metadata
253         var result EmbeddedSquash
254         config := &DecoderConfig{
255                 Metadata: &md,
256                 Result:   &result,
257         }
258
259         decoder, err := NewDecoder(config)
260         if err != nil {
261                 b.Fatalf("err: %s", err)
262         }
263
264         for i := 0; i < b.N; i++ {
265                 decoder.Decode(input)
266         }
267 }
268
269 func Benchmark_DecodeTagged(b *testing.B) {
270         input := map[string]interface{}{
271                 "foo": "bar",
272                 "bar": "value",
273         }
274
275         var result Tagged
276         for i := 0; i < b.N; i++ {
277                 Decode(input, &result)
278         }
279 }