Skip to content

Commit ad8b917

Browse files
Add benchmarking cases for nested, repeat and deserialize (#241)
1 parent a331265 commit ad8b917

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

benchmarks/benchmarks.py

+69
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import betterproto
22
from dataclasses import dataclass
33

4+
from typing import List
5+
46

57
@dataclass
68
class TestMessage(betterproto.Message):
@@ -9,13 +11,60 @@ class TestMessage(betterproto.Message):
911
baz: float = betterproto.float_field(2)
1012

1113

14+
@dataclass
15+
class TestNestedChildMessage(betterproto.Message):
16+
str_key: str = betterproto.string_field(0)
17+
bytes_key: bytes = betterproto.bytes_field(1)
18+
bool_key: bool = betterproto.bool_field(2)
19+
float_key: float = betterproto.float_field(3)
20+
int_key: int = betterproto.uint64_field(4)
21+
22+
23+
@dataclass
24+
class TestNestedMessage(betterproto.Message):
25+
foo: TestNestedChildMessage = betterproto.message_field(0)
26+
bar: TestNestedChildMessage = betterproto.message_field(1)
27+
baz: TestNestedChildMessage = betterproto.message_field(2)
28+
29+
30+
@dataclass
31+
class TestRepeatedMessage(betterproto.Message):
32+
foo_repeat: List[str] = betterproto.string_field(0)
33+
bar_repeat: List[int] = betterproto.int64_field(1)
34+
baz_repeat: List[bool] = betterproto.bool_field(2)
35+
36+
1237
class BenchMessage:
1338
"""Test creation and usage a proto message."""
1439

1540
def setup(self):
1641
self.cls = TestMessage
1742
self.instance = TestMessage()
1843
self.instance_filled = TestMessage(0, "test", 0.0)
44+
self.instance_filled_bytes = bytes(self.instance_filled)
45+
self.instance_filled_nested = TestNestedMessage(
46+
TestNestedChildMessage("foo", bytearray(b"test1"), True, 0.1234, 500),
47+
TestNestedChildMessage("bar", bytearray(b"test2"), True, 3.1415, -302),
48+
TestNestedChildMessage("baz", bytearray(b"test3"), False, 1e5, 300),
49+
)
50+
self.instance_filled_nested_bytes = bytes(self.instance_filled_nested)
51+
self.instance_filled_repeated = TestRepeatedMessage(
52+
[
53+
"test1",
54+
"test2",
55+
"test3",
56+
"test4",
57+
"test5",
58+
"test6",
59+
"test7",
60+
"test8",
61+
"test9",
62+
"test10",
63+
],
64+
[2, -100, 0, 500000, 600, -425678, 1000000000, -300, 1, -694214214466],
65+
[True, False, False, False, True, True, False, True, False, False],
66+
)
67+
self.instance_filled_repeated_bytes = bytes(self.instance_filled_repeated)
1968

2069
def time_overhead(self):
2170
"""Overhead in class definition."""
@@ -50,6 +99,26 @@ def time_serialize(self):
5099
"""Time serializing a message to wire."""
51100
bytes(self.instance_filled)
52101

102+
def time_deserialize(self):
103+
"""Time deserialize a message."""
104+
TestMessage().parse(self.instance_filled_bytes)
105+
106+
def time_serialize_nested(self):
107+
"""Time serializing a nested message to wire."""
108+
bytes(self.instance_filled_nested)
109+
110+
def time_deserialize_nested(self):
111+
"""Time deserialize a nested message."""
112+
TestNestedMessage().parse(self.instance_filled_nested_bytes)
113+
114+
def time_serialize_repeated(self):
115+
"""Time serializing a repeated message to wire."""
116+
bytes(self.instance_filled_repeated)
117+
118+
def time_deserialize_repeated(self):
119+
"""Time deserialize a repeated message."""
120+
TestRepeatedMessage().parse(self.instance_filled_repeated_bytes)
121+
53122

54123
class MemSuite:
55124
def setup(self):

0 commit comments

Comments
 (0)