Skip to content

Commit 506c3cd

Browse files
committed
Refactor BenchmarkUnmarshal cases, fix ReportAllocs.
1 parent 3cb5a6a commit 506c3cd

File tree

1 file changed

+49
-43
lines changed

1 file changed

+49
-43
lines changed

bson/benchmark_test.go

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,6 @@ func readExtJSONFile(filename string) map[string]interface{} {
182182
}
183183

184184
func BenchmarkMarshal(b *testing.B) {
185-
b.ReportAllocs()
186-
187185
cases := []struct {
188186
desc string
189187
value any
@@ -232,6 +230,7 @@ func BenchmarkMarshal(b *testing.B) {
232230
tc := tc // Capture range variable.
233231

234232
b.Run(tc.desc, func(b *testing.B) {
233+
b.ReportAllocs()
235234
b.RunParallel(func(pb *testing.PB) {
236235
for pb.Next() {
237236
_, err := Marshal(tc.value)
@@ -249,6 +248,7 @@ func BenchmarkMarshal(b *testing.B) {
249248
tc := tc // Capture range variable.
250249

251250
b.Run(tc.desc, func(b *testing.B) {
251+
b.ReportAllocs()
252252
b.RunParallel(func(pb *testing.PB) {
253253
for pb.Next() {
254254
_, err := MarshalExtJSON(tc.value, true, false)
@@ -266,6 +266,7 @@ func BenchmarkMarshal(b *testing.B) {
266266
tc := tc // Capture range variable.
267267

268268
b.Run(tc.desc, func(b *testing.B) {
269+
b.ReportAllocs()
269270
b.RunParallel(func(pb *testing.PB) {
270271
for pb.Next() {
271272
_, err := json.Marshal(tc.value)
@@ -280,80 +281,81 @@ func BenchmarkMarshal(b *testing.B) {
280281
}
281282

282283
func BenchmarkUnmarshal(b *testing.B) {
283-
b.ReportAllocs()
284-
285-
cases := []struct {
284+
type testcase struct {
286285
desc string
287286
value any
288287
dst func() any
289-
}{
288+
}
289+
290+
cases := []testcase{
290291
{
291-
desc: "simple to struct",
292+
desc: "simple struct",
292293
value: encodetestInstance,
293294
dst: func() any { return &encodetest{} },
294295
},
295296
{
296-
desc: "simple to map",
297-
value: encodetestInstance,
298-
dst: func() any { return &map[string]any{} },
299-
},
300-
{
301-
desc: "simple to D",
302-
value: encodetestInstance,
303-
dst: func() any { return &D{} },
304-
},
305-
{
306-
desc: "nested to struct",
297+
desc: "nested struct",
307298
value: nestedInstance,
308299
dst: func() any { return &encodetest{} },
309300
},
301+
}
302+
303+
inputs := []struct {
304+
name string
305+
value any
306+
}{
310307
{
311-
desc: "nested to map",
312-
value: nestedInstance,
313-
dst: func() any { return &map[string]any{} },
308+
name: "simple",
309+
value: encodetestInstance,
314310
},
315311
{
316-
desc: "nested to D",
312+
name: "nested",
317313
value: nestedInstance,
318-
dst: func() any { return &D{} },
319314
},
320315
{
321-
desc: "deep_bson.json.gz to map",
316+
name: "deep_bson.json.gz",
322317
value: readExtJSONFile("deep_bson.json.gz"),
323-
dst: func() any { return &map[string]any{} },
324318
},
325319
{
326-
desc: "deep_bson.json.gz to D",
327-
value: readExtJSONFile("deep_bson.json.gz"),
328-
dst: func() any { return &D{} },
329-
},
330-
{
331-
desc: "flat_bson.json.gz to map",
320+
name: "flat_bson.json.gz",
332321
value: readExtJSONFile("flat_bson.json.gz"),
333-
dst: func() any { return &map[string]any{} },
334322
},
335323
{
336-
desc: "flat_bson.json.gz to D",
337-
value: readExtJSONFile("flat_bson.json.gz"),
338-
dst: func() any { return &D{} },
324+
name: "full_bson.json.gz",
325+
value: readExtJSONFile("full_bson.json.gz"),
339326
},
327+
}
328+
329+
destinations := []struct {
330+
name string
331+
dst func() any
332+
}{
340333
{
341-
desc: "full_bson.json.gz to map",
342-
value: readExtJSONFile("full_bson.json.gz"),
343-
dst: func() any { return &map[string]any{} },
334+
name: "to map",
335+
dst: func() any { return &map[string]any{} },
344336
},
345337
{
346-
desc: "full_bson.json.gz to D",
347-
value: readExtJSONFile("full_bson.json.gz"),
348-
dst: func() any { return &D{} },
338+
name: "to D",
339+
dst: func() any { return &D{} },
349340
},
350341
}
351342

343+
for _, input := range inputs {
344+
for _, dest := range destinations {
345+
cases = append(cases, testcase{
346+
desc: input.name + " " + dest.name,
347+
value: input.value,
348+
dst: dest.dst,
349+
})
350+
}
351+
}
352+
352353
b.Run("BSON", func(b *testing.B) {
353354
for _, tc := range cases {
354355
tc := tc // Capture range variable.
355356

356357
b.Run(tc.desc, func(b *testing.B) {
358+
b.ReportAllocs()
357359
data, err := Marshal(tc.value)
358360
if err != nil {
359361
b.Errorf("error marshalling BSON: %s", err)
@@ -380,6 +382,7 @@ func BenchmarkUnmarshal(b *testing.B) {
380382
tc := tc // Capture range variable.
381383

382384
b.Run(tc.desc, func(b *testing.B) {
385+
b.ReportAllocs()
383386
data, err := MarshalExtJSON(tc.value, true, false)
384387
if err != nil {
385388
b.Errorf("error marshalling extended JSON: %s", err)
@@ -406,6 +409,7 @@ func BenchmarkUnmarshal(b *testing.B) {
406409
tc := tc // Capture range variable.
407410

408411
b.Run(tc.desc, func(b *testing.B) {
412+
b.ReportAllocs()
409413
data, err := json.Marshal(tc.value)
410414
if err != nil {
411415
b.Errorf("error marshalling JSON: %s", err)
@@ -494,13 +498,13 @@ func codeInit() {
494498
}
495499

496500
func BenchmarkCodeUnmarshal(b *testing.B) {
497-
b.ReportAllocs()
498501
if codeJSON == nil {
499502
b.StopTimer()
500503
codeInit()
501504
b.StartTimer()
502505
}
503506
b.Run("BSON", func(b *testing.B) {
507+
b.ReportAllocs()
504508
b.RunParallel(func(pb *testing.PB) {
505509
for pb.Next() {
506510
var r codeResponse
@@ -512,6 +516,7 @@ func BenchmarkCodeUnmarshal(b *testing.B) {
512516
b.SetBytes(int64(len(codeBSON)))
513517
})
514518
b.Run("JSON", func(b *testing.B) {
519+
b.ReportAllocs()
515520
b.RunParallel(func(pb *testing.PB) {
516521
for pb.Next() {
517522
var r codeResponse
@@ -525,13 +530,13 @@ func BenchmarkCodeUnmarshal(b *testing.B) {
525530
}
526531

527532
func BenchmarkCodeMarshal(b *testing.B) {
528-
b.ReportAllocs()
529533
if codeJSON == nil {
530534
b.StopTimer()
531535
codeInit()
532536
b.StartTimer()
533537
}
534538
b.Run("BSON", func(b *testing.B) {
539+
b.ReportAllocs()
535540
b.RunParallel(func(pb *testing.PB) {
536541
for pb.Next() {
537542
if _, err := Marshal(&codeStruct); err != nil {
@@ -542,6 +547,7 @@ func BenchmarkCodeMarshal(b *testing.B) {
542547
b.SetBytes(int64(len(codeBSON)))
543548
})
544549
b.Run("JSON", func(b *testing.B) {
550+
b.ReportAllocs()
545551
b.RunParallel(func(pb *testing.PB) {
546552
for pb.Next() {
547553
if _, err := json.Marshal(&codeStruct); err != nil {

0 commit comments

Comments
 (0)