Add benchmarks (#148)

Add asv based benchmarks to guide future optimisation work.
This commit is contained in:
Adrian Garcia Badaracco
2020-09-19 09:28:16 -05:00
committed by GitHub
parent 0028cc384a
commit a3f5f21738
6 changed files with 335 additions and 92 deletions

1
benchmarks/__init__.py Normal file
View File

@@ -0,0 +1 @@

63
benchmarks/benchmarks.py Normal file
View File

@@ -0,0 +1,63 @@
import betterproto
from dataclasses import dataclass
@dataclass
class TestMessage(betterproto.Message):
foo: int = betterproto.uint32_field(0)
bar: str = betterproto.string_field(1)
baz: float = betterproto.float_field(2)
class BenchMessage:
"""Test creation and usage a proto message.
"""
def setup(self):
self.cls = TestMessage
self.instance = TestMessage()
self.instance_filled = TestMessage(0, "test", 0.0)
def time_overhead(self):
"""Overhead in class definition.
"""
@dataclass
class Message(betterproto.Message):
foo: int = betterproto.uint32_field(0)
bar: str = betterproto.string_field(1)
baz: float = betterproto.float_field(2)
def time_instantiation(self):
"""Time instantiation
"""
self.cls()
def time_attribute_access(self):
"""Time to access an attribute
"""
self.instance.foo
self.instance.bar
self.instance.baz
def time_init_with_values(self):
"""Time to set an attribute
"""
self.cls(0, "test", 0.0)
def time_attribute_setting(self):
"""Time to set attributes
"""
self.instance.foo = 0
self.instance.bar = "test"
self.instance.baz = 0.0
def time_serialize(self):
"""Time serializing a message to wire."""
bytes(self.instance_filled)
class MemSuite:
def setup(self):
self.cls = TestMessage
def mem_instance(self):
return self.cls()