jsoniter_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // +build use_jsoniter
  2. package benchmark
  3. import (
  4. "testing"
  5. jsoniter "github.com/json-iterator/go"
  6. )
  7. func BenchmarkJI_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 := jsoniter.Unmarshal(largeStructText, &s)
  12. if err != nil {
  13. b.Error(err)
  14. }
  15. }
  16. }
  17. func BenchmarkJI_Unmarshal_S(b *testing.B) {
  18. for i := 0; i < b.N; i++ {
  19. var s Entities
  20. err := jsoniter.Unmarshal(smallStructText, &s)
  21. if err != nil {
  22. b.Error(err)
  23. }
  24. }
  25. b.SetBytes(int64(len(smallStructText)))
  26. }
  27. func BenchmarkJI_Marshal_M(b *testing.B) {
  28. var l int64
  29. for i := 0; i < b.N; i++ {
  30. data, err := jsoniter.Marshal(&largeStructData)
  31. if err != nil {
  32. b.Error(err)
  33. }
  34. l = int64(len(data))
  35. }
  36. b.SetBytes(l)
  37. }
  38. func BenchmarkJI_Marshal_L(b *testing.B) {
  39. var l int64
  40. for i := 0; i < b.N; i++ {
  41. data, err := jsoniter.Marshal(&xlStructData)
  42. if err != nil {
  43. b.Error(err)
  44. }
  45. l = int64(len(data))
  46. }
  47. b.SetBytes(l)
  48. }
  49. func BenchmarkJI_Marshal_M_Parallel(b *testing.B) {
  50. var l int64
  51. b.RunParallel(func(pb *testing.PB) {
  52. for pb.Next() {
  53. data, err := jsoniter.Marshal(&largeStructData)
  54. if err != nil {
  55. b.Error(err)
  56. }
  57. l = int64(len(data))
  58. }
  59. })
  60. b.SetBytes(l)
  61. }
  62. func BenchmarkJI_Marshal_L_Parallel(b *testing.B) {
  63. var l int64
  64. b.RunParallel(func(pb *testing.PB) {
  65. for pb.Next() {
  66. data, err := jsoniter.Marshal(&xlStructData)
  67. if err != nil {
  68. b.Error(err)
  69. }
  70. l = int64(len(data))
  71. }
  72. })
  73. b.SetBytes(l)
  74. }
  75. func BenchmarkJI_Marshal_S(b *testing.B) {
  76. var l int64
  77. for i := 0; i < b.N; i++ {
  78. data, err := jsoniter.Marshal(&smallStructData)
  79. if err != nil {
  80. b.Error(err)
  81. }
  82. l = int64(len(data))
  83. }
  84. b.SetBytes(l)
  85. }
  86. func BenchmarkJI_Marshal_S_Parallel(b *testing.B) {
  87. var l int64
  88. b.RunParallel(func(pb *testing.PB) {
  89. for pb.Next() {
  90. data, err := jsoniter.Marshal(&smallStructData)
  91. if err != nil {
  92. b.Error(err)
  93. }
  94. l = int64(len(data))
  95. }
  96. })
  97. b.SetBytes(l)
  98. }
  99. func BenchmarkJI_Marshal_M_ToWriter(b *testing.B) {
  100. enc := jsoniter.NewEncoder(&DummyWriter{})
  101. for i := 0; i < b.N; i++ {
  102. err := enc.Encode(&largeStructData)
  103. if err != nil {
  104. b.Error(err)
  105. }
  106. }
  107. }