| 1 |
package benchmark |
| 2 |
|
| 3 |
import ( |
| 4 |
stdjson "encoding/json" |
| 5 |
"fmt" |
| 6 |
"reflect" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/bytedance/sonic" |
| 11 |
gojson "github.com/goccy/go-json" |
| 12 |
) |
| 13 |
|
| 14 |
// The decode benchmarks of the keys: structs of the same fields and values whose keys differ only, each decoded |
| 15 |
// from an array of objects. The keys of ShortKeys are of less than 8 bytes, the ones of MediumKeys of 8 to 15 |
| 16 |
// bytes and the ones of LongKeys of 16 to 31 bytes. The keys of real payloads are of every one of these |
| 17 |
// lengths: in the responses of the GitHub API, of Kubernetes, of Stripe and of Twitter, 40 to 60% of the keys |
| 18 |
// are of 8 to 15 bytes and 5 to 30% longer. The keys of NonASCIIKeys are Japanese words. |
| 19 |
|
| 20 |
type ShortKeys struct { |
| 21 |
ID int `json:"id"` |
| 22 |
Name string `json:"name"` |
| 23 |
Type string `json:"type"` |
| 24 |
State string `json:"state"` |
| 25 |
Count int `json:"count"` |
| 26 |
Email string `json:"email"` |
| 27 |
URL string `json:"url"` |
| 28 |
Size int `json:"size"` |
| 29 |
Title string `json:"title"` |
| 30 |
Forks int `json:"forks"` |
| 31 |
Owner string `json:"owner"` |
| 32 |
Issues int `json:"issues"` |
| 33 |
} |
| 34 |
|
| 35 |
type MediumKeys struct { |
| 36 |
ID int `json:"repository_id"` |
| 37 |
Name string `json:"full_name"` |
| 38 |
Type string `json:"visibility"` |
| 39 |
State string `json:"merge_state"` |
| 40 |
Count int `json:"forks_count"` |
| 41 |
Email string `json:"author_email"` |
| 42 |
URL string `json:"html_url"` |
| 43 |
Size int `json:"disk_usage"` |
| 44 |
Title string `json:"description"` |
| 45 |
Forks int `json:"watchers_count"` |
| 46 |
Owner string `json:"default_branch"` |
| 47 |
Issues int `json:"open_issues"` |
| 48 |
} |
| 49 |
|
| 50 |
type LongKeys struct { |
| 51 |
ID int `json:"stargazers_count"` |
| 52 |
Name string `json:"squash_merge_commit_title"` |
| 53 |
Type string `json:"merge_commit_message"` |
| 54 |
State string `json:"security_and_analysis"` |
| 55 |
Count int `json:"subscribers_count"` |
| 56 |
Email string `json:"commit_author_email_address"` |
| 57 |
URL string `json:"repository_html_url"` |
| 58 |
Size int `json:"open_issues_count"` |
| 59 |
Title string `json:"repository_description"` |
| 60 |
Forks int `json:"delete_branch_on_merge"` |
| 61 |
Owner string `json:"allow_update_branch"` |
| 62 |
Issues int `json:"web_commit_signoff_required"` |
| 63 |
} |
| 64 |
|
| 65 |
type NonASCIIKeys struct { |
| 66 |
ID int `json:"番号"` |
| 67 |
Name string `json:"名前"` |
| 68 |
Type string `json:"種類"` |
| 69 |
State string `json:"状態"` |
| 70 |
Count int `json:"件数"` |
| 71 |
Email string `json:"メールアドレス"` |
| 72 |
URL string `json:"ウェブサイト"` |
| 73 |
Size int `json:"大きさ"` |
| 74 |
Title string `json:"表題"` |
| 75 |
Forks int `json:"複製数"` |
| 76 |
Owner string `json:"所有者"` |
| 77 |
Issues int `json:"課題数"` |
| 78 |
} |
| 79 |
|
| 80 |
// keyLengthFixture returns the JSON of an array of objects of the struct type, by its tags. |
| 81 |
func keyLengthFixture(typ reflect.Type) []byte { |
| 82 |
values := []string{`12345`, `"go-json"`, `"public"`, `"clean"`, `42`, `"[email protected]"`, |
| 83 |
`"https://github.com/goccy/go-json"`, `2048`, `"Fast JSON encoder/decoder compatible with encoding/json"`, |
| 84 |
`3100`, `"master"`, `17`} |
| 85 |
var objects []string |
| 86 |
for i := 0; i < 16; i++ { |
| 87 |
var fields []string |
| 88 |
for j := 0; j < typ.NumField(); j++ { |
| 89 |
fields = append(fields, fmt.Sprintf("%q:%s", typ.Field(j).Tag.Get("json"), values[j])) |
| 90 |
} |
| 91 |
objects = append(objects, "{"+strings.Join(fields, ",")+"}") |
| 92 |
} |
| 93 |
return []byte("[" + strings.Join(objects, ",") + "]") |
| 94 |
} |
| 95 |
|
| 96 |
var ( |
| 97 |
shortKeysFixture = keyLengthFixture(reflect.TypeOf(ShortKeys{})) |
| 98 |
mediumKeysFixture = keyLengthFixture(reflect.TypeOf(MediumKeys{})) |
| 99 |
longKeysFixture = keyLengthFixture(reflect.TypeOf(LongKeys{})) |
| 100 |
nonASCIIKeysFixture = keyLengthFixture(reflect.TypeOf(NonASCIIKeys{})) |
| 101 |
) |
| 102 |
|
| 103 |
func init() { |
| 104 |
sonicTypes = append(sonicTypes, reflect.TypeOf([]ShortKeys{}), reflect.TypeOf([]MediumKeys{}), reflect.TypeOf([]LongKeys{}), reflect.TypeOf([]NonASCIIKeys{})) |
| 105 |
} |
| 106 |
|
| 107 |
func benchDecodeOf[T any](b *testing.B, data []byte) { |
| 108 |
b.SetBytes(int64(len(data))) |
| 109 |
b.ReportAllocs() |
| 110 |
b.ResetTimer() |
| 111 |
for i := 0; i < b.N; i++ { |
| 112 |
var v T |
| 113 |
if err := gojson.UnmarshalOf(data, &v); err != nil { |
| 114 |
b.Fatal(err) |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func Benchmark_Decode_ShortKeys_Unmarshal_EncodingJson(b *testing.B) { |
| 120 |
benchDecode[[]ShortKeys](b, shortKeysFixture, stdjson.Unmarshal) |
| 121 |
} |
| 122 |
|
| 123 |
func Benchmark_Decode_ShortKeys_Unmarshal_GoJson(b *testing.B) { |
| 124 |
benchDecode[[]ShortKeys](b, shortKeysFixture, gojson.Unmarshal) |
| 125 |
} |
| 126 |
|
| 127 |
func Benchmark_Decode_ShortKeys_Unmarshal_GoJsonUnmarshalOf(b *testing.B) { |
| 128 |
benchDecodeOf[[]ShortKeys](b, shortKeysFixture) |
| 129 |
} |
| 130 |
|
| 131 |
func Benchmark_Decode_ShortKeys_Unmarshal_Sonic(b *testing.B) { |
| 132 |
pretouchSonic() |
| 133 |
benchDecode[[]ShortKeys](b, shortKeysFixture, sonic.ConfigDefault.Unmarshal) |
| 134 |
} |
| 135 |
|
| 136 |
func Benchmark_Decode_ShortKeys_Unmarshal_SonicStd(b *testing.B) { |
| 137 |
pretouchSonic() |
| 138 |
benchDecode[[]ShortKeys](b, shortKeysFixture, sonic.ConfigStd.Unmarshal) |
| 139 |
} |
| 140 |
|
| 141 |
func Benchmark_Decode_MediumKeys_Unmarshal_EncodingJson(b *testing.B) { |
| 142 |
benchDecode[[]MediumKeys](b, mediumKeysFixture, stdjson.Unmarshal) |
| 143 |
} |
| 144 |
|
| 145 |
func Benchmark_Decode_MediumKeys_Unmarshal_GoJson(b *testing.B) { |
| 146 |
benchDecode[[]MediumKeys](b, mediumKeysFixture, gojson.Unmarshal) |
| 147 |
} |
| 148 |
|
| 149 |
func Benchmark_Decode_MediumKeys_Unmarshal_GoJsonUnmarshalOf(b *testing.B) { |
| 150 |
benchDecodeOf[[]MediumKeys](b, mediumKeysFixture) |
| 151 |
} |
| 152 |
|
| 153 |
func Benchmark_Decode_MediumKeys_Unmarshal_Sonic(b *testing.B) { |
| 154 |
pretouchSonic() |
| 155 |
benchDecode[[]MediumKeys](b, mediumKeysFixture, sonic.ConfigDefault.Unmarshal) |
| 156 |
} |
| 157 |
|
| 158 |
func Benchmark_Decode_MediumKeys_Unmarshal_SonicStd(b *testing.B) { |
| 159 |
pretouchSonic() |
| 160 |
benchDecode[[]MediumKeys](b, mediumKeysFixture, sonic.ConfigStd.Unmarshal) |
| 161 |
} |
| 162 |
|
| 163 |
func Benchmark_Decode_LongKeys_Unmarshal_EncodingJson(b *testing.B) { |
| 164 |
benchDecode[[]LongKeys](b, longKeysFixture, stdjson.Unmarshal) |
| 165 |
} |
| 166 |
|
| 167 |
func Benchmark_Decode_LongKeys_Unmarshal_GoJson(b *testing.B) { |
| 168 |
benchDecode[[]LongKeys](b, longKeysFixture, gojson.Unmarshal) |
| 169 |
} |
| 170 |
|
| 171 |
func Benchmark_Decode_LongKeys_Unmarshal_GoJsonUnmarshalOf(b *testing.B) { |
| 172 |
benchDecodeOf[[]LongKeys](b, longKeysFixture) |
| 173 |
} |
| 174 |
|
| 175 |
func Benchmark_Decode_LongKeys_Unmarshal_Sonic(b *testing.B) { |
| 176 |
pretouchSonic() |
| 177 |
benchDecode[[]LongKeys](b, longKeysFixture, sonic.ConfigDefault.Unmarshal) |
| 178 |
} |
| 179 |
|
| 180 |
func Benchmark_Decode_LongKeys_Unmarshal_SonicStd(b *testing.B) { |
| 181 |
pretouchSonic() |
| 182 |
benchDecode[[]LongKeys](b, longKeysFixture, sonic.ConfigStd.Unmarshal) |
| 183 |
} |
| 184 |
|
| 185 |
func Benchmark_Decode_NonASCIIKeys_Unmarshal_EncodingJson(b *testing.B) { |
| 186 |
benchDecode[[]NonASCIIKeys](b, nonASCIIKeysFixture, stdjson.Unmarshal) |
| 187 |
} |
| 188 |
|
| 189 |
func Benchmark_Decode_NonASCIIKeys_Unmarshal_GoJson(b *testing.B) { |
| 190 |
benchDecode[[]NonASCIIKeys](b, nonASCIIKeysFixture, gojson.Unmarshal) |
| 191 |
} |
| 192 |
|
| 193 |
func Benchmark_Decode_NonASCIIKeys_Unmarshal_GoJsonUnmarshalOf(b *testing.B) { |
| 194 |
benchDecodeOf[[]NonASCIIKeys](b, nonASCIIKeysFixture) |
| 195 |
} |
| 196 |
|
| 197 |
func Benchmark_Decode_NonASCIIKeys_Unmarshal_Sonic(b *testing.B) { |
| 198 |
pretouchSonic() |
| 199 |
benchDecode[[]NonASCIIKeys](b, nonASCIIKeysFixture, sonic.ConfigDefault.Unmarshal) |
| 200 |
} |
| 201 |
|
| 202 |
func Benchmark_Decode_NonASCIIKeys_Unmarshal_SonicStd(b *testing.B) { |
| 203 |
pretouchSonic() |
| 204 |
benchDecode[[]NonASCIIKeys](b, nonASCIIKeysFixture, sonic.ConfigStd.Unmarshal) |
| 205 |
} |
| 206 |
|
| 207 |
// UnknownNonASCIIKeys decodes the objects of NonASCIIKeys into ShortKeys, whose keys are ASCII: every key is of |
| 208 |
// no field, and is skipped. |
| 209 |
func Benchmark_Decode_UnknownNonASCIIKeys_Unmarshal_EncodingJson(b *testing.B) { |
| 210 |
benchDecode[[]ShortKeys](b, nonASCIIKeysFixture, stdjson.Unmarshal) |
| 211 |
} |
| 212 |
|
| 213 |
func Benchmark_Decode_UnknownNonASCIIKeys_Unmarshal_GoJson(b *testing.B) { |
| 214 |
benchDecode[[]ShortKeys](b, nonASCIIKeysFixture, gojson.Unmarshal) |
| 215 |
} |
| 216 |
|
| 217 |
func Benchmark_Decode_UnknownNonASCIIKeys_Unmarshal_Sonic(b *testing.B) { |
| 218 |
pretouchSonic() |
| 219 |
benchDecode[[]ShortKeys](b, nonASCIIKeysFixture, sonic.ConfigDefault.Unmarshal) |
| 220 |
} |
| 221 |
|
| 222 |
func Benchmark_Decode_ShortKeys_Unmarshal_GoJsonUnmarshalOfNoCopyString(b *testing.B) { |
| 223 |
benchDecodeNoCopy[[]ShortKeys](b, shortKeysFixture) |
| 224 |
} |
| 225 |
|
| 226 |
func Benchmark_Decode_ShortKeys_Unmarshal_SonicFastest(b *testing.B) { |
| 227 |
pretouchSonic() |
| 228 |
benchDecodeSonicFastest[[]ShortKeys](b, shortKeysFixture) |
| 229 |
} |
| 230 |
|
| 231 |
func Benchmark_Decode_ShortKeys_Unmarshal_SonicFastestValidating(b *testing.B) { |
| 232 |
pretouchSonic() |
| 233 |
benchDecodeSonicFastestValidating[[]ShortKeys](b, shortKeysFixture) |
| 234 |
} |
| 235 |
|
| 236 |
func Benchmark_Decode_MediumKeys_Unmarshal_GoJsonUnmarshalOfNoCopyString(b *testing.B) { |
| 237 |
benchDecodeNoCopy[[]MediumKeys](b, mediumKeysFixture) |
| 238 |
} |
| 239 |
|
| 240 |
func Benchmark_Decode_MediumKeys_Unmarshal_SonicFastest(b *testing.B) { |
| 241 |
pretouchSonic() |
| 242 |
benchDecodeSonicFastest[[]MediumKeys](b, mediumKeysFixture) |
| 243 |
} |
| 244 |
|
| 245 |
func Benchmark_Decode_MediumKeys_Unmarshal_SonicFastestValidating(b *testing.B) { |
| 246 |
pretouchSonic() |
| 247 |
benchDecodeSonicFastestValidating[[]MediumKeys](b, mediumKeysFixture) |
| 248 |
} |
| 249 |
|
| 250 |
func Benchmark_Decode_LongKeys_Unmarshal_GoJsonUnmarshalOfNoCopyString(b *testing.B) { |
| 251 |
benchDecodeNoCopy[[]LongKeys](b, longKeysFixture) |
| 252 |
} |
| 253 |
|
| 254 |
func Benchmark_Decode_LongKeys_Unmarshal_SonicFastest(b *testing.B) { |
| 255 |
pretouchSonic() |
| 256 |
benchDecodeSonicFastest[[]LongKeys](b, longKeysFixture) |
| 257 |
} |
| 258 |
|
| 259 |
func Benchmark_Decode_LongKeys_Unmarshal_SonicFastestValidating(b *testing.B) { |
| 260 |
pretouchSonic() |
| 261 |
benchDecodeSonicFastestValidating[[]LongKeys](b, longKeysFixture) |
| 262 |
} |
| 263 |
|
| 264 |
func Benchmark_Decode_NonASCIIKeys_Unmarshal_GoJsonUnmarshalOfNoCopyString(b *testing.B) { |
| 265 |
benchDecodeNoCopy[[]NonASCIIKeys](b, nonASCIIKeysFixture) |
| 266 |
} |
| 267 |
|
| 268 |
func Benchmark_Decode_NonASCIIKeys_Unmarshal_SonicFastest(b *testing.B) { |
| 269 |
pretouchSonic() |
| 270 |
benchDecodeSonicFastest[[]NonASCIIKeys](b, nonASCIIKeysFixture) |
| 271 |
} |
| 272 |
|
| 273 |
func Benchmark_Decode_NonASCIIKeys_Unmarshal_SonicFastestValidating(b *testing.B) { |
| 274 |
pretouchSonic() |
| 275 |
benchDecodeSonicFastestValidating[[]NonASCIIKeys](b, nonASCIIKeysFixture) |
| 276 |
} |
| 277 |
|
| 278 |
func Benchmark_Decode_UnknownNonASCIIKeys_Unmarshal_GoJsonUnmarshalOfNoCopyString(b *testing.B) { |
| 279 |
benchDecodeNoCopy[[]ShortKeys](b, nonASCIIKeysFixture) |
| 280 |
} |
| 281 |
|
| 282 |
func Benchmark_Decode_UnknownNonASCIIKeys_Unmarshal_SonicFastest(b *testing.B) { |
| 283 |
pretouchSonic() |
| 284 |
benchDecodeSonicFastest[[]ShortKeys](b, nonASCIIKeysFixture) |
| 285 |
} |
| 286 |
|
| 287 |
func Benchmark_Decode_UnknownNonASCIIKeys_Unmarshal_SonicFastestValidating(b *testing.B) { |
| 288 |
pretouchSonic() |
| 289 |
benchDecodeSonicFastestValidating[[]ShortKeys](b, nonASCIIKeysFixture) |
| 290 |
} |