/Godeps/_workspace/src/gopkg.in/bluesuncorp/validator.v5/benchmarks_test.go

https://gitlab.com/voxxit/gogeoip2 · Go · 163 lines · 145 code · 18 blank · 0 comment · 5 complexity · 3603ff5cdbf9b58236894e783ec3cf5c MD5 · raw file

  1. package validator
  2. import "testing"
  3. func BenchmarkValidateField(b *testing.B) {
  4. for n := 0; n < b.N; n++ {
  5. validate.Field("1", "len=1")
  6. }
  7. }
  8. func BenchmarkValidateStructSimple(b *testing.B) {
  9. type Foo struct {
  10. StringValue string `validate:"min=5,max=10"`
  11. IntValue int `validate:"min=5,max=10"`
  12. }
  13. validFoo := &Foo{StringValue: "Foobar", IntValue: 7}
  14. invalidFoo := &Foo{StringValue: "Fo", IntValue: 3}
  15. for n := 0; n < b.N; n++ {
  16. validate.Struct(validFoo)
  17. validate.Struct(invalidFoo)
  18. }
  19. }
  20. func BenchmarkTemplateParallelSimple(b *testing.B) {
  21. type Foo struct {
  22. StringValue string `validate:"min=5,max=10"`
  23. IntValue int `validate:"min=5,max=10"`
  24. }
  25. validFoo := &Foo{StringValue: "Foobar", IntValue: 7}
  26. invalidFoo := &Foo{StringValue: "Fo", IntValue: 3}
  27. b.RunParallel(func(pb *testing.PB) {
  28. for pb.Next() {
  29. validate.Struct(validFoo)
  30. validate.Struct(invalidFoo)
  31. }
  32. })
  33. }
  34. func BenchmarkValidateStructLarge(b *testing.B) {
  35. tFail := &TestString{
  36. Required: "",
  37. Len: "",
  38. Min: "",
  39. Max: "12345678901",
  40. MinMax: "",
  41. Lt: "0123456789",
  42. Lte: "01234567890",
  43. Gt: "1",
  44. Gte: "1",
  45. OmitEmpty: "12345678901",
  46. Sub: &SubTest{
  47. Test: "",
  48. },
  49. Anonymous: struct {
  50. A string `validate:"required"`
  51. }{
  52. A: "",
  53. },
  54. Iface: &Impl{
  55. F: "12",
  56. },
  57. }
  58. tSuccess := &TestString{
  59. Required: "Required",
  60. Len: "length==10",
  61. Min: "min=1",
  62. Max: "1234567890",
  63. MinMax: "12345",
  64. Lt: "012345678",
  65. Lte: "0123456789",
  66. Gt: "01234567890",
  67. Gte: "0123456789",
  68. OmitEmpty: "",
  69. Sub: &SubTest{
  70. Test: "1",
  71. },
  72. SubIgnore: &SubTest{
  73. Test: "",
  74. },
  75. Anonymous: struct {
  76. A string `validate:"required"`
  77. }{
  78. A: "1",
  79. },
  80. Iface: &Impl{
  81. F: "123",
  82. },
  83. }
  84. for n := 0; n < b.N; n++ {
  85. validate.Struct(tSuccess)
  86. validate.Struct(tFail)
  87. }
  88. }
  89. func BenchmarkTemplateParallelLarge(b *testing.B) {
  90. tFail := &TestString{
  91. Required: "",
  92. Len: "",
  93. Min: "",
  94. Max: "12345678901",
  95. MinMax: "",
  96. Lt: "0123456789",
  97. Lte: "01234567890",
  98. Gt: "1",
  99. Gte: "1",
  100. OmitEmpty: "12345678901",
  101. Sub: &SubTest{
  102. Test: "",
  103. },
  104. Anonymous: struct {
  105. A string `validate:"required"`
  106. }{
  107. A: "",
  108. },
  109. Iface: &Impl{
  110. F: "12",
  111. },
  112. }
  113. tSuccess := &TestString{
  114. Required: "Required",
  115. Len: "length==10",
  116. Min: "min=1",
  117. Max: "1234567890",
  118. MinMax: "12345",
  119. Lt: "012345678",
  120. Lte: "0123456789",
  121. Gt: "01234567890",
  122. Gte: "0123456789",
  123. OmitEmpty: "",
  124. Sub: &SubTest{
  125. Test: "1",
  126. },
  127. SubIgnore: &SubTest{
  128. Test: "",
  129. },
  130. Anonymous: struct {
  131. A string `validate:"required"`
  132. }{
  133. A: "1",
  134. },
  135. Iface: &Impl{
  136. F: "123",
  137. },
  138. }
  139. b.RunParallel(func(pb *testing.PB) {
  140. for pb.Next() {
  141. validate.Struct(tSuccess)
  142. validate.Struct(tFail)
  143. }
  144. })
  145. }