|
| 1 | +/* |
| 2 | + * MinIO Cloud Storage, (C) 2022 MinIO, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package simdjson_benchmarks |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "io/ioutil" |
| 22 | + "path/filepath" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/buger/jsonparser" |
| 26 | + jsoniter "github.com/json-iterator/go" |
| 27 | + "github.com/klauspost/compress/zstd" |
| 28 | + |
| 29 | + simdjson "github.com/minio/simdjson-go" |
| 30 | +) |
| 31 | + |
| 32 | +func benchmarkEncodingJson(b *testing.B, filename string) { |
| 33 | + msg := loadCompressed(b, filename) |
| 34 | + |
| 35 | + b.SetBytes(int64(len(msg))) |
| 36 | + b.ReportAllocs() |
| 37 | + b.ResetTimer() |
| 38 | + |
| 39 | + var parsed interface{} |
| 40 | + for i := 0; i < b.N; i++ { |
| 41 | + if err := json.Unmarshal(msg, &parsed); err != nil { |
| 42 | + b.Fatal(err) |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func benchmarkJsoniter(b *testing.B, filename string) { |
| 48 | + msg := loadCompressed(b, filename) |
| 49 | + |
| 50 | + b.SetBytes(int64(len(msg))) |
| 51 | + b.ReportAllocs() |
| 52 | + b.ResetTimer() |
| 53 | + |
| 54 | + var json = jsoniter.ConfigCompatibleWithStandardLibrary |
| 55 | + var parsed interface{} |
| 56 | + for i := 0; i < b.N; i++ { |
| 57 | + if err := json.Unmarshal(msg, &parsed); err != nil { |
| 58 | + b.Fatal(err) |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func benchmarkSimdJson(b *testing.B, filename string) { |
| 64 | + if !simdjson.SupportedCPU() { |
| 65 | + b.SkipNow() |
| 66 | + } |
| 67 | + |
| 68 | + msg := loadCompressed(b, filename) |
| 69 | + |
| 70 | + b.SetBytes(int64(len(msg))) |
| 71 | + b.ReportAllocs() |
| 72 | + b.ResetTimer() |
| 73 | + |
| 74 | + pj := &simdjson.ParsedJson{} |
| 75 | + for i := 0; i < b.N; i++ { |
| 76 | + // Reset tape |
| 77 | + var err error |
| 78 | + pj, err = simdjson.Parse(msg, pj, simdjson.WithCopyStrings(false)) |
| 79 | + if err != nil { |
| 80 | + b.Fatal(err) |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func BenchmarkEncodingJsonApache_builds(b *testing.B) { benchmarkEncodingJson(b, "apache_builds") } |
| 86 | +func BenchmarkEncodingJsonCanada(b *testing.B) { benchmarkEncodingJson(b, "canada") } |
| 87 | +func BenchmarkEncodingJsonCitm_catalog(b *testing.B) { benchmarkEncodingJson(b, "citm_catalog") } |
| 88 | +func BenchmarkEncodingJsonGithub_events(b *testing.B) { benchmarkEncodingJson(b, "github_events") } |
| 89 | +func BenchmarkEncodingJsonGsoc_2018(b *testing.B) { benchmarkEncodingJson(b, "gsoc-2018") } |
| 90 | +func BenchmarkEncodingJsonInstruments(b *testing.B) { benchmarkEncodingJson(b, "instruments") } |
| 91 | +func BenchmarkEncodingJsonMarine_ik(b *testing.B) { benchmarkEncodingJson(b, "marine_ik") } |
| 92 | +func BenchmarkEncodingJsonMesh(b *testing.B) { benchmarkEncodingJson(b, "mesh") } |
| 93 | +func BenchmarkEncodingJsonMesh_pretty(b *testing.B) { benchmarkEncodingJson(b, "mesh.pretty") } |
| 94 | +func BenchmarkEncodingJsonNumbers(b *testing.B) { benchmarkEncodingJson(b, "numbers") } |
| 95 | +func BenchmarkEncodingJsonRandom(b *testing.B) { benchmarkEncodingJson(b, "random") } |
| 96 | +func BenchmarkEncodingJsonTwitter(b *testing.B) { benchmarkEncodingJson(b, "twitter") } |
| 97 | +func BenchmarkEncodingJsonTwitterescaped(b *testing.B) { benchmarkEncodingJson(b, "twitterescaped") } |
| 98 | +func BenchmarkEncodingJsonUpdate_center(b *testing.B) { benchmarkEncodingJson(b, "update-center") } |
| 99 | + |
| 100 | +func BenchmarkJsoniterApache_builds(b *testing.B) { benchmarkJsoniter(b, "apache_builds") } |
| 101 | +func BenchmarkJsoniterCanada(b *testing.B) { benchmarkJsoniter(b, "canada") } |
| 102 | +func BenchmarkJsoniterCitm_catalog(b *testing.B) { benchmarkJsoniter(b, "citm_catalog") } |
| 103 | +func BenchmarkJsoniterGithub_events(b *testing.B) { benchmarkJsoniter(b, "github_events") } |
| 104 | +func BenchmarkJsoniterGsoc_2018(b *testing.B) { benchmarkJsoniter(b, "gsoc-2018") } |
| 105 | +func BenchmarkJsoniterInstruments(b *testing.B) { benchmarkJsoniter(b, "instruments") } |
| 106 | +func BenchmarkJsoniterMarine_ik(b *testing.B) { benchmarkJsoniter(b, "marine_ik") } |
| 107 | +func BenchmarkJsoniterMesh(b *testing.B) { benchmarkJsoniter(b, "mesh") } |
| 108 | +func BenchmarkJsoniterMesh_pretty(b *testing.B) { benchmarkJsoniter(b, "mesh.pretty") } |
| 109 | +func BenchmarkJsoniterNumbers(b *testing.B) { benchmarkJsoniter(b, "numbers") } |
| 110 | +func BenchmarkJsoniterRandom(b *testing.B) { benchmarkJsoniter(b, "random") } |
| 111 | +func BenchmarkJsoniterTwitter(b *testing.B) { benchmarkJsoniter(b, "twitter") } |
| 112 | +func BenchmarkJsoniterTwitterescaped(b *testing.B) { benchmarkJsoniter(b, "twitterescaped") } |
| 113 | +func BenchmarkJsoniterUpdate_center(b *testing.B) { benchmarkJsoniter(b, "update-center") } |
| 114 | + |
| 115 | +func BenchmarkSimdJsonApache_builds(b *testing.B) { benchmarkSimdJson(b, "apache_builds") } |
| 116 | +func BenchmarkSimdJsonCanada(b *testing.B) { benchmarkSimdJson(b, "canada") } |
| 117 | +func BenchmarkSimdJsonCitm_catalog(b *testing.B) { benchmarkSimdJson(b, "citm_catalog") } |
| 118 | +func BenchmarkSimdJsonGithub_events(b *testing.B) { benchmarkSimdJson(b, "github_events") } |
| 119 | +func BenchmarkSimdJsonGsoc_2018(b *testing.B) { benchmarkSimdJson(b, "gsoc-2018") } |
| 120 | +func BenchmarkSimdJsonInstruments(b *testing.B) { benchmarkSimdJson(b, "instruments") } |
| 121 | +func BenchmarkSimdJsonMarine_ik(b *testing.B) { benchmarkSimdJson(b, "marine_ik") } |
| 122 | +func BenchmarkSimdJsonMesh(b *testing.B) { benchmarkSimdJson(b, "mesh") } |
| 123 | +func BenchmarkSimdJsonMesh_pretty(b *testing.B) { benchmarkSimdJson(b, "mesh.pretty") } |
| 124 | +func BenchmarkSimdJsonNumbers(b *testing.B) { benchmarkSimdJson(b, "numbers") } |
| 125 | +func BenchmarkSimdJsonRandom(b *testing.B) { benchmarkSimdJson(b, "random") } |
| 126 | +func BenchmarkSimdJsonTwitter(b *testing.B) { benchmarkSimdJson(b, "twitter") } |
| 127 | +func BenchmarkSimdJsonTwitterEscaped(b *testing.B) { benchmarkSimdJson(b, "twitterescaped") } |
| 128 | +func BenchmarkSimdJsonUpdate_center(b *testing.B) { benchmarkSimdJson(b, "update-center") } |
| 129 | + |
| 130 | +func BenchmarkBugerJsonParserLarge(b *testing.B) { |
| 131 | + largeFixture := loadCompressed(b, "payload-large") |
| 132 | + const logVals = false |
| 133 | + b.SetBytes(int64(len(largeFixture))) |
| 134 | + b.ReportAllocs() |
| 135 | + b.ResetTimer() |
| 136 | + var dump int |
| 137 | + for i := 0; i < b.N; i++ { |
| 138 | + jsonparser.ArrayEach(largeFixture, func(value []byte, dataType jsonparser.ValueType, offset int, err error) { |
| 139 | + sval, _, _, _ := jsonparser.Get(value, "username") |
| 140 | + if logVals && i == 0 { |
| 141 | + b.Log(string(sval)) |
| 142 | + } |
| 143 | + dump += len(sval) |
| 144 | + }, "users") |
| 145 | + |
| 146 | + jsonparser.ArrayEach(largeFixture, func(value []byte, dataType jsonparser.ValueType, offset int, err error) { |
| 147 | + ival, _ := jsonparser.GetInt(value, "id") |
| 148 | + if logVals && i == 0 { |
| 149 | + b.Log(ival) |
| 150 | + } |
| 151 | + dump += int(ival) |
| 152 | + sval, _, _, _ := jsonparser.Get(value, "slug") |
| 153 | + if logVals && i == 0 { |
| 154 | + b.Log(string(sval)) |
| 155 | + } |
| 156 | + dump += len(sval) |
| 157 | + }, "topics", "topics") |
| 158 | + } |
| 159 | + if dump == 0 { |
| 160 | + b.Log("") |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +// tester and loadCompressed should be kept in sync with minio/simdjson-go/parsed_json_test.go. |
| 165 | +type tester interface { |
| 166 | + Fatal(args ...interface{}) |
| 167 | +} |
| 168 | + |
| 169 | +func loadCompressed(t tester, file string) (ref []byte) { |
| 170 | + dec, err := zstd.NewReader(nil) |
| 171 | + if err != nil { |
| 172 | + t.Fatal(err) |
| 173 | + } |
| 174 | + ref, err = ioutil.ReadFile(filepath.Join("../", "testdata", file+".json.zst")) |
| 175 | + if err != nil { |
| 176 | + t.Fatal(err) |
| 177 | + } |
| 178 | + ref, err = dec.DecodeAll(ref, nil) |
| 179 | + if err != nil { |
| 180 | + t.Fatal(err) |
| 181 | + } |
| 182 | + |
| 183 | + return ref |
| 184 | +} |
0 commit comments