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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
import sys
from dataclasses import dataclass
from functools import partial
from inspect import signature
from typing import Callable, ClassVar
from unittest.mock import Mock
import pytest
from psygnal import EmissionInfo, Signal, SignalGroupDescriptor, SignalInstance, evented
if all(x not in {"--codspeed", "--benchmark", "tests/test_bench.py"} for x in sys.argv):
pytest.skip("use --benchmark to run benchmark", allow_module_level=True)
CALLBACK_TYPES = [
"function",
"method",
"lambda",
"partial",
"partial_method",
"setattr",
"setitem",
"real_func",
"print",
]
# fmt: off
class Emitter:
one_int = Signal(int)
int_str = Signal(int, str)
class Obj:
x: int = 0
def __setitem__(self, key: str, value: int) -> None:
self.x = value
def no_args(self) -> None: ...
def one_int(self, x: int) -> None: ...
def int_str(self, x: int, y: str) -> None: ...
def no_args() -> None: ...
def one_int(x: int) -> None: ...
def int_str(x: int, y: str) -> None: ...
def real_func() -> None: list(range(4)) # simulate a brief thing
INT_SIG = signature(one_int)
# fmt: on
def _get_callback(callback_type: str, obj: Obj) -> Callable:
callback_types: dict[str, Callable] = {
"function": one_int,
"method": obj.one_int,
"lambda": lambda x: None,
"partial": partial(int_str, y="foo"),
"partial_method": partial(obj.int_str, y="foo"),
"real_func": real_func,
"print": print,
}
return callback_types[callback_type]
# Creation suite ------------------------------------------
def test_create_signal(benchmark: Callable) -> None:
benchmark(Signal, int)
def test_create_signal_instance(benchmark: Callable) -> None:
benchmark(SignalInstance, INT_SIG)
# Connect suite ---------------------------------------------
@pytest.mark.parametrize("check_types", ["check_types", ""])
@pytest.mark.parametrize("callback_type", CALLBACK_TYPES)
def test_connect_time(
benchmark: Callable, callback_type: str, check_types: str
) -> None:
emitter = Emitter()
obj = Obj()
kwargs = {}
if callback_type == "setattr":
func: Callable = emitter.one_int.connect_setattr
args: tuple = (obj, "x")
kwargs = {"maxargs": 1}
elif callback_type == "setitem":
func = emitter.one_int.connect_setitem
args = (obj, "x")
kwargs = {"maxargs": 1}
else:
func = emitter.one_int.connect
args = (_get_callback(callback_type, obj),)
kwargs = {"check_types": bool(check_types)}
benchmark(func, *args, **kwargs)
# Emit suite ------------------------------------------------
@pytest.mark.parametrize("n_connections", range(2, 2**6, 16))
@pytest.mark.parametrize("callback_type", CALLBACK_TYPES)
def test_emit_time(benchmark: Callable, n_connections: int, callback_type: str) -> None:
emitter = Emitter()
obj = Obj()
if callback_type == "setattr":
for _ in range(n_connections):
emitter.one_int.connect_setattr(obj, "x", maxargs=1)
elif callback_type == "setitem":
for _ in range(n_connections):
emitter.one_int.connect_setitem(obj, "x", maxargs=1)
else:
callback = _get_callback(callback_type, obj)
for _ in range(n_connections):
emitter.one_int.connect(callback, unique=False)
benchmark(emitter.one_int.emit, 1)
@pytest.mark.benchmark
def test_evented_creation() -> None:
@evented
@dataclass
class Obj:
x: int = 0
y: str = "hi"
z: bool = False
_ = Obj().events # type: ignore
def test_evented_setattr(benchmark: Callable) -> None:
@evented
@dataclass
class Obj:
x: int = 0
y: str = "hi"
z: bool = False
obj = Obj()
_ = obj.events # type: ignore
benchmark(setattr, obj, "x", 1)
def _get_dataclass(type_: str) -> type:
if type_ == "attrs":
from attrs import define
@define
class Foo:
a: int
b: str
c: bool
d: float
e: tuple[int, str]
events: ClassVar = SignalGroupDescriptor()
elif type_ == "dataclass":
@dataclass
class Foo: # type: ignore [no-redef]
a: int
b: str
c: bool
d: float
e: tuple[int, str]
events: ClassVar = SignalGroupDescriptor()
elif type_ == "msgspec":
import msgspec
class Foo(msgspec.Struct): # type: ignore [no-redef]
a: int
b: str
c: bool
d: float
e: tuple[int, str]
events: ClassVar = SignalGroupDescriptor()
elif type_ == "pydantic":
from pydantic import BaseModel
class Foo(BaseModel): # type: ignore [no-redef]
a: int
b: str
c: bool
d: float
e: tuple[int, str]
events: ClassVar = SignalGroupDescriptor()
return Foo
@pytest.mark.parametrize("type_", ["dataclass", "pydantic", "attrs", "msgspec"])
def test_dataclass_group_create(type_: str, benchmark: Callable) -> None:
if type_ == "msgspec":
pytest.importorskip("msgspec")
Foo = _get_dataclass(type_)
foo = Foo(a=1, b="hi", c=True, d=1.0, e=(1, "hi"))
benchmark(getattr, foo, "events")
@pytest.mark.parametrize("type_", ["dataclass", "pydantic", "attrs", "msgspec"])
def test_dataclass_setattr(type_: str, benchmark: Callable) -> None:
if type_ == "msgspec":
pytest.importorskip("msgspec")
Foo = _get_dataclass(type_)
foo = Foo(a=1, b="hi", c=True, d=1.0, e=(1, "hi"))
mock = Mock()
foo.events._psygnal_relay.connect(mock)
def _doit() -> None:
foo.a = 2
foo.b = "hello"
foo.c = False
foo.d = 2.0
foo.e = (2, "hello")
benchmark(_doit)
for emitted, attr in zip(
[(2, 1), ("hello", "hi"), (False, True), (2.0, 1.0), ((2, "hello"), (1, "hi"))],
"abcde",
):
mock.assert_any_call(EmissionInfo(getattr(foo.events, attr), emitted))
assert getattr(foo, attr) == emitted[0]
|