github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/gin/binding/form_mapping_benchmark_test.go (about) 1 // Copyright 2019 Gin Core Team. All rights reserved. 2 // Use of this source code is governed by a MIT style 3 // license that can be found in the LICENSE file. 4 5 package binding 6 7 import ( 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 var form = map[string][]string{ 15 "name": {"mike"}, 16 "friends": {"anna", "nicole"}, 17 "id_number": {"12345678"}, 18 "id_date": {"2018-01-20"}, 19 } 20 21 type structFull struct { 22 Name string `form:"name"` 23 Age int `form:"age,default=25"` 24 Friends []string `form:"friends"` 25 ID *struct { 26 Number string `form:"id_number"` 27 DateOfIssue time.Time `form:"id_date" time_format:"2006-01-02" time_utc:"true"` 28 } 29 Nationality *string `form:"nationality"` 30 } 31 32 func BenchmarkMapFormFull(b *testing.B) { 33 var s structFull 34 for i := 0; i < b.N; i++ { 35 err := mapForm(&s, form) 36 if err != nil { 37 b.Fatalf("Error on a form mapping") 38 } 39 } 40 b.StopTimer() 41 42 t := b 43 assert.Equal(t, "mike", s.Name) 44 assert.Equal(t, 25, s.Age) 45 assert.Equal(t, []string{"anna", "nicole"}, s.Friends) 46 assert.Equal(t, "12345678", s.ID.Number) 47 assert.Equal(t, time.Date(2018, 1, 20, 0, 0, 0, 0, time.UTC), s.ID.DateOfIssue) 48 assert.Nil(t, s.Nationality) 49 } 50 51 type structName struct { 52 Name string `form:"name"` 53 } 54 55 func BenchmarkMapFormName(b *testing.B) { 56 var s structName 57 for i := 0; i < b.N; i++ { 58 err := mapForm(&s, form) 59 if err != nil { 60 b.Fatalf("Error on a form mapping") 61 } 62 } 63 b.StopTimer() 64 65 t := b 66 assert.Equal(t, "mike", s.Name) 67 }