8 func Benchmark_Decode(b *testing.B) {
13 Extra map[string]string
16 input := map[string]interface{}{
19 "emails": []string{"one", "two", "three"},
20 "extra": map[string]string{
21 "twitter": "mitchellh",
26 for i := 0; i < b.N; i++ {
27 Decode(input, &result)
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)
40 return json.Unmarshal(b, v)
43 func Benchmark_DecodeViaJSON(b *testing.B) {
48 Extra map[string]string
51 input := map[string]interface{}{
54 "emails": []string{"one", "two", "three"},
55 "extra": map[string]string{
56 "twitter": "mitchellh",
61 for i := 0; i < b.N; i++ {
62 decodeViaJSON(input, &result)
66 func Benchmark_DecodeBasic(b *testing.B) {
67 input := map[string]interface{}{
78 for i := 0; i < b.N; i++ {
79 Decode(input, &result)
83 func Benchmark_DecodeEmbedded(b *testing.B) {
84 input := map[string]interface{}{
86 "Basic": map[string]interface{}{
87 "vstring": "innerfoo",
93 for i := 0; i < b.N; i++ {
94 Decode(input, &result)
98 func Benchmark_DecodeTypeConversion(b *testing.B) {
99 input := map[string]interface{}{
111 "BoolToString": true,
113 "FloatToUint": 42.42,
114 "FloatToBool": 42.42,
115 "FloatToString": 42.42,
117 "StringToUint": "42",
119 "StringToFloat": "42.42",
120 "SliceToMap": []interface{}{},
121 "MapToSlice": map[string]interface{}{},
124 var resultStrict TypeConversionResult
125 for i := 0; i < b.N; i++ {
126 Decode(input, &resultStrict)
130 func Benchmark_DecodeMap(b *testing.B) {
131 input := map[string]interface{}{
133 "vother": map[interface{}]interface{}{
140 for i := 0; i < b.N; i++ {
141 Decode(input, &result)
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"},
153 var result MapOfStruct
154 for i := 0; i < b.N; i++ {
155 Decode(input, &result)
159 func Benchmark_DecodeSlice(b *testing.B) {
160 input := map[string]interface{}{
162 "vbar": []string{"foo", "bar", "baz"},
166 for i := 0; i < b.N; i++ {
167 Decode(input, &result)
171 func Benchmark_DecodeSliceOfStruct(b *testing.B) {
172 input := map[string]interface{}{
173 "value": []map[string]interface{}{
179 var result SliceOfStruct
180 for i := 0; i < b.N; i++ {
181 Decode(input, &result)
185 func Benchmark_DecodeWeaklyTypedInput(b *testing.B) {
192 // This input can come from anywhere, but typically comes from
193 // something like decoding JSON, generated by a weakly typed language
195 input := map[string]interface{}{
196 "name": 123, // number => string
197 "age": "42", // string => number
198 "emails": map[string]interface{}{}, // empty map => empty array
202 config := &DecoderConfig{
203 WeaklyTypedInput: true,
207 decoder, err := NewDecoder(config)
212 for i := 0; i < b.N; i++ {
213 decoder.Decode(input)
217 func Benchmark_DecodeMetadata(b *testing.B) {
223 input := map[string]interface{}{
226 "email": "foo@bar.com",
231 config := &DecoderConfig{
236 decoder, err := NewDecoder(config)
241 for i := 0; i < b.N; i++ {
242 decoder.Decode(input)
246 func Benchmark_DecodeMetadataEmbedded(b *testing.B) {
247 input := map[string]interface{}{
253 var result EmbeddedSquash
254 config := &DecoderConfig{
259 decoder, err := NewDecoder(config)
261 b.Fatalf("err: %s", err)
264 for i := 0; i < b.N; i++ {
265 decoder.Decode(input)
269 func Benchmark_DecodeTagged(b *testing.B) {
270 input := map[string]interface{}{
276 for i := 0; i < b.N; i++ {
277 Decode(input, &result)