forked from monoculum/formam-benchmark
/
formam_1_test.go
119 lines (111 loc) · 2.74 KB
/
formam_1_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
110
111
112
113
114
115
116
117
118
119
package formam_benchmark
import (
"encoding/json"
"github.com/ajg/form"
"github.com/monoculum/formam"
"net/url"
"testing"
)
type Anonymous struct {
Int int `formam:"int" json:"int"`
}
type Bench struct {
Nest struct {
Children []struct {
Id string
Name string
}
}
String string `formam:"string" json:"string"`
Slice []int
Map map[string][]string
Bool bool
Ptr *string
Anonymous
}
type BenchAJG struct {
Nest struct {
Children []struct {
Id string
Name string
}
}
String string `form:"string"`
Slice []int
Map map[string][]string
Int int `form:"int"`
Bool bool
Anonymous
}
var (
valuesFormamT1 = url.Values{
"Nest.Children[0].Id": []string{"monoculum_id"},
"Nest.Children[0].Name": []string{"Monoculum"},
"Map.es_Es[0]": []string{"javier"},
"Map.es_Es[1]": []string{"javier"},
"Map.es_Es[2]": []string{"javier"},
"Map.es_Es[3]": []string{"javier"},
"Map.es_Es[4]": []string{"javier"},
"Map.es_Es[5]": []string{"javier"},
"string": []string{"golang is very fun"},
"Slice[0]": []string{"1"},
"Slice[1]": []string{"2"},
"int": []string{"1"},
"Bool": []string{"true"},
}
valuesAJGFormT1 = url.Values{
"Nest.Children.0.Id": []string{"monoculum_id"},
"Nest.Children.0.Name": []string{"Monoculum"},
"Map.es_Es.0": []string{"javier"},
"Map.es_Es.1": []string{"javier"},
"Map.es_Es.2": []string{"javier"},
"Map.es_Es.3": []string{"javier"},
"Map.es_Es.4": []string{"javier"},
"Map.es_Es.5": []string{"javier"},
"string": []string{"golang is very fun"},
"Slice.0": []string{"1"},
"Slice.1": []string{"2"},
"int": []string{"1"},
"Bool": []string{"true"},
}
valuesJSONT1 = `
{
"Nest":
{
"Children": [{"Id": "monoculum_id", "Name":"Monoculum"}]
},
"string": "golang is very fun",
"Map": {"es_Es": ["javier", "javier", "javier", "javier", "javier", "javier"]},
"Slice": [1, 2],
"int": 20,
"Bool": true
}
`
)
func BenchmarkAJGFormTest1(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
ne := new(BenchAJG)
if err := form.DecodeValues(ne, valuesAJGFormT1); err != nil {
b.Error(err)
}
}
}
func BenchmarkFormamTest1(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
test := new(Bench)
if err := formam.Decode(valuesFormamT1, test); err != nil {
b.Error(err)
}
}
}
func BenchmarkJSONTest1(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
test := new(Bench)
if err := json.Unmarshal([]byte(valuesJSONT1), test); err != nil {
b.Error(err)
}
}
}