|
| 1 | +import pytest |
| 2 | + |
| 3 | +from hat import aio |
| 4 | + |
| 5 | +from hat.event import common |
| 6 | +import hat.event.backends.dummy |
| 7 | +import hat.event.server.engine |
| 8 | + |
| 9 | + |
| 10 | +pytestmark = pytest.mark.perf |
| 11 | + |
| 12 | + |
| 13 | +class Module(common.Module): |
| 14 | + |
| 15 | + def __init__(self, conf, engine, source): |
| 16 | + self._async_group = aio.Group() |
| 17 | + self._subscription = common.create_subscription( |
| 18 | + tuple(i) for i in conf['subscriptions']) |
| 19 | + self._recursion_count = conf['recursion_count'] |
| 20 | + self._counter = 0 |
| 21 | + |
| 22 | + @property |
| 23 | + def async_group(self): |
| 24 | + return self._async_group |
| 25 | + |
| 26 | + @property |
| 27 | + def subscription(self): |
| 28 | + return self._subscription |
| 29 | + |
| 30 | + def on_session_start(self, session_id): |
| 31 | + self._counter = self._recursion_count |
| 32 | + |
| 33 | + def process(self, source, event): |
| 34 | + if self._counter < 1: |
| 35 | + return |
| 36 | + |
| 37 | + self._counter -= 1 |
| 38 | + yield common.RegisterEvent(type=event.type, |
| 39 | + source_timestamp=event.source_timestamp, |
| 40 | + payload=event.payload) |
| 41 | + |
| 42 | + |
| 43 | +info = common.ModuleInfo(Module) |
| 44 | + |
| 45 | + |
| 46 | +class EventerServer(aio.Resource): |
| 47 | + |
| 48 | + def __init__(self): |
| 49 | + self._async_group = aio.Group() |
| 50 | + |
| 51 | + @property |
| 52 | + def async_group(self): |
| 53 | + return self._async_group |
| 54 | + |
| 55 | + def get_client_names(self): |
| 56 | + raise NotImplementedError() |
| 57 | + |
| 58 | + async def set_status(self, status, engine): |
| 59 | + raise NotImplementedError() |
| 60 | + |
| 61 | + async def notify_events(self, events, persisted, with_ack): |
| 62 | + raise NotImplementedError() |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.parametrize("recursion_count", [0, 1, 5, 10]) |
| 66 | +@pytest.mark.parametrize("module_count", [1, 10, 100, 1000]) |
| 67 | +async def test_register(duration, recursion_count, module_count): |
| 68 | + event_types = [('a', 'b', str(i)) |
| 69 | + for i in range(module_count)] |
| 70 | + |
| 71 | + module_confs = [{'module': __name__, |
| 72 | + 'subscriptions': [list(event_type)], |
| 73 | + 'recursion_count': recursion_count} |
| 74 | + for event_type in event_types] |
| 75 | + |
| 76 | + backend = hat.event.backends.dummy.DummyBackend(None, None, None) |
| 77 | + eventer_server = EventerServer() |
| 78 | + server_id = 1 |
| 79 | + |
| 80 | + engine = await hat.event.server.engine.create_engine( |
| 81 | + backend=backend, |
| 82 | + eventer_server=eventer_server, |
| 83 | + module_confs=module_confs, |
| 84 | + server_id=server_id, |
| 85 | + restart_cb=lambda: None, |
| 86 | + reset_monitor_ready_cb=lambda: None) |
| 87 | + |
| 88 | + description = (f'recursion_count: {recursion_count}; ' |
| 89 | + f'module_count: {module_count}') |
| 90 | + |
| 91 | + source = common.Source(type=common.SourceType.EVENTER, |
| 92 | + id=1) |
| 93 | + events = [common.RegisterEvent(type=event_type, |
| 94 | + source_timestamp=None, |
| 95 | + payload=None) |
| 96 | + for event_type in event_types] |
| 97 | + |
| 98 | + with duration(description): |
| 99 | + await engine.register(source=source, |
| 100 | + events=events) |
| 101 | + |
| 102 | + await engine.async_close() |
| 103 | + await eventer_server.async_close() |
| 104 | + await backend.async_close() |
0 commit comments