benchmark_large_payload_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Each test should process 24kb json record (based on Discourse API)
  3. It should read 2 arrays, and for each item in array get few fields.
  4. Basically it means processing full JSON file.
  5. */
  6. package benchmark
  7. import (
  8. "github.com/buger/jsonparser"
  9. "testing"
  10. // "github.com/Jeffail/gabs"
  11. // "github.com/bitly/go-simplejson"
  12. "encoding/json"
  13. "github.com/a8m/djson"
  14. jlexer "github.com/mailru/easyjson/jlexer"
  15. "github.com/pquerna/ffjson/ffjson"
  16. // "github.com/antonholmquist/jason"
  17. // "fmt"
  18. )
  19. /*
  20. github.com/buger/jsonparser
  21. */
  22. func BenchmarkJsonParserLarge(b *testing.B) {
  23. for i := 0; i < b.N; i++ {
  24. jsonparser.ArrayEach(largeFixture, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
  25. jsonparser.Get(value, "username")
  26. nothing()
  27. }, "users")
  28. jsonparser.ArrayEach(largeFixture, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
  29. jsonparser.GetInt(value, "id")
  30. jsonparser.Get(value, "slug")
  31. nothing()
  32. }, "topics", "topics")
  33. }
  34. }
  35. /*
  36. encoding/json
  37. */
  38. func BenchmarkEncodingJsonStructLarge(b *testing.B) {
  39. for i := 0; i < b.N; i++ {
  40. var data LargePayload
  41. json.Unmarshal(largeFixture, &data)
  42. for _, u := range data.Users {
  43. nothing(u.Username)
  44. }
  45. for _, t := range data.Topics.Topics {
  46. nothing(t.Id, t.Slug)
  47. }
  48. }
  49. }
  50. func BenchmarkEncodingJsonInterfaceLarge(b *testing.B) {
  51. for i := 0; i < b.N; i++ {
  52. var data interface{}
  53. json.Unmarshal(largeFixture, &data)
  54. m := data.(map[string]interface{})
  55. users := m["users"].([]interface{})
  56. for _, u := range users {
  57. nothing(u.(map[string]interface{})["username"].(string))
  58. }
  59. topics := m["topics"].(map[string]interface{})["topics"].([]interface{})
  60. for _, t := range topics {
  61. tI := t.(map[string]interface{})
  62. nothing(tI["id"].(float64), tI["slug"].(string))
  63. }
  64. }
  65. }
  66. /*
  67. github.com/pquerna/ffjson
  68. */
  69. func BenchmarkFFJsonLarge(b *testing.B) {
  70. for i := 0; i < b.N; i++ {
  71. var data LargePayload
  72. ffjson.Unmarshal(largeFixture, &data)
  73. for _, u := range data.Users {
  74. nothing(u.Username)
  75. }
  76. for _, t := range data.Topics.Topics {
  77. nothing(t.Id, t.Slug)
  78. }
  79. }
  80. }
  81. /*
  82. github.com/mailru/easyjson
  83. */
  84. func BenchmarkEasyJsonLarge(b *testing.B) {
  85. for i := 0; i < b.N; i++ {
  86. lexer := &jlexer.Lexer{Data: largeFixture}
  87. data := new(LargePayload)
  88. data.UnmarshalEasyJSON(lexer)
  89. for _, u := range data.Users {
  90. nothing(u.Username)
  91. }
  92. for _, t := range data.Topics.Topics {
  93. nothing(t.Id, t.Slug)
  94. }
  95. }
  96. }
  97. /*
  98. github.com/a8m/djson
  99. */
  100. func BenchmarkDjsonLarge(b *testing.B) {
  101. for i := 0; i < b.N; i++ {
  102. m, _ := djson.DecodeObject(largeFixture)
  103. users := m["users"].([]interface{})
  104. for _, u := range users {
  105. nothing(u.(map[string]interface{})["username"].(string))
  106. }
  107. topics := m["topics"].(map[string]interface{})["topics"].([]interface{})
  108. for _, t := range topics {
  109. tI := t.(map[string]interface{})
  110. nothing(tI["id"].(float64), tI["slug"].(string))
  111. }
  112. }
  113. }