1ffe3c632Sopenharmony_cipackage main
2ffe3c632Sopenharmony_ci
3ffe3c632Sopenharmony_ciimport (
4ffe3c632Sopenharmony_ci	benchmarkWrapper "../tmp"
5ffe3c632Sopenharmony_ci	googleMessage1Proto2 "../tmp/datasets/google_message1/proto2"
6ffe3c632Sopenharmony_ci	googleMessage1Proto3 "../tmp/datasets/google_message1/proto3"
7ffe3c632Sopenharmony_ci	googleMessage2 "../tmp/datasets/google_message2"
8ffe3c632Sopenharmony_ci	googleMessage3 "../tmp/datasets/google_message3"
9ffe3c632Sopenharmony_ci	googleMessage4 "../tmp/datasets/google_message4"
10ffe3c632Sopenharmony_ci	"flag"
11ffe3c632Sopenharmony_ci	"github.com/golang/protobuf/proto"
12ffe3c632Sopenharmony_ci	"io/ioutil"
13ffe3c632Sopenharmony_ci	"testing"
14ffe3c632Sopenharmony_ci)
15ffe3c632Sopenharmony_ci
16ffe3c632Sopenharmony_ci// Data is returned by the Load function.
17ffe3c632Sopenharmony_citype Dataset struct {
18ffe3c632Sopenharmony_ci	name        string
19ffe3c632Sopenharmony_ci	newMessage  func() proto.Message
20ffe3c632Sopenharmony_ci	marshaled   [][]byte
21ffe3c632Sopenharmony_ci	unmarshaled []proto.Message
22ffe3c632Sopenharmony_ci}
23ffe3c632Sopenharmony_ci
24ffe3c632Sopenharmony_civar datasets []Dataset
25ffe3c632Sopenharmony_ci
26ffe3c632Sopenharmony_ci// This is used to getDefaultInstance for a message type.
27ffe3c632Sopenharmony_cifunc generateNewMessageFunction(dataset benchmarkWrapper.BenchmarkDataset) func() proto.Message {
28ffe3c632Sopenharmony_ci	switch dataset.MessageName {
29ffe3c632Sopenharmony_ci	case "benchmarks.proto3.GoogleMessage1":
30ffe3c632Sopenharmony_ci		return func() proto.Message { return new(googleMessage1Proto3.GoogleMessage1) }
31ffe3c632Sopenharmony_ci	case "benchmarks.proto2.GoogleMessage1":
32ffe3c632Sopenharmony_ci		return func() proto.Message { return new(googleMessage1Proto2.GoogleMessage1) }
33ffe3c632Sopenharmony_ci	case "benchmarks.proto2.GoogleMessage2":
34ffe3c632Sopenharmony_ci		return func() proto.Message { return new(googleMessage2.GoogleMessage2) }
35ffe3c632Sopenharmony_ci	case "benchmarks.google_message3.GoogleMessage3":
36ffe3c632Sopenharmony_ci		return func() proto.Message { return new(googleMessage3.GoogleMessage3) }
37ffe3c632Sopenharmony_ci	case "benchmarks.google_message4.GoogleMessage4":
38ffe3c632Sopenharmony_ci		return func() proto.Message { return new(googleMessage4.GoogleMessage4) }
39ffe3c632Sopenharmony_ci	default:
40ffe3c632Sopenharmony_ci		panic("Unknown message type: " + dataset.MessageName)
41ffe3c632Sopenharmony_ci	}
42ffe3c632Sopenharmony_ci}
43ffe3c632Sopenharmony_ci
44ffe3c632Sopenharmony_cifunc init() {
45ffe3c632Sopenharmony_ci	flag.Parse()
46ffe3c632Sopenharmony_ci	for _, f := range flag.Args() {
47ffe3c632Sopenharmony_ci		// Load the benchmark.
48ffe3c632Sopenharmony_ci		b, err := ioutil.ReadFile(f)
49ffe3c632Sopenharmony_ci		if err != nil {
50ffe3c632Sopenharmony_ci			panic(err)
51ffe3c632Sopenharmony_ci		}
52ffe3c632Sopenharmony_ci
53ffe3c632Sopenharmony_ci		// Parse the benchmark.
54ffe3c632Sopenharmony_ci		var dm benchmarkWrapper.BenchmarkDataset
55ffe3c632Sopenharmony_ci		if err := proto.Unmarshal(b, &dm); err != nil {
56ffe3c632Sopenharmony_ci			panic(err)
57ffe3c632Sopenharmony_ci		}
58ffe3c632Sopenharmony_ci
59ffe3c632Sopenharmony_ci		// Determine the concrete protobuf message type to use.
60ffe3c632Sopenharmony_ci		var ds Dataset
61ffe3c632Sopenharmony_ci		ds.newMessage = generateNewMessageFunction(dm)
62ffe3c632Sopenharmony_ci
63ffe3c632Sopenharmony_ci		// Unmarshal each test message.
64ffe3c632Sopenharmony_ci		for _, payload := range dm.Payload {
65ffe3c632Sopenharmony_ci			ds.marshaled = append(ds.marshaled, payload)
66ffe3c632Sopenharmony_ci			m := ds.newMessage()
67ffe3c632Sopenharmony_ci			if err := proto.Unmarshal(payload, m); err != nil {
68ffe3c632Sopenharmony_ci				panic(err)
69ffe3c632Sopenharmony_ci			}
70ffe3c632Sopenharmony_ci			ds.unmarshaled = append(ds.unmarshaled, m)
71ffe3c632Sopenharmony_ci		}
72ffe3c632Sopenharmony_ci		ds.name = f
73ffe3c632Sopenharmony_ci
74ffe3c632Sopenharmony_ci		datasets = append(datasets, ds)
75ffe3c632Sopenharmony_ci	}
76ffe3c632Sopenharmony_ci}
77ffe3c632Sopenharmony_ci
78ffe3c632Sopenharmony_cifunc Benchmark(b *testing.B) {
79ffe3c632Sopenharmony_ci	for _, ds := range datasets {
80ffe3c632Sopenharmony_ci		b.Run(ds.name, func(b *testing.B) {
81ffe3c632Sopenharmony_ci			b.Run("Unmarshal", func(b *testing.B) {
82ffe3c632Sopenharmony_ci				for i := 0; i < b.N; i++ {
83ffe3c632Sopenharmony_ci					for j, payload := range ds.marshaled {
84ffe3c632Sopenharmony_ci						out := ds.newMessage()
85ffe3c632Sopenharmony_ci						if err := proto.Unmarshal(payload, out); err != nil {
86ffe3c632Sopenharmony_ci							b.Fatalf("can't unmarshal message %d %v", j, err)
87ffe3c632Sopenharmony_ci						}
88ffe3c632Sopenharmony_ci					}
89ffe3c632Sopenharmony_ci				}
90ffe3c632Sopenharmony_ci			})
91ffe3c632Sopenharmony_ci			b.Run("Marshal", func(b *testing.B) {
92ffe3c632Sopenharmony_ci				for i := 0; i < b.N; i++ {
93ffe3c632Sopenharmony_ci					for j, m := range ds.unmarshaled {
94ffe3c632Sopenharmony_ci						if _, err := proto.Marshal(m); err != nil {
95ffe3c632Sopenharmony_ci							b.Fatalf("can't marshal message %d %+v: %v", j, m, err)
96ffe3c632Sopenharmony_ci						}
97ffe3c632Sopenharmony_ci					}
98ffe3c632Sopenharmony_ci				}
99ffe3c632Sopenharmony_ci			})
100ffe3c632Sopenharmony_ci			b.Run("Size", func(b *testing.B) {
101ffe3c632Sopenharmony_ci				for i := 0; i < b.N; i++ {
102ffe3c632Sopenharmony_ci					for _, m := range ds.unmarshaled {
103ffe3c632Sopenharmony_ci						proto.Size(m)
104ffe3c632Sopenharmony_ci					}
105ffe3c632Sopenharmony_ci				}
106ffe3c632Sopenharmony_ci			})
107ffe3c632Sopenharmony_ci			b.Run("Clone", func(b *testing.B) {
108ffe3c632Sopenharmony_ci				for i := 0; i < b.N; i++ {
109ffe3c632Sopenharmony_ci					for _, m := range ds.unmarshaled {
110ffe3c632Sopenharmony_ci						proto.Clone(m)
111ffe3c632Sopenharmony_ci					}
112ffe3c632Sopenharmony_ci				}
113ffe3c632Sopenharmony_ci			})
114ffe3c632Sopenharmony_ci			b.Run("Merge", func(b *testing.B) {
115ffe3c632Sopenharmony_ci				for i := 0; i < b.N; i++ {
116ffe3c632Sopenharmony_ci					for _, m := range ds.unmarshaled {
117ffe3c632Sopenharmony_ci						out := ds.newMessage()
118ffe3c632Sopenharmony_ci						proto.Merge(out, m)
119ffe3c632Sopenharmony_ci					}
120ffe3c632Sopenharmony_ci				}
121ffe3c632Sopenharmony_ci			})
122ffe3c632Sopenharmony_ci		})
123ffe3c632Sopenharmony_ci	}
124ffe3c632Sopenharmony_ci}
125