benchmark_small_payload_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. Each test should process 190 byte http log like json record
  3. It should read multiple fields
  4. */
  5. package benchmark
  6. import (
  7. "encoding/json"
  8. "testing"
  9. "github.com/Jeffail/gabs"
  10. "github.com/a8m/djson"
  11. "github.com/antonholmquist/jason"
  12. "github.com/bitly/go-simplejson"
  13. "github.com/buger/jsonparser"
  14. jlexer "github.com/mailru/easyjson/jlexer"
  15. "github.com/mreiferson/go-ujson"
  16. "github.com/pquerna/ffjson/ffjson"
  17. "github.com/ugorji/go/codec"
  18. // "fmt"
  19. "bytes"
  20. "errors"
  21. )
  22. // Just for emulating field access, so it will not throw "evaluated but not used"
  23. func nothing(_ ...interface{}) {}
  24. /*
  25. github.com/buger/jsonparser
  26. */
  27. func BenchmarkJsonParserSmall(b *testing.B) {
  28. for i := 0; i < b.N; i++ {
  29. jsonparser.Get(smallFixture, "uuid")
  30. jsonparser.GetInt(smallFixture, "tz")
  31. jsonparser.Get(smallFixture, "ua")
  32. jsonparser.GetInt(smallFixture, "st")
  33. nothing()
  34. }
  35. }
  36. func BenchmarkJsonParserEachKeyManualSmall(b *testing.B) {
  37. paths := [][]string{
  38. []string{"uuid"},
  39. []string{"tz"},
  40. []string{"ua"},
  41. []string{"st"},
  42. }
  43. for i := 0; i < b.N; i++ {
  44. jsonparser.EachKey(smallFixture, func(idx int, value []byte, vt jsonparser.ValueType, err error) {
  45. switch idx {
  46. case 0:
  47. // jsonparser.ParseString(value)
  48. case 1:
  49. jsonparser.ParseInt(value)
  50. case 2:
  51. // jsonparser.ParseString(value)
  52. case 3:
  53. jsonparser.ParseInt(value)
  54. }
  55. }, paths...)
  56. }
  57. }
  58. func BenchmarkJsonParserEachKeyStructSmall(b *testing.B) {
  59. paths := [][]string{
  60. []string{"uuid"},
  61. []string{"tz"},
  62. []string{"ua"},
  63. []string{"st"},
  64. }
  65. for i := 0; i < b.N; i++ {
  66. var data SmallPayload
  67. jsonparser.EachKey(smallFixture, func(idx int, value []byte, vt jsonparser.ValueType, err error) {
  68. switch idx {
  69. case 0:
  70. data.Uuid, _ = jsonparser.ParseString(value)
  71. case 1:
  72. v, _ := jsonparser.ParseInt(value)
  73. data.Tz = int(v)
  74. case 2:
  75. data.Ua, _ = jsonparser.ParseString(value)
  76. case 3:
  77. v, _ := jsonparser.ParseInt(value)
  78. data.St = int(v)
  79. }
  80. }, paths...)
  81. nothing(data.Uuid, data.Tz, data.Ua, data.St)
  82. }
  83. }
  84. func BenchmarkJsonParserObjectEachStructSmall(b *testing.B) {
  85. uuidKey, tzKey, uaKey, stKey := []byte("uuid"), []byte("tz"), []byte("ua"), []byte("st")
  86. errStop := errors.New("stop")
  87. for i := 0; i < b.N; i++ {
  88. var data SmallPayload
  89. missing := 4
  90. jsonparser.ObjectEach(smallFixture, func(key, value []byte, vt jsonparser.ValueType, off int) error {
  91. switch {
  92. case bytes.Equal(key, uuidKey):
  93. data.Uuid, _ = jsonparser.ParseString(value)
  94. missing--
  95. case bytes.Equal(key, tzKey):
  96. v, _ := jsonparser.ParseInt(value)
  97. data.Tz = int(v)
  98. missing--
  99. case bytes.Equal(key, uaKey):
  100. data.Ua, _ = jsonparser.ParseString(value)
  101. missing--
  102. case bytes.Equal(key, stKey):
  103. v, _ := jsonparser.ParseInt(value)
  104. data.St = int(v)
  105. missing--
  106. }
  107. if missing == 0 {
  108. return errStop
  109. } else {
  110. return nil
  111. }
  112. })
  113. nothing(data.Uuid, data.Tz, data.Ua, data.St)
  114. }
  115. }
  116. func BenchmarkJsonParserSetSmall(b *testing.B) {
  117. for i := 0; i < b.N; i++ {
  118. jsonparser.Set(smallFixture, []byte(`"c90927dd-1588-4fe7-a14f-8a8950cfcbd8"`), "uuid")
  119. jsonparser.Set(smallFixture, []byte("-3"), "tz")
  120. jsonparser.Set(smallFixture, []byte(`"server_agent"`), "ua")
  121. jsonparser.Set(smallFixture, []byte("3"), "st")
  122. nothing()
  123. }
  124. }
  125. func BenchmarkJsonParserDelSmall(b *testing.B) {
  126. fixture := make([]byte, 0, len(smallFixture))
  127. b.ResetTimer()
  128. for i := 0; i < b.N; i++ {
  129. fixture = append(fixture[:0], smallFixture...)
  130. fixture = jsonparser.Delete(fixture, "uuid")
  131. fixture = jsonparser.Delete(fixture, "tz")
  132. fixture = jsonparser.Delete(fixture, "ua")
  133. fixture = jsonparser.Delete(fixture, "stt")
  134. nothing()
  135. }
  136. }
  137. /*
  138. encoding/json
  139. */
  140. func BenchmarkEncodingJsonStructSmall(b *testing.B) {
  141. for i := 0; i < b.N; i++ {
  142. var data SmallPayload
  143. json.Unmarshal(smallFixture, &data)
  144. nothing(data.Uuid, data.Tz, data.Ua, data.St)
  145. }
  146. }
  147. func BenchmarkEncodingJsonInterfaceSmall(b *testing.B) {
  148. for i := 0; i < b.N; i++ {
  149. var data interface{}
  150. json.Unmarshal(smallFixture, &data)
  151. m := data.(map[string]interface{})
  152. nothing(m["uuid"].(string), m["tz"].(float64), m["ua"].(string), m["st"].(float64))
  153. }
  154. }
  155. /*
  156. github.com/Jeffail/gabs
  157. */
  158. func BenchmarkGabsSmall(b *testing.B) {
  159. for i := 0; i < b.N; i++ {
  160. json, _ := gabs.ParseJSON(smallFixture)
  161. nothing(
  162. json.Path("uuid").Data().(string),
  163. json.Path("tz").Data().(float64),
  164. json.Path("ua").Data().(string),
  165. json.Path("st").Data().(float64),
  166. )
  167. }
  168. }
  169. /*
  170. github.com/bitly/go-simplejson
  171. */
  172. func BenchmarkGoSimplejsonSmall(b *testing.B) {
  173. for i := 0; i < b.N; i++ {
  174. json, _ := simplejson.NewJson(smallFixture)
  175. json.Get("uuid").String()
  176. json.Get("tz").Float64()
  177. json.Get("ua").String()
  178. json.Get("st").Float64()
  179. nothing()
  180. }
  181. }
  182. func BenchmarkGoSimplejsonSetSmall(b *testing.B) {
  183. for i := 0; i < b.N; i++ {
  184. json, _ := simplejson.NewJson(smallFixture)
  185. json.SetPath([]string{"uuid"}, "c90927dd-1588-4fe7-a14f-8a8950cfcbd8")
  186. json.SetPath([]string{"tz"}, -3)
  187. json.SetPath([]string{"ua"}, "server_agent")
  188. json.SetPath([]string{"st"}, 3)
  189. nothing()
  190. }
  191. }
  192. /*
  193. github.com/pquerna/ffjson
  194. */
  195. func BenchmarkFFJsonSmall(b *testing.B) {
  196. for i := 0; i < b.N; i++ {
  197. var data SmallPayload
  198. ffjson.Unmarshal(smallFixture, &data)
  199. nothing(data.Uuid, data.Tz, data.Ua, data.St)
  200. }
  201. }
  202. /*
  203. github.com/bitly/go-simplejson
  204. */
  205. func BenchmarkJasonSmall(b *testing.B) {
  206. for i := 0; i < b.N; i++ {
  207. json, _ := jason.NewObjectFromBytes(smallFixture)
  208. json.GetString("uuid")
  209. json.GetFloat64("tz")
  210. json.GetString("ua")
  211. json.GetFloat64("st")
  212. nothing()
  213. }
  214. }
  215. /*
  216. github.com/mreiferson/go-ujson
  217. */
  218. func BenchmarkUjsonSmall(b *testing.B) {
  219. for i := 0; i < b.N; i++ {
  220. json, _ := ujson.NewFromBytes(smallFixture)
  221. json.Get("uuid").String()
  222. json.Get("tz").Float64()
  223. json.Get("ua").String()
  224. json.Get("st").Float64()
  225. nothing()
  226. }
  227. }
  228. /*
  229. github.com/a8m/djson
  230. */
  231. func BenchmarkDjsonSmall(b *testing.B) {
  232. for i := 0; i < b.N; i++ {
  233. m, _ := djson.DecodeObject(smallFixture)
  234. nothing(m["uuid"].(string), m["tz"].(float64), m["ua"].(string), m["st"].(float64))
  235. }
  236. }
  237. /*
  238. github.com/ugorji/go/codec
  239. */
  240. func BenchmarkUgirjiSmall(b *testing.B) {
  241. for i := 0; i < b.N; i++ {
  242. decoder := codec.NewDecoderBytes(smallFixture, new(codec.JsonHandle))
  243. data := new(SmallPayload)
  244. data.CodecDecodeSelf(decoder)
  245. nothing(data.Uuid, data.Tz, data.Ua, data.St)
  246. }
  247. }
  248. /*
  249. github.com/mailru/easyjson
  250. */
  251. func BenchmarkEasyJsonSmall(b *testing.B) {
  252. for i := 0; i < b.N; i++ {
  253. lexer := &jlexer.Lexer{Data: smallFixture}
  254. data := new(SmallPayload)
  255. data.UnmarshalEasyJSON(lexer)
  256. nothing(data.Uuid, data.Tz, data.Ua, data.St)
  257. }
  258. }