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 enc
:= NewEncoder(ioutil
.Discard
)
86 for i
:= 0; i
< b
.N
; i
++ {
87 if err
:= enc
.Encode(&codeStruct
); err
!= nil {
88 b
.Fatal("Encode:", err
)
91 b
.SetBytes(int64(len(codeJSON
)))
94 func BenchmarkCodeMarshal(b
*testing
.B
) {
100 for i
:= 0; i
< b
.N
; i
++ {
101 if _
, err
:= Marshal(&codeStruct
); err
!= nil {
102 b
.Fatal("Marshal:", err
)
105 b
.SetBytes(int64(len(codeJSON
)))
108 func BenchmarkCodeDecoder(b
*testing
.B
) {
115 dec
:= NewDecoder(&buf
)
117 for i
:= 0; i
< b
.N
; i
++ {
123 if err
:= dec
.Decode(&r
); err
!= nil {
124 b
.Fatal("Decode:", err
)
127 b
.SetBytes(int64(len(codeJSON
)))
130 func BenchmarkDecoderStream(b
*testing
.B
) {
133 dec
:= NewDecoder(&buf
)
134 buf
.WriteString(`"` + strings
.Repeat("x", 1000000) + `"` + "\n\n\n")
136 if err
:= dec
.Decode(&x
); err
!= nil {
137 b
.Fatal("Decode:", err
)
139 ones
:= strings
.Repeat(" 1\n", 300000) + "\n\n\n"
141 for i
:= 0; i
< b
.N
; i
++ {
143 buf
.WriteString(ones
)
146 if err
:= dec
.Decode(&x
); err
!= nil || x
!= 1.0 {
147 b
.Fatalf("Decode: %v after %d", err
, i
)
152 func BenchmarkCodeUnmarshal(b
*testing
.B
) {
158 for i
:= 0; i
< b
.N
; i
++ {
160 if err
:= Unmarshal(codeJSON
, &r
); err
!= nil {
161 b
.Fatal("Unmarshal:", err
)
164 b
.SetBytes(int64(len(codeJSON
)))
167 func BenchmarkCodeUnmarshalReuse(b
*testing
.B
) {
174 for i
:= 0; i
< b
.N
; i
++ {
175 if err
:= Unmarshal(codeJSON
, &r
); err
!= nil {
176 b
.Fatal("Unmarshal:", err
)
181 func BenchmarkUnmarshalString(b
*testing
.B
) {
182 data
:= []byte(`"hello, world"`)
185 for i
:= 0; i
< b
.N
; i
++ {
186 if err
:= Unmarshal(data
, &s
); err
!= nil {
187 b
.Fatal("Unmarshal:", err
)
192 func BenchmarkUnmarshalFloat64(b
*testing
.B
) {
194 data
:= []byte(`3.14`)
196 for i
:= 0; i
< b
.N
; i
++ {
197 if err
:= Unmarshal(data
, &f
); err
!= nil {
198 b
.Fatal("Unmarshal:", err
)
203 func BenchmarkUnmarshalInt64(b
*testing
.B
) {
207 for i
:= 0; i
< b
.N
; i
++ {
208 if err
:= Unmarshal(data
, &x
); err
!= nil {
209 b
.Fatal("Unmarshal:", err
)
214 func BenchmarkIssue10335(b
*testing
.B
) {
217 j
:= []byte(`{"a":{ }}`)
218 for n
:= 0; n
< b
.N
; n
++ {
219 if err
:= Unmarshal(j
, &s
); err
!= nil {