1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Large data benchmark.
6 // The JSON data is a summary of agl's changes in the
7 // go, webkit, and chromium open source projects.
8 // We benchmark converting between the JSON form
9 // and in-memory data structures.
21 type codeResponse struct {
22 Tree *codeNode `json:"tree"`
23 Username string `json:"username"`
26 type codeNode struct {
27 Name string `json:"name"`
28 Kids []*codeNode `json:"kids"`
29 CLWeight float64 `json:"cl_weight"`
30 Touches int `json:"touches"`
31 MinT int64 `json:"min_t"`
32 MaxT int64 `json:"max_t"`
33 MeanT int64 `json:"mean_t"`
37 var codeStruct codeResponse
40 f, err := os.Open("testdata/code.json.gz")
45 gz, err := gzip.NewReader(f)
49 data, err := ioutil.ReadAll(gz)
56 if err := Unmarshal(codeJSON, &codeStruct); err != nil {
57 panic("unmarshal code.json: " + err.Error())
60 if data, err = Marshal(&codeStruct); err != nil {
61 panic("marshal code.json: " + err.Error())
64 if !bytes.Equal(data, codeJSON) {
65 println("different lengths", len(data), len(codeJSON))
66 for i := 0; i < len(data) && i < len(codeJSON); i++ {
67 if data[i] != codeJSON[i] {
68 println("re-marshal: changed at byte", i)
69 println("orig: ", string(codeJSON[i-10:i+10]))
70 println("new: ", string(data[i-10:i+10]))
74 panic("re-marshal code.json: different result")
78 func BenchmarkCodeEncoder(b *testing.B) {
84 enc := NewEncoder(ioutil.Discard)
85 for i := 0; i < b.N; i++ {
86 if err := enc.Encode(&codeStruct); err != nil {
87 b.Fatal("Encode:", err)
90 b.SetBytes(int64(len(codeJSON)))
93 func BenchmarkCodeMarshal(b *testing.B) {
99 for i := 0; i < b.N; i++ {
100 if _, err := Marshal(&codeStruct); err != nil {
101 b.Fatal("Marshal:", err)
104 b.SetBytes(int64(len(codeJSON)))
107 func BenchmarkCodeDecoder(b *testing.B) {
114 dec := NewDecoder(&buf)
116 for i := 0; i < b.N; i++ {
122 if err := dec.Decode(&r); err != nil {
123 b.Fatal("Decode:", err)
126 b.SetBytes(int64(len(codeJSON)))
129 func BenchmarkCodeUnmarshal(b *testing.B) {
135 for i := 0; i < b.N; i++ {
137 if err := Unmarshal(codeJSON, &r); err != nil {
138 b.Fatal("Unmmarshal:", err)
141 b.SetBytes(int64(len(codeJSON)))
144 func BenchmarkCodeUnmarshalReuse(b *testing.B) {
151 for i := 0; i < b.N; i++ {
152 if err := Unmarshal(codeJSON, &r); err != nil {
153 b.Fatal("Unmmarshal:", err)
156 b.SetBytes(int64(len(codeJSON)))