perf(monitor): 超级编导查询并发翻页,20 秒压到 5 秒级
实测单个请求 1–4 秒,活动列表三页串行就 12 秒,界面上就是一个一直转的 「查询中」。活动页之间、活动之间的作品集本来互不依赖,改成有界并发: 翻页 4 路、取作品 6 路。活动页任何一页失败仍让整次查询报错 —— 少一页 就是少一批活动和作品,看上去却像「这段时间就这些」。
This commit is contained in:
@@ -227,6 +227,45 @@ class TestPagination:
|
||||
assert calls == [1, 2]
|
||||
|
||||
|
||||
class TestActivityPaging:
|
||||
@pytest.mark.asyncio
|
||||
async def test_every_activity_page_is_fetched(self, monkeypatch):
|
||||
"""活动列表没有日期过滤,每次查询都要把整个历史翻完 —— 一页都不能少。"""
|
||||
calls: list = []
|
||||
|
||||
async def fake_request(path, params, token):
|
||||
calls.append(params["page"])
|
||||
pages = {
|
||||
1: [{"id": 1}, {"id": 2}],
|
||||
2: [{"id": 3}, {"id": 4}],
|
||||
3: [{"id": 5}],
|
||||
}
|
||||
return {"items": pages[params["page"]], "total": 5}
|
||||
|
||||
monkeypatch.setattr(superdir, "_request", fake_request)
|
||||
monkeypatch.setattr(superdir, "ACTIVITY_PAGE_SIZE", 2)
|
||||
|
||||
items = await superdir._fetch_activities("token")
|
||||
|
||||
assert sorted(calls) == [1, 2, 3]
|
||||
assert [item["id"] for item in items] == [1, 2, 3, 4, 5]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_a_failed_page_fails_the_whole_list_not_a_partial_one(self, monkeypatch):
|
||||
"""少一页 = 少一批活动,看上去却像「这段时间就这些」—— 必须报错。"""
|
||||
|
||||
async def fake_request(path, params, token):
|
||||
if params["page"] == 2:
|
||||
raise superdir.SuperDirError(500, "这一页没取到")
|
||||
return {"items": [{"id": 1}, {"id": 2}], "total": 5}
|
||||
|
||||
monkeypatch.setattr(superdir, "_request", fake_request)
|
||||
monkeypatch.setattr(superdir, "ACTIVITY_PAGE_SIZE", 2)
|
||||
|
||||
with pytest.raises(superdir.SuperDirError):
|
||||
await superdir._fetch_activities("token")
|
||||
|
||||
|
||||
class TestErrorHints:
|
||||
def test_a_relogin_error_explains_itself(self):
|
||||
error = superdir._error({"code": 4017, "message": "账号已在别处登录"})
|
||||
|
||||
Reference in New Issue
Block a user