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.
22 type codeResponse
struct {
23 Tree
*codeNode
`json:"tree"`
24 Username
string `json:"username"`
27 type codeNode
struct {
28 Name
string `json:"name"`
29 Kids
[]*codeNode
`json:"kids"`
30 CLWeight
float64 `json:"cl_weight"`
31 Touches
int `json:"touches"`
32 MinT
int64 `json:"min_t"`
33 MaxT
int64 `json:"max_t"`
34 MeanT
int64 `json:"mean_t"`
38 var codeStruct codeResponse
41 f
, err
:= os
.Open("testdata/code.json.gz")
46 gz
, err
:= gzip
.NewReader(f
)
50 data
, err
:= ioutil
.ReadAll(gz
)
57 if err
:= Unmarshal(codeJSON
, &codeStruct
); err
!= nil {
58 panic("unmarshal code.json: " + err
.Error())
61 if data
, err
= Marshal(&codeStruct
); err
!= nil {
62 panic("marshal code.json: " + err
.Error())
65 if !bytes
.Equal(data
, codeJSON
) {
66 println("different lengths", len(data
), len(codeJSON
))
67 for i
:= 0; i
< len(data
) && i
< len(codeJSON
); i
++ {
68 if data
[i
] != codeJSON
[i
] {
69 println("re-marshal: changed at byte", i
)
70 println("orig: ", string(codeJSON
[i
-10:i
+10]))
71 println("new: ", string(data
[i
-10:i
+10]))
75 panic("re-marshal code.json: different result")
79 func BenchmarkCodeEncoder(b
*testing
.B
) {
85 b
.RunParallel(func(pb
*testing
.PB
) {
86 enc
:= NewEncoder(ioutil
.Discard
)
88 if err
:= enc
.Encode(&codeStruct
); err
!= nil {
89 b
.Fatal("Encode:", err
)
93 b
.SetBytes(int64(len(codeJSON
)))
96 func BenchmarkCodeMarshal(b
*testing
.B
) {
102 b
.RunParallel(func(pb
*testing
.PB
) {
104 if _
, err
:= Marshal(&codeStruct
); err
!= nil {
105 b
.Fatal("Marshal:", err
)
109 b
.SetBytes(int64(len(codeJSON
)))
112 func BenchmarkCodeDecoder(b
*testing
.B
) {
118 b
.RunParallel(func(pb
*testing
.PB
) {
120 dec
:= NewDecoder(&buf
)
128 if err
:= dec
.Decode(&r
); err
!= nil {
129 b
.Fatal("Decode:", err
)
133 b
.SetBytes(int64(len(codeJSON
)))
136 func BenchmarkUnicodeDecoder(b
*testing
.B
) {
137 j
:= []byte(`"\uD83D\uDE01"`)
138 b
.SetBytes(int64(len(j
)))
139 r
:= bytes
.NewReader(j
)
143 for i
:= 0; i
< b
.N
; i
++ {
144 if err
:= dec
.Decode(&out
); err
!= nil {
145 b
.Fatal("Decode:", err
)
151 func BenchmarkDecoderStream(b
*testing
.B
) {
154 dec
:= NewDecoder(&buf
)
155 buf
.WriteString(`"` + strings
.Repeat("x", 1000000) + `"` + "\n\n\n")
157 if err
:= dec
.Decode(&x
); err
!= nil {
158 b
.Fatal("Decode:", err
)
160 ones
:= strings
.Repeat(" 1\n", 300000) + "\n\n\n"
162 for i
:= 0; i
< b
.N
; i
++ {
164 buf
.WriteString(ones
)
167 if err
:= dec
.Decode(&x
); err
!= nil || x
!= 1.0 {
168 b
.Fatalf("Decode: %v after %d", err
, i
)
173 func BenchmarkCodeUnmarshal(b
*testing
.B
) {
179 b
.RunParallel(func(pb
*testing
.PB
) {
182 if err
:= Unmarshal(codeJSON
, &r
); err
!= nil {
183 b
.Fatal("Unmarshal:", err
)
187 b
.SetBytes(int64(len(codeJSON
)))
190 func BenchmarkCodeUnmarshalReuse(b
*testing
.B
) {
196 b
.RunParallel(func(pb
*testing
.PB
) {
199 if err
:= Unmarshal(codeJSON
, &r
); err
!= nil {
200 b
.Fatal("Unmarshal:", err
)
204 // TODO(bcmills): Is there a missing b.SetBytes here?
207 func BenchmarkUnmarshalString(b
*testing
.B
) {
208 data
:= []byte(`"hello, world"`)
209 b
.RunParallel(func(pb
*testing
.PB
) {
212 if err
:= Unmarshal(data
, &s
); err
!= nil {
213 b
.Fatal("Unmarshal:", err
)
219 func BenchmarkUnmarshalFloat64(b
*testing
.B
) {
220 data
:= []byte(`3.14`)
221 b
.RunParallel(func(pb
*testing
.PB
) {
224 if err
:= Unmarshal(data
, &f
); err
!= nil {
225 b
.Fatal("Unmarshal:", err
)
231 func BenchmarkUnmarshalInt64(b
*testing
.B
) {
233 b
.RunParallel(func(pb
*testing
.PB
) {
236 if err
:= Unmarshal(data
, &x
); err
!= nil {
237 b
.Fatal("Unmarshal:", err
)
243 func BenchmarkIssue10335(b
*testing
.B
) {
245 j
:= []byte(`{"a":{ }}`)
246 b
.RunParallel(func(pb
*testing
.PB
) {
249 if err
:= Unmarshal(j
, &s
); err
!= nil {
256 func BenchmarkUnmapped(b
*testing
.B
) {
258 j
:= []byte(`{"s": "hello", "y": 2, "o": {"x": 0}, "a": [1, 99, {"x": 1}]}`)
259 b
.RunParallel(func(pb
*testing
.PB
) {
262 if err
:= Unmarshal(j
, &s
); err
!= nil {