Skip to content

Commit 0d7fc4e

Browse files
author
Breno Loyola
committed
add simple benchmark
1 parent 72513e3 commit 0d7fc4e

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

unmarshal_test.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package queryparam_test
22

33
import (
4-
"testing"
5-
"github.com/tomwright/queryparam"
6-
"net/url"
7-
"net/http"
84
"fmt"
5+
"net/http"
6+
"net/url"
97
"reflect"
8+
"testing"
9+
10+
"github.com/tomwright/queryparam"
1011
)
1112

1213
// ExampleUnmarshal creates a dummy http request and unmarshals the data into a struct
@@ -96,7 +97,7 @@ func TestUnmarshal(t *testing.T) {
9697
st.Errorf("unable to unmarshal url: %s", err)
9798
}
9899

99-
if ! reflect.DeepEqual(tc.Data, tc.OutputData) {
100+
if !reflect.DeepEqual(tc.Data, tc.OutputData) {
100101
st.Errorf("expected `%v`, got `%v`", tc.OutputData, tc.Data)
101102
}
102103
})
@@ -120,7 +121,7 @@ func TestUnmarshal_BlankDelimiterUsesDefaultDelimiter(t *testing.T) {
120121
t.Errorf("unexpected error: %s", err)
121122
}
122123

123-
if exp, got := []string{"Tom", "Jim"}, req.Name; ! reflect.DeepEqual(exp, got) {
124+
if exp, got := []string{"Tom", "Jim"}, req.Name; !reflect.DeepEqual(exp, got) {
124125
t.Errorf("unexpected result. expected `%v`, got `%v`", exp, got)
125126
}
126127
}
@@ -209,3 +210,22 @@ func TestUnmarshal_InvalidFieldType(t *testing.T) {
209210
t.Errorf("unexpected error string. expected `%s`, got `%s`", exp, got)
210211
}
211212
}
213+
214+
func BenchmarkUnmarshal(b *testing.B) {
215+
216+
u, err := url.Parse("http://localhost:123?name=abcd&namelist=a,b,c&namelistdash=abc&age=12")
217+
if err != nil {
218+
b.FailNow()
219+
}
220+
221+
b.ResetTimer()
222+
for i := 0; i < b.N; i++ {
223+
data := testData{}
224+
err = queryparam.Unmarshal(u, &data)
225+
if err != nil {
226+
b.FailNow()
227+
}
228+
}
229+
b.StopTimer()
230+
b.ReportAllocs()
231+
}

0 commit comments

Comments
 (0)