github.com/dgraph-io/simdjson-go@v0.3.0/benchmarks_test.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2020 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 18 19 import ( 20 "encoding/json" 21 "testing" 22 23 jsoniter "github.com/json-iterator/go" 24 ) 25 26 func benchmarkFromFile(b *testing.B, filename string) { 27 if !SupportedCPU() { 28 b.SkipNow() 29 } 30 msg := loadCompressed(b, filename) 31 32 b.SetBytes(int64(len(msg))) 33 b.ReportAllocs() 34 b.ResetTimer() 35 36 pj := &ParsedJson{} 37 38 for i := 0; i < b.N; i++ { 39 // Reset tape 40 var err error 41 pj, err = Parse(msg, pj) 42 if err != nil { 43 b.Fatal(err) 44 } 45 } 46 } 47 48 func BenchmarkApache_builds(b *testing.B) { benchmarkFromFile(b, "apache_builds") } 49 func BenchmarkCanada(b *testing.B) { benchmarkFromFile(b, "canada") } 50 func BenchmarkCitm_catalog(b *testing.B) { benchmarkFromFile(b, "citm_catalog") } 51 func BenchmarkGithub_events(b *testing.B) { benchmarkFromFile(b, "github_events") } 52 func BenchmarkGsoc_2018(b *testing.B) { benchmarkFromFile(b, "gsoc-2018") } 53 func BenchmarkInstruments(b *testing.B) { benchmarkFromFile(b, "instruments") } 54 func BenchmarkMarine_ik(b *testing.B) { benchmarkFromFile(b, "marine_ik") } 55 func BenchmarkMesh(b *testing.B) { benchmarkFromFile(b, "mesh") } 56 func BenchmarkMesh_pretty(b *testing.B) { benchmarkFromFile(b, "mesh.pretty") } 57 func BenchmarkNumbers(b *testing.B) { benchmarkFromFile(b, "numbers") } 58 func BenchmarkRandom(b *testing.B) { benchmarkFromFile(b, "random") } 59 func BenchmarkTwitter(b *testing.B) { benchmarkFromFile(b, "twitter") } 60 func BenchmarkTwitterescaped(b *testing.B) { benchmarkFromFile(b, "twitterescaped") } 61 func BenchmarkUpdate_center(b *testing.B) { benchmarkFromFile(b, "update-center") } 62 63 func benchmarkJsoniter(b *testing.B, filename string) { 64 65 msg := loadCompressed(b, filename) 66 67 b.SetBytes(int64(len(msg))) 68 b.ReportAllocs() 69 b.ResetTimer() 70 71 var json = jsoniter.ConfigCompatibleWithStandardLibrary 72 for i := 0; i < b.N; i++ { 73 74 var parsed interface{} 75 if err := json.Unmarshal(msg, &parsed); err != nil { 76 b.Fatal(err) 77 } 78 } 79 } 80 81 func benchmarkEncodingJson(b *testing.B, filename string) { 82 83 msg := loadCompressed(b, filename) 84 85 b.SetBytes(int64(len(msg))) 86 b.ReportAllocs() 87 b.ResetTimer() 88 89 for i := 0; i < b.N; i++ { 90 91 var parsed interface{} 92 if err := json.Unmarshal(msg, &parsed); err != nil { 93 b.Fatal(err) 94 } 95 } 96 } 97 98 func BenchmarkEncodingJsonApache_builds(b *testing.B) { benchmarkEncodingJson(b, "apache_builds") } 99 func BenchmarkEncodingJsonCanada(b *testing.B) { benchmarkEncodingJson(b, "canada") } 100 func BenchmarkEncodingJsonCitm_catalog(b *testing.B) { benchmarkEncodingJson(b, "citm_catalog") } 101 func BenchmarkEncodingJsonGithub_events(b *testing.B) { benchmarkEncodingJson(b, "github_events") } 102 func BenchmarkEncodingJsonGsoc_2018(b *testing.B) { benchmarkEncodingJson(b, "gsoc-2018") } 103 func BenchmarkEncodingJsonInstruments(b *testing.B) { benchmarkEncodingJson(b, "instruments") } 104 func BenchmarkEncodingJsonMarine_ik(b *testing.B) { benchmarkEncodingJson(b, "marine_ik") } 105 func BenchmarkEncodingJsonMesh(b *testing.B) { benchmarkEncodingJson(b, "mesh") } 106 func BenchmarkEncodingJsonMesh_pretty(b *testing.B) { benchmarkEncodingJson(b, "mesh.pretty") } 107 func BenchmarkEncodingJsonNumbers(b *testing.B) { benchmarkEncodingJson(b, "numbers") } 108 func BenchmarkEncodingJsonRandom(b *testing.B) { benchmarkEncodingJson(b, "random") } 109 func BenchmarkEncodingJsonTwitter(b *testing.B) { benchmarkEncodingJson(b, "twitter") } 110 func BenchmarkEncodingJsonTwitterescaped(b *testing.B) { benchmarkEncodingJson(b, "twitterescaped") } 111 func BenchmarkEncodingJsonUpdate_center(b *testing.B) { benchmarkEncodingJson(b, "update-center") } 112 113 func BenchmarkJsoniterApache_builds(b *testing.B) { benchmarkJsoniter(b, "apache_builds") } 114 func BenchmarkJsoniterCanada(b *testing.B) { benchmarkJsoniter(b, "canada") } 115 func BenchmarkJsoniterCitm_catalog(b *testing.B) { benchmarkJsoniter(b, "citm_catalog") } 116 func BenchmarkJsoniterGithub_events(b *testing.B) { benchmarkJsoniter(b, "github_events") } 117 func BenchmarkJsoniterGsoc_2018(b *testing.B) { benchmarkJsoniter(b, "gsoc-2018") } 118 func BenchmarkJsoniterInstruments(b *testing.B) { benchmarkJsoniter(b, "instruments") } 119 func BenchmarkJsoniterMarine_ik(b *testing.B) { benchmarkJsoniter(b, "marine_ik") } 120 func BenchmarkJsoniterMesh(b *testing.B) { benchmarkJsoniter(b, "mesh") } 121 func BenchmarkJsoniterMesh_pretty(b *testing.B) { benchmarkJsoniter(b, "mesh.pretty") } 122 func BenchmarkJsoniterNumbers(b *testing.B) { benchmarkJsoniter(b, "numbers") } 123 func BenchmarkJsoniterRandom(b *testing.B) { benchmarkJsoniter(b, "random") } 124 func BenchmarkJsoniterTwitter(b *testing.B) { benchmarkJsoniter(b, "twitter") } 125 func BenchmarkJsoniterTwitterescaped(b *testing.B) { benchmarkJsoniter(b, "twitterescaped") } 126 func BenchmarkJsoniterUpdate_center(b *testing.B) { benchmarkJsoniter(b, "update-center") }