ffjson_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // +build use_ffjson
  2. package benchmark
  3. import (
  4. "testing"
  5. "github.com/pquerna/ffjson/ffjson"
  6. )
  7. func BenchmarkFF_Unmarshal_M(b *testing.B) {
  8. b.SetBytes(int64(len(largeStructText)))
  9. for i := 0; i < b.N; i++ {
  10. var s LargeStruct
  11. err := ffjson.UnmarshalFast(largeStructText, &s)
  12. if err != nil {
  13. b.Error(err)
  14. }
  15. }
  16. }
  17. func BenchmarkFF_Unmarshal_S(b *testing.B) {
  18. for i := 0; i < b.N; i++ {
  19. var s Entities
  20. err := ffjson.UnmarshalFast(smallStructText, &s)
  21. if err != nil {
  22. b.Error(err)
  23. }
  24. }
  25. b.SetBytes(int64(len(smallStructText)))
  26. }
  27. func BenchmarkFF_Marshal_M(b *testing.B) {
  28. var l int64
  29. for i := 0; i < b.N; i++ {
  30. data, err := ffjson.MarshalFast(&largeStructData)
  31. if err != nil {
  32. b.Error(err)
  33. }
  34. l = int64(len(data))
  35. }
  36. b.SetBytes(l)
  37. }
  38. func BenchmarkFF_Marshal_S(b *testing.B) {
  39. var l int64
  40. for i := 0; i < b.N; i++ {
  41. data, err := ffjson.MarshalFast(&smallStructData)
  42. if err != nil {
  43. b.Error(err)
  44. }
  45. l = int64(len(data))
  46. }
  47. b.SetBytes(l)
  48. }
  49. func BenchmarkFF_Marshal_M_Pool(b *testing.B) {
  50. var l int64
  51. for i := 0; i < b.N; i++ {
  52. data, err := ffjson.MarshalFast(&largeStructData)
  53. if err != nil {
  54. b.Error(err)
  55. }
  56. l = int64(len(data))
  57. ffjson.Pool(data)
  58. }
  59. b.SetBytes(l)
  60. }
  61. func BenchmarkFF_Marshal_L(b *testing.B) {
  62. var l int64
  63. for i := 0; i < b.N; i++ {
  64. data, err := ffjson.MarshalFast(&xlStructData)
  65. if err != nil {
  66. b.Error(err)
  67. }
  68. l = int64(len(data))
  69. }
  70. b.SetBytes(l)
  71. }
  72. func BenchmarkFF_Marshal_L_Pool(b *testing.B) {
  73. var l int64
  74. for i := 0; i < b.N; i++ {
  75. data, err := ffjson.MarshalFast(&xlStructData)
  76. if err != nil {
  77. b.Error(err)
  78. }
  79. l = int64(len(data))
  80. ffjson.Pool(data)
  81. }
  82. b.SetBytes(l)
  83. }
  84. func BenchmarkFF_Marshal_L_Pool_Parallel(b *testing.B) {
  85. var l int64
  86. for i := 0; i < b.N; i++ {
  87. data, err := ffjson.MarshalFast(&xlStructData)
  88. if err != nil {
  89. b.Error(err)
  90. }
  91. l = int64(len(data))
  92. ffjson.Pool(data)
  93. }
  94. b.SetBytes(l)
  95. }
  96. func BenchmarkFF_Marshal_M_Pool_Parallel(b *testing.B) {
  97. var l int64
  98. b.RunParallel(func(pb *testing.PB) {
  99. for pb.Next() {
  100. data, err := ffjson.MarshalFast(&largeStructData)
  101. if err != nil {
  102. b.Error(err)
  103. }
  104. l = int64(len(data))
  105. ffjson.Pool(data)
  106. }
  107. })
  108. b.SetBytes(l)
  109. }
  110. func BenchmarkFF_Marshal_S_Pool(b *testing.B) {
  111. var l int64
  112. for i := 0; i < b.N; i++ {
  113. data, err := ffjson.MarshalFast(&smallStructData)
  114. if err != nil {
  115. b.Error(err)
  116. }
  117. l = int64(len(data))
  118. ffjson.Pool(data)
  119. }
  120. b.SetBytes(l)
  121. }
  122. func BenchmarkFF_Marshal_S_Pool_Parallel(b *testing.B) {
  123. var l int64
  124. b.RunParallel(func(pb *testing.PB) {
  125. for pb.Next() {
  126. data, err := ffjson.MarshalFast(&smallStructData)
  127. if err != nil {
  128. b.Error(err)
  129. }
  130. l = int64(len(data))
  131. ffjson.Pool(data)
  132. }
  133. })
  134. b.SetBytes(l)
  135. }
  136. func BenchmarkFF_Marshal_S_Parallel(b *testing.B) {
  137. var l int64
  138. b.RunParallel(func(pb *testing.PB) {
  139. for pb.Next() {
  140. data, err := ffjson.MarshalFast(&smallStructData)
  141. if err != nil {
  142. b.Error(err)
  143. }
  144. l = int64(len(data))
  145. }
  146. })
  147. b.SetBytes(l)
  148. }
  149. func BenchmarkFF_Marshal_M_Parallel(b *testing.B) {
  150. var l int64
  151. b.RunParallel(func(pb *testing.PB) {
  152. for pb.Next() {
  153. data, err := ffjson.MarshalFast(&largeStructData)
  154. if err != nil {
  155. b.Error(err)
  156. }
  157. l = int64(len(data))
  158. }
  159. })
  160. b.SetBytes(l)
  161. }
  162. func BenchmarkFF_Marshal_L_Parallel(b *testing.B) {
  163. var l int64
  164. b.RunParallel(func(pb *testing.PB) {
  165. for pb.Next() {
  166. data, err := ffjson.MarshalFast(&xlStructData)
  167. if err != nil {
  168. b.Error(err)
  169. }
  170. l = int64(len(data))
  171. }
  172. })
  173. b.SetBytes(l)
  174. }