File: lazy_bench_test.go

package info (click to toggle)
golang-google-protobuf 1.36.7-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 14,996 kB
  • sloc: sh: 94; makefile: 4
file content (92 lines) | stat: -rw-r--r-- 2,718 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package proto_test

import (
	"testing"

	"google.golang.org/protobuf/proto"

	lazyopaquepb "google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque"
)

// testMessageLinked returns a test message with a few fields of various
// possible types filled in that nests more messages like a linked list.
func testMessageLinked(nesting int) *lazyopaquepb.Node {
	const (
		shortVarint = 23              // encodes into 1 byte
		longVarint  = 562949953421312 // encodes into 8 bytes
	)
	msg := lazyopaquepb.Node_builder{
		Int32:    proto.Int32(shortVarint),
		Int64:    proto.Int64(longVarint),
		Uint32:   proto.Uint32(shortVarint),
		Uint64:   proto.Uint64(longVarint),
		Sint32:   proto.Int32(shortVarint),
		Sint64:   proto.Int64(longVarint),
		Fixed32:  proto.Uint32(shortVarint),
		Fixed64:  proto.Uint64(longVarint),
		Sfixed32: proto.Int32(shortVarint),
		Sfixed64: proto.Int64(longVarint),
		Float:    proto.Float32(23.42),
		Double:   proto.Float64(23.42),
		Bool:     proto.Bool(true),
		String:   proto.String("hello"),
		Bytes:    []byte("world"),
	}.Build()
	if nesting > 0 {
		msg.SetNested(testMessageLinked(nesting - 1))
	}
	return msg
}

// A higher nesting level than 15 messages deep does not result in (relative)
// performance changes. In other words, the full effect of lazy decoding is
// visible with a nesting level of 15 messages deep. Lower nesting levels (like
// 5 messages deep) also show significant improvement.
const nesting = 15

func BenchmarkUnmarshal(b *testing.B) {
	encoded, err := proto.Marshal(testMessageLinked(nesting))
	if err != nil {
		b.Fatal(err)
	}

	for _, tt := range []struct {
		desc  string
		uopts proto.UnmarshalOptions
	}{
		{
			desc:  "lazy",
			uopts: proto.UnmarshalOptions{},
		},

		// When running the benchmark directly, print lazy vs. nolazy in the
		// same run. When using the benchstat tool, you can compare lazy
		// vs. nolazy by running only the lazy variant and disabling lazy
		// decoding with the -test_lazy_unmarshal command-line flag:
		//
		// benchstat \
		//   nolazy=<(go test -run=^$ -bench=Unmarshal/^lazy -count=6) \
		//   lazy=<(go test -run=^$ -bench=Unmarshal/^lazy -count=6 -test_lazy_unmarshal)
		{
			desc: "nolazy",
			uopts: proto.UnmarshalOptions{
				NoLazyDecoding: true,
			},
		},
	} {
		b.Run(tt.desc, func(b *testing.B) {
			b.ResetTimer()
			b.ReportAllocs()
			for i := 0; i < b.N; i++ {
				out := &lazyopaquepb.Node{}
				if err := tt.uopts.Unmarshal(encoded, out); err != nil {
					b.Fatalf("can't unmarshal message: %v", err)
				}
			}
		})
	}
}