...

Source file src/github.com/json-iterator/go/benchmarks/jsoniter_large_file_test.go

Documentation: github.com/json-iterator/go/benchmarks

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

View as plain text