refactor: improve premium squads feature performance, add benchmarks
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import asyncio
|
||||
import unittest
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import patch
|
||||
|
||||
from bot.services import panel_api_service
|
||||
from bot.utils import config_link
|
||||
from bot.utils.config_link import prepare_config_links
|
||||
from bot.utils.ttl_cache import AsyncTTLCache
|
||||
|
||||
|
||||
class AsyncTTLCacheSingleflightTests(unittest.IsolatedAsyncioTestCase):
|
||||
async def test_redis_backed_cache_uses_local_singleflight_on_cold_miss(self):
|
||||
settings = SimpleNamespace(REDIS_URL="redis://example", REDIS_KEY_PREFIX="test")
|
||||
cache = AsyncTTLCache(ttl_seconds=60, settings=settings, namespace="bench")
|
||||
loader_calls = 0
|
||||
set_calls = 0
|
||||
|
||||
async def loader():
|
||||
nonlocal loader_calls
|
||||
loader_calls += 1
|
||||
await asyncio.sleep(0.001)
|
||||
return {"ok": True}
|
||||
|
||||
async def fake_get(settings, key):
|
||||
return None
|
||||
|
||||
async def fake_set(settings, key, value, ttl):
|
||||
nonlocal set_calls
|
||||
set_calls += 1
|
||||
|
||||
with (
|
||||
patch("bot.infra.redis.cache_get_json", new=fake_get),
|
||||
patch("bot.infra.redis.cache_set_json", new=fake_set),
|
||||
):
|
||||
values = await asyncio.gather(*(cache.get_or_load("same", loader) for _ in range(100)))
|
||||
|
||||
self.assertEqual(values, [{"ok": True}] * 100)
|
||||
self.assertEqual(loader_calls, 1)
|
||||
self.assertEqual(set_calls, 1)
|
||||
|
||||
|
||||
class Crypt4LinkCacheTests(unittest.IsolatedAsyncioTestCase):
|
||||
async def asyncTearDown(self):
|
||||
config_link._CRYPT4_LINK_CACHES.clear()
|
||||
|
||||
async def test_prepare_config_links_singleflights_same_crypt4_link(self):
|
||||
config_link._CRYPT4_LINK_CACHES.clear()
|
||||
settings = SimpleNamespace(
|
||||
CRYPT4_ENABLED=True,
|
||||
CRYPT4_REDIRECT_URL="",
|
||||
CRYPT4_LINK_CACHE_TTL_SECONDS=3600,
|
||||
PANEL_API_URL="https://panel.example.test/api",
|
||||
PANEL_API_KEY="key",
|
||||
USER_HWID_DEVICE_LIMIT=None,
|
||||
)
|
||||
encrypt_calls = 0
|
||||
|
||||
async def fake_encrypt(self, raw_link):
|
||||
nonlocal encrypt_calls
|
||||
encrypt_calls += 1
|
||||
await asyncio.sleep(0.001)
|
||||
return "happ://crypt4/encrypted"
|
||||
|
||||
async def fake_close(self):
|
||||
return None
|
||||
|
||||
with (
|
||||
patch.object(panel_api_service.PanelApiService, "encrypt_happ_link", fake_encrypt),
|
||||
patch.object(panel_api_service.PanelApiService, "close_session", fake_close),
|
||||
):
|
||||
values = await asyncio.gather(
|
||||
*(
|
||||
prepare_config_links(settings, "https://panel.example.test/sub/user")
|
||||
for _ in range(100)
|
||||
)
|
||||
)
|
||||
|
||||
self.assertEqual(values, [("happ://crypt4/encrypted", "happ://crypt4/encrypted")] * 100)
|
||||
self.assertEqual(encrypt_calls, 1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -531,3 +531,89 @@ class TariffWorkerTests(unittest.IsolatedAsyncioTestCase):
|
||||
# 4 GB used vs 1 GB baseline + 10 GB bonus = 11 GB limit → not limited.
|
||||
self.assertFalse(sub.premium_is_limited)
|
||||
self.assertEqual(int(sub.premium_used_bytes), 4 * (1024**3))
|
||||
|
||||
async def test_premium_usage_lookup_sums_uuid_and_username_without_double_counting(self):
|
||||
panel_service = AsyncMock(spec=PanelApiService)
|
||||
panel_service.get_node_users_bandwidth_stats = AsyncMock(
|
||||
return_value={
|
||||
"topUsers": [
|
||||
{"user": {"uuid": "u-1", "username": "alice"}, "total": 10},
|
||||
{"username": "alice", "total": 5},
|
||||
{"userUuid": "u-1", "total": 7},
|
||||
{"user": {"uuid": "other", "username": "alice"}, "total": 3},
|
||||
]
|
||||
}
|
||||
)
|
||||
worker = TariffTrafficWorker(
|
||||
settings=SimpleNamespace(),
|
||||
session_factory=SimpleNamespace(),
|
||||
panel_service=panel_service,
|
||||
subscription_service=SimpleNamespace(),
|
||||
)
|
||||
|
||||
total = await worker._premium_usage_for_user(
|
||||
"u-1",
|
||||
["node-1"],
|
||||
"2026-05-01",
|
||||
"2026-05-20",
|
||||
panel_username="alice",
|
||||
)
|
||||
total_again = await worker._premium_usage_for_user(
|
||||
"u-1",
|
||||
["node-1"],
|
||||
"2026-05-01",
|
||||
"2026-05-20",
|
||||
panel_username="alice",
|
||||
)
|
||||
|
||||
# The first row has both uuid and username, so it should be counted once.
|
||||
self.assertEqual(total, 25)
|
||||
self.assertEqual(total_again, 25)
|
||||
panel_service.get_node_users_bandwidth_stats.assert_awaited_once()
|
||||
|
||||
async def test_bulk_panel_prefetch_maps_panel_users_by_uuid_above_threshold(self):
|
||||
settings = SimpleNamespace(TARIFF_WORKER_BULK_PANEL_FETCH_THRESHOLD=2)
|
||||
panel_service = AsyncMock(spec=PanelApiService)
|
||||
panel_service.get_all_panel_users = AsyncMock(
|
||||
return_value=[
|
||||
{"uuid": "panel-1", "username": "one"},
|
||||
{"uuid": "panel-2", "username": "two"},
|
||||
{"username": "missing-uuid"},
|
||||
]
|
||||
)
|
||||
worker = TariffTrafficWorker(
|
||||
settings=settings,
|
||||
session_factory=SimpleNamespace(),
|
||||
panel_service=panel_service,
|
||||
subscription_service=SimpleNamespace(),
|
||||
)
|
||||
|
||||
result = await worker._prefetch_panel_users_by_uuid(
|
||||
[
|
||||
SimpleNamespace(panel_user_uuid="panel-1"),
|
||||
SimpleNamespace(panel_user_uuid="panel-2"),
|
||||
]
|
||||
)
|
||||
|
||||
self.assertEqual(set(result), {"panel-1", "panel-2"})
|
||||
panel_service.get_all_panel_users.assert_awaited_once_with(log_responses=False)
|
||||
|
||||
async def test_bulk_panel_prefetch_skips_below_threshold(self):
|
||||
settings = SimpleNamespace(TARIFF_WORKER_BULK_PANEL_FETCH_THRESHOLD=3)
|
||||
panel_service = AsyncMock(spec=PanelApiService)
|
||||
worker = TariffTrafficWorker(
|
||||
settings=settings,
|
||||
session_factory=SimpleNamespace(),
|
||||
panel_service=panel_service,
|
||||
subscription_service=SimpleNamespace(),
|
||||
)
|
||||
|
||||
result = await worker._prefetch_panel_users_by_uuid(
|
||||
[
|
||||
SimpleNamespace(panel_user_uuid="panel-1"),
|
||||
SimpleNamespace(panel_user_uuid="panel-2"),
|
||||
]
|
||||
)
|
||||
|
||||
self.assertIsNone(result)
|
||||
panel_service.get_all_panel_users.assert_not_awaited()
|
||||
|
||||
Reference in New Issue
Block a user