It runs once per datagram, for OBP and local traffic alike, and built a tuple of every subscription to answer a yes/no question. Profiling a production server at 198 datagrams/s put 67.8% of all CPU work inside it. legs_in_table already reads the _by_table index and is on the port. Both were added in the same commit as the scan, which never used them. Constant time now instead of growing with the mesh: 6.8us to 0.30us at 120 subscriptions, 26.2us to 0.29us at 2400. On the server, 67.8% of work down to 0.84% and 42% less CPU at equal load.pull/101/head
parent
5f78545809
commit
91ff6128d9
@ -0,0 +1,99 @@
|
||||
# ADN DMR Peer Server - tests application store_has_table indexed
|
||||
#
|
||||
# Copyright (C) 2026 Rodrigo Pérez, CE5RPY <ce5rpy@qmd.cl>
|
||||
#
|
||||
###############################################################################
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software Foundation,
|
||||
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
###############################################################################
|
||||
|
||||
"""``store_has_table`` answers from the index, not from a scan.
|
||||
|
||||
It runs once per datagram. Scanning every subscription to answer a yes/no
|
||||
question was 68% of the server's CPU at 198 datagrams/s. The index it now reads
|
||||
is maintained by upsert/remove, so these check it cannot answer from a stale one.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from adn_server.application.subscription.store_sync import replace_store_from_routing_table
|
||||
from adn_server.application.subscription.subscription_queries import store_has_table
|
||||
from adn_server.domain import bytes_3
|
||||
from adn_server.infrastructure.subscription_store import InMemorySubscriptionStore
|
||||
|
||||
|
||||
def _row(system: str, tgid: int, ts: int = 1) -> dict:
|
||||
tg_b = bytes_3(tgid)
|
||||
return {
|
||||
"SYSTEM": system, "TS": ts, "TGID": tg_b, "ACTIVE": True, "TIMEOUT": 3600.0,
|
||||
"TO_TYPE": "ON", "ON": [tg_b], "OFF": [], "RESET": [], "TIMER": 0.0,
|
||||
}
|
||||
|
||||
|
||||
def _store(*tgids: int) -> InMemorySubscriptionStore:
|
||||
store = InMemorySubscriptionStore()
|
||||
replace_store_from_routing_table(
|
||||
store, {str(t): [_row("OBP-A", t), _row("OBP-B", t)] for t in tgids}
|
||||
)
|
||||
return store
|
||||
|
||||
|
||||
def _by_scan(store: InMemorySubscriptionStore, table_key: str) -> bool:
|
||||
"""What store_has_table did before it used the index."""
|
||||
return any(sub.table_key() == table_key for sub in store.snapshot())
|
||||
|
||||
|
||||
def test_it_answers_what_a_full_scan_answers() -> None:
|
||||
store = _store(7305, 52090, 730444)
|
||||
for key in ("7305", "52090", "730444", "9999", "", "0"):
|
||||
assert store_has_table(store, key) == _by_scan(store, key), key
|
||||
|
||||
|
||||
def test_removing_the_last_leg_empties_the_table() -> None:
|
||||
store = _store(7305, 52090)
|
||||
assert store_has_table(store, "7305")
|
||||
|
||||
for sub in store.legs_in_table("7305"):
|
||||
store.remove(sub.subscription_id)
|
||||
|
||||
assert store_has_table(store, "7305") is False
|
||||
assert store_has_table(store, "7305") == _by_scan(store, "7305")
|
||||
assert store_has_table(store, "52090") is True
|
||||
|
||||
|
||||
def test_removing_one_of_two_legs_keeps_the_table() -> None:
|
||||
store = _store(7305)
|
||||
store.remove(store.legs_in_table("7305")[0].subscription_id)
|
||||
|
||||
assert store_has_table(store, "7305") is True
|
||||
assert store_has_table(store, "7305") == _by_scan(store, "7305")
|
||||
|
||||
|
||||
def test_a_replaced_store_forgets_the_old_tables() -> None:
|
||||
store = _store(7305)
|
||||
replace_store_from_routing_table(store, {"52090": [_row("OBP-A", 52090)]})
|
||||
|
||||
assert store_has_table(store, "7305") is False
|
||||
assert store_has_table(store, "52090") is True
|
||||
for key in ("7305", "52090"):
|
||||
assert store_has_table(store, key) == _by_scan(store, key), key
|
||||
|
||||
|
||||
def test_it_does_not_get_slower_as_the_store_grows() -> None:
|
||||
"""The scan was O(n) per datagram; the index must not be."""
|
||||
small = _store(*range(1000, 1010))
|
||||
large = _store(*range(1000, 1400))
|
||||
assert len(large.snapshot()) > 20 * len(small.snapshot())
|
||||
assert store_has_table(small, "1005") == store_has_table(large, "1005")
|
||||
assert len(large.legs_in_table("1005")) == 2
|
||||
Loading…
Reference in new issue