-
Notifications
You must be signed in to change notification settings - Fork 764
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Import json package from Go under internal.
- Loading branch information
Showing
19 changed files
with
7,632 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Copyright (c) 2012 The Go Authors. All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are | ||
met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following disclaimer | ||
in the documentation and/or other materials provided with the | ||
distribution. | ||
* Neither the name of Google Inc. nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
// 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" | ||
"strings" | ||
"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 { | ||
b.Fatal("Encode:", 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 { | ||
b.Fatal("Marshal:", 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 { | ||
b.Fatal("Decode:", err) | ||
} | ||
} | ||
b.SetBytes(int64(len(codeJSON))) | ||
} | ||
|
||
func BenchmarkDecoderStream(b *testing.B) { | ||
b.StopTimer() | ||
var buf bytes.Buffer | ||
dec := NewDecoder(&buf) | ||
buf.WriteString(`"` + strings.Repeat("x", 1000000) + `"` + "\n\n\n") | ||
var x interface{} | ||
if err := dec.Decode(&x); err != nil { | ||
b.Fatal("Decode:", err) | ||
} | ||
ones := strings.Repeat(" 1\n", 300000) + "\n\n\n" | ||
b.StartTimer() | ||
for i := 0; i < b.N; i++ { | ||
if i%300000 == 0 { | ||
buf.WriteString(ones) | ||
} | ||
x = nil | ||
if err := dec.Decode(&x); err != nil || x != 1.0 { | ||
b.Fatalf("Decode: %v after %d", err, i) | ||
} | ||
} | ||
} | ||
|
||
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 { | ||
b.Fatal("Unmarshal:", 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 { | ||
b.Fatal("Unmarshal:", err) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkUnmarshalString(b *testing.B) { | ||
data := []byte(`"hello, world"`) | ||
var s string | ||
|
||
for i := 0; i < b.N; i++ { | ||
if err := Unmarshal(data, &s); err != nil { | ||
b.Fatal("Unmarshal:", err) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkUnmarshalFloat64(b *testing.B) { | ||
var f float64 | ||
data := []byte(`3.14`) | ||
|
||
for i := 0; i < b.N; i++ { | ||
if err := Unmarshal(data, &f); err != nil { | ||
b.Fatal("Unmarshal:", err) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkUnmarshalInt64(b *testing.B) { | ||
var x int64 | ||
data := []byte(`3`) | ||
|
||
for i := 0; i < b.N; i++ { | ||
if err := Unmarshal(data, &x); err != nil { | ||
b.Fatal("Unmarshal:", err) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkIssue10335(b *testing.B) { | ||
b.ReportAllocs() | ||
var s struct{} | ||
j := []byte(`{"a":{ }}`) | ||
for n := 0; n < b.N; n++ { | ||
if err := Unmarshal(j, &s); err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
} |
Oops, something went wrong.