jsoniter_large_file_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package test
  2. import (
  3. "encoding/json"
  4. "github.com/json-iterator/go"
  5. "io/ioutil"
  6. "os"
  7. "testing"
  8. )
  9. //func Test_large_file(t *testing.T) {
  10. // file, err := os.Open("/tmp/large-file.json")
  11. // if err != nil {
  12. // t.Fatal(err)
  13. // }
  14. // iter := Parse(file, 4096)
  15. // count := 0
  16. // for iter.ReadArray() {
  17. // iter.Skip()
  18. // count++
  19. // }
  20. // if count != 11351 {
  21. // t.Fatal(count)
  22. // }
  23. //}
  24. func init() {
  25. ioutil.WriteFile("/tmp/large-file.json", []byte(`[{
  26. "person": {
  27. "id": "d50887ca-a6ce-4e59-b89f-14f0b5d03b03",
  28. "name": {
  29. "fullName": "Leonid Bugaev",
  30. "givenName": "Leonid",
  31. "familyName": "Bugaev"
  32. },
  33. "email": "leonsbox@gmail.com",
  34. "gender": "male",
  35. "location": "Saint Petersburg, Saint Petersburg, RU",
  36. "geo": {
  37. "city": "Saint Petersburg",
  38. "state": "Saint Petersburg",
  39. "country": "Russia",
  40. "lat": 59.9342802,
  41. "lng": 30.3350986
  42. },
  43. "bio": "Senior engineer at Granify.com",
  44. "site": "http://flickfaver.com",
  45. "avatar": "https://d1ts43dypk8bqh.cloudfront.net/v1/avatars/d50887ca-a6ce-4e59-b89f-14f0b5d03b03",
  46. "employment": {
  47. "name": "www.latera.ru",
  48. "title": "Software Engineer",
  49. "domain": "gmail.com"
  50. },
  51. "facebook": {
  52. "handle": "leonid.bugaev"
  53. },
  54. "github": {
  55. "handle": "buger",
  56. "id": 14009,
  57. "avatar": "https://avatars.githubusercontent.com/u/14009?v=3",
  58. "company": "Granify",
  59. "blog": "http://leonsbox.com",
  60. "followers": 95,
  61. "following": 10
  62. },
  63. "twitter": {
  64. "handle": "flickfaver",
  65. "id": 77004410,
  66. "bio": null,
  67. "followers": 2,
  68. "following": 1,
  69. "statuses": 5,
  70. "favorites": 0,
  71. "location": "",
  72. "site": "http://flickfaver.com",
  73. "avatar": null
  74. },
  75. "linkedin": {
  76. "handle": "in/leonidbugaev"
  77. },
  78. "googleplus": {
  79. "handle": null
  80. },
  81. "angellist": {
  82. "handle": "leonid-bugaev",
  83. "id": 61541,
  84. "bio": "Senior engineer at Granify.com",
  85. "blog": "http://buger.github.com",
  86. "site": "http://buger.github.com",
  87. "followers": 41,
  88. "avatar": "https://d1qb2nb5cznatu.cloudfront.net/users/61541-medium_jpg?1405474390"
  89. },
  90. "klout": {
  91. "handle": null,
  92. "score": null
  93. },
  94. "foursquare": {
  95. "handle": null
  96. },
  97. "aboutme": {
  98. "handle": "leonid.bugaev",
  99. "bio": null,
  100. "avatar": null
  101. },
  102. "gravatar": {
  103. "handle": "buger",
  104. "urls": [
  105. ],
  106. "avatar": "http://1.gravatar.com/avatar/f7c8edd577d13b8930d5522f28123510",
  107. "avatars": [
  108. {
  109. "url": "http://1.gravatar.com/avatar/f7c8edd577d13b8930d5522f28123510",
  110. "type": "thumbnail"
  111. }
  112. ]
  113. },
  114. "fuzzy": false
  115. },
  116. "company": "hello"
  117. }]`), 0666)
  118. }
  119. /*
  120. 200000 8886 ns/op 4336 B/op 6 allocs/op
  121. 50000 34244 ns/op 6744 B/op 14 allocs/op
  122. */
  123. func Benchmark_jsoniter_large_file(b *testing.B) {
  124. b.ReportAllocs()
  125. for n := 0; n < b.N; n++ {
  126. file, _ := os.Open("/tmp/large-file.json")
  127. iter := jsoniter.Parse(jsoniter.ConfigDefault, file, 4096)
  128. count := 0
  129. iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool {
  130. // Skip() is strict by default, use --tags jsoniter-sloppy to skip without validation
  131. iter.Skip()
  132. count++
  133. return true
  134. })
  135. file.Close()
  136. if iter.Error != nil {
  137. b.Error(iter.Error)
  138. }
  139. }
  140. }
  141. func Benchmark_json_large_file(b *testing.B) {
  142. b.ReportAllocs()
  143. for n := 0; n < b.N; n++ {
  144. file, _ := os.Open("/tmp/large-file.json")
  145. bytes, _ := ioutil.ReadAll(file)
  146. file.Close()
  147. result := []struct{}{}
  148. err := json.Unmarshal(bytes, &result)
  149. if err != nil {
  150. b.Error(err)
  151. }
  152. }
  153. }