form_mapping_benchmark_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. package binding
  5. import (
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. var form = map[string][]string{
  11. "name": {"mike"},
  12. "friends": {"anna", "nicole"},
  13. "id_number": {"12345678"},
  14. "id_date": {"2018-01-20"},
  15. }
  16. type structFull struct {
  17. Name string `form:"name"`
  18. Age int `form:"age,default=25"`
  19. Friends []string `form:"friends"`
  20. ID *struct {
  21. Number string `form:"id_number"`
  22. DateOfIssue time.Time `form:"id_date" time_format:"2006-01-02" time_utc:"true"`
  23. }
  24. Nationality *string `form:"nationality"`
  25. }
  26. func BenchmarkMapFormFull(b *testing.B) {
  27. var s structFull
  28. for i := 0; i < b.N; i++ {
  29. mapForm(&s, form)
  30. }
  31. b.StopTimer()
  32. t := b
  33. assert.Equal(t, "mike", s.Name)
  34. assert.Equal(t, 25, s.Age)
  35. assert.Equal(t, []string{"anna", "nicole"}, s.Friends)
  36. assert.Equal(t, "12345678", s.ID.Number)
  37. assert.Equal(t, time.Date(2018, 1, 20, 0, 0, 0, 0, time.UTC), s.ID.DateOfIssue)
  38. assert.Nil(t, s.Nationality)
  39. }
  40. type structName struct {
  41. Name string `form:"name"`
  42. }
  43. func BenchmarkMapFormName(b *testing.B) {
  44. var s structName
  45. for i := 0; i < b.N; i++ {
  46. mapForm(&s, form)
  47. }
  48. b.StopTimer()
  49. t := b
  50. assert.Equal(t, "mike", s.Name)
  51. }