Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
552a556a
Commit
552a556a
authored
Nov 15, 2011
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
encoding/json: add marshal/unmarshal benchmark
R=bradfitz CC=golang-dev
https://golang.org/cl/5387041
parent
8998673c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
157 additions
and
0 deletions
+157
-0
bench_test.go
src/pkg/encoding/json/bench_test.go
+157
-0
code.json.gz
src/pkg/encoding/json/testdata/code.json.gz
+0
-0
No files found.
src/pkg/encoding/json/bench_test.go
0 → 100644
View file @
552a556a
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Large data benchmark.
// The JSON data is a summary of agl's changes in the
// go, webkit, and chromium open source projects.
// We benchmark converting between the JSON form
// and in-memory data structures.
package
json
import
(
"bytes"
"compress/gzip"
"io/ioutil"
"os"
"testing"
)
type
codeResponse
struct
{
Tree
*
codeNode
`json:"tree"`
Username
string
`json:"username"`
}
type
codeNode
struct
{
Name
string
`json:"name"`
Kids
[]
*
codeNode
`json:"kids"`
CLWeight
float64
`json:"cl_weight"`
Touches
int
`json:"touches"`
MinT
int64
`json:"min_t"`
MaxT
int64
`json:"max_t"`
MeanT
int64
`json:"mean_t"`
}
var
codeJSON
[]
byte
var
codeStruct
codeResponse
func
codeInit
()
{
f
,
err
:=
os
.
Open
(
"testdata/code.json.gz"
)
if
err
!=
nil
{
panic
(
err
)
}
defer
f
.
Close
()
gz
,
err
:=
gzip
.
NewReader
(
f
)
if
err
!=
nil
{
panic
(
err
)
}
data
,
err
:=
ioutil
.
ReadAll
(
gz
)
if
err
!=
nil
{
panic
(
err
)
}
codeJSON
=
data
if
err
:=
Unmarshal
(
codeJSON
,
&
codeStruct
);
err
!=
nil
{
panic
(
"unmarshal code.json: "
+
err
.
Error
())
}
if
data
,
err
=
Marshal
(
&
codeStruct
);
err
!=
nil
{
panic
(
"marshal code.json: "
+
err
.
Error
())
}
if
!
bytes
.
Equal
(
data
,
codeJSON
)
{
println
(
"different lengths"
,
len
(
data
),
len
(
codeJSON
))
for
i
:=
0
;
i
<
len
(
data
)
&&
i
<
len
(
codeJSON
);
i
++
{
if
data
[
i
]
!=
codeJSON
[
i
]
{
println
(
"re-marshal: changed at byte"
,
i
)
println
(
"orig: "
,
string
(
codeJSON
[
i
-
10
:
i
+
10
]))
println
(
"new: "
,
string
(
data
[
i
-
10
:
i
+
10
]))
break
}
}
panic
(
"re-marshal code.json: different result"
)
}
}
func
BenchmarkCodeEncoder
(
b
*
testing
.
B
)
{
if
codeJSON
==
nil
{
b
.
StopTimer
()
codeInit
()
b
.
StartTimer
()
}
enc
:=
NewEncoder
(
ioutil
.
Discard
)
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
if
err
:=
enc
.
Encode
(
&
codeStruct
);
err
!=
nil
{
panic
(
err
)
}
}
b
.
SetBytes
(
int64
(
len
(
codeJSON
)))
}
func
BenchmarkCodeMarshal
(
b
*
testing
.
B
)
{
if
codeJSON
==
nil
{
b
.
StopTimer
()
codeInit
()
b
.
StartTimer
()
}
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
if
_
,
err
:=
Marshal
(
&
codeStruct
);
err
!=
nil
{
panic
(
err
)
}
}
b
.
SetBytes
(
int64
(
len
(
codeJSON
)))
}
func
BenchmarkCodeDecoder
(
b
*
testing
.
B
)
{
if
codeJSON
==
nil
{
b
.
StopTimer
()
codeInit
()
b
.
StartTimer
()
}
var
buf
bytes
.
Buffer
dec
:=
NewDecoder
(
&
buf
)
var
r
codeResponse
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
buf
.
Write
(
codeJSON
)
// hide EOF
buf
.
WriteByte
(
'\n'
)
buf
.
WriteByte
(
'\n'
)
buf
.
WriteByte
(
'\n'
)
if
err
:=
dec
.
Decode
(
&
r
);
err
!=
nil
{
panic
(
err
)
}
}
b
.
SetBytes
(
int64
(
len
(
codeJSON
)))
}
func
BenchmarkCodeUnmarshal
(
b
*
testing
.
B
)
{
if
codeJSON
==
nil
{
b
.
StopTimer
()
codeInit
()
b
.
StartTimer
()
}
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
var
r
codeResponse
if
err
:=
Unmarshal
(
codeJSON
,
&
r
);
err
!=
nil
{
panic
(
err
)
}
}
b
.
SetBytes
(
int64
(
len
(
codeJSON
)))
}
func
BenchmarkCodeUnmarshalReuse
(
b
*
testing
.
B
)
{
if
codeJSON
==
nil
{
b
.
StopTimer
()
codeInit
()
b
.
StartTimer
()
}
var
r
codeResponse
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
if
err
:=
Unmarshal
(
codeJSON
,
&
r
);
err
!=
nil
{
panic
(
err
)
}
}
b
.
SetBytes
(
int64
(
len
(
codeJSON
)))
}
src/pkg/encoding/json/testdata/code.json.gz
0 → 100644
View file @
552a556a
File added
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment