Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame^] | 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. |
| 4 | |
| 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. |
| 10 | |
| 11 | package json |
| 12 | |
| 13 | import ( |
| 14 | "bytes" |
| 15 | "compress/gzip" |
| 16 | "io/ioutil" |
| 17 | "os" |
| 18 | "testing" |
| 19 | ) |
| 20 | |
| 21 | type codeResponse struct { |
| 22 | Tree *codeNode `json:"tree"` |
| 23 | Username string `json:"username"` |
| 24 | } |
| 25 | |
| 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"` |
| 34 | } |
| 35 | |
| 36 | var codeJSON []byte |
| 37 | var codeStruct codeResponse |
| 38 | |
| 39 | func codeInit() { |
| 40 | f, err := os.Open("testdata/code.json.gz") |
| 41 | if err != nil { |
| 42 | panic(err) |
| 43 | } |
| 44 | defer f.Close() |
| 45 | gz, err := gzip.NewReader(f) |
| 46 | if err != nil { |
| 47 | panic(err) |
| 48 | } |
| 49 | data, err := ioutil.ReadAll(gz) |
| 50 | if err != nil { |
| 51 | panic(err) |
| 52 | } |
| 53 | |
| 54 | codeJSON = data |
| 55 | |
| 56 | if err := Unmarshal(codeJSON, &codeStruct); err != nil { |
| 57 | panic("unmarshal code.json: " + err.Error()) |
| 58 | } |
| 59 | |
| 60 | if data, err = Marshal(&codeStruct); err != nil { |
| 61 | panic("marshal code.json: " + err.Error()) |
| 62 | } |
| 63 | |
| 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])) |
| 71 | break |
| 72 | } |
| 73 | } |
| 74 | panic("re-marshal code.json: different result") |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func BenchmarkCodeEncoder(b *testing.B) { |
| 79 | if codeJSON == nil { |
| 80 | b.StopTimer() |
| 81 | codeInit() |
| 82 | b.StartTimer() |
| 83 | } |
| 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) |
| 88 | } |
| 89 | } |
| 90 | b.SetBytes(int64(len(codeJSON))) |
| 91 | } |
| 92 | |
| 93 | func BenchmarkCodeMarshal(b *testing.B) { |
| 94 | if codeJSON == nil { |
| 95 | b.StopTimer() |
| 96 | codeInit() |
| 97 | b.StartTimer() |
| 98 | } |
| 99 | for i := 0; i < b.N; i++ { |
| 100 | if _, err := Marshal(&codeStruct); err != nil { |
| 101 | b.Fatal("Marshal:", err) |
| 102 | } |
| 103 | } |
| 104 | b.SetBytes(int64(len(codeJSON))) |
| 105 | } |
| 106 | |
| 107 | func BenchmarkCodeDecoder(b *testing.B) { |
| 108 | if codeJSON == nil { |
| 109 | b.StopTimer() |
| 110 | codeInit() |
| 111 | b.StartTimer() |
| 112 | } |
| 113 | var buf bytes.Buffer |
| 114 | dec := NewDecoder(&buf) |
| 115 | var r codeResponse |
| 116 | for i := 0; i < b.N; i++ { |
| 117 | buf.Write(codeJSON) |
| 118 | // hide EOF |
| 119 | buf.WriteByte('\n') |
| 120 | buf.WriteByte('\n') |
| 121 | buf.WriteByte('\n') |
| 122 | if err := dec.Decode(&r); err != nil { |
| 123 | b.Fatal("Decode:", err) |
| 124 | } |
| 125 | } |
| 126 | b.SetBytes(int64(len(codeJSON))) |
| 127 | } |
| 128 | |
| 129 | func BenchmarkCodeUnmarshal(b *testing.B) { |
| 130 | if codeJSON == nil { |
| 131 | b.StopTimer() |
| 132 | codeInit() |
| 133 | b.StartTimer() |
| 134 | } |
| 135 | for i := 0; i < b.N; i++ { |
| 136 | var r codeResponse |
| 137 | if err := Unmarshal(codeJSON, &r); err != nil { |
| 138 | b.Fatal("Unmmarshal:", err) |
| 139 | } |
| 140 | } |
| 141 | b.SetBytes(int64(len(codeJSON))) |
| 142 | } |
| 143 | |
| 144 | func BenchmarkCodeUnmarshalReuse(b *testing.B) { |
| 145 | if codeJSON == nil { |
| 146 | b.StopTimer() |
| 147 | codeInit() |
| 148 | b.StartTimer() |
| 149 | } |
| 150 | var r codeResponse |
| 151 | for i := 0; i < b.N; i++ { |
| 152 | if err := Unmarshal(codeJSON, &r); err != nil { |
| 153 | b.Fatal("Unmmarshal:", err) |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func BenchmarkUnmarshalString(b *testing.B) { |
| 159 | data := []byte(`"hello, world"`) |
| 160 | var s string |
| 161 | |
| 162 | for i := 0; i < b.N; i++ { |
| 163 | if err := Unmarshal(data, &s); err != nil { |
| 164 | b.Fatal("Unmarshal:", err) |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | func BenchmarkUnmarshalFloat64(b *testing.B) { |
| 170 | var f float64 |
| 171 | data := []byte(`3.14`) |
| 172 | |
| 173 | for i := 0; i < b.N; i++ { |
| 174 | if err := Unmarshal(data, &f); err != nil { |
| 175 | b.Fatal("Unmarshal:", err) |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | func BenchmarkUnmarshalInt64(b *testing.B) { |
| 181 | var x int64 |
| 182 | data := []byte(`3`) |
| 183 | |
| 184 | for i := 0; i < b.N; i++ { |
| 185 | if err := Unmarshal(data, &x); err != nil { |
| 186 | b.Fatal("Unmarshal:", err) |
| 187 | } |
| 188 | } |
| 189 | } |