mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 09:20:22 +08:00
perf: 优化请求鉴权链路并批量化统计/调度查询
- 为 Pipeline/Context 增加按需读取请求体能力,支持 async 懒加载 JSON body - 为 chat/cli/video/claude/openai-cli 适配器关闭默认预读,减少无效 body 读取与超时风险 - 将本地登录、JWT 用户加载、API Key 鉴权迁移到线程池隔离会话执行,避免阻塞事件循环 - 为 API Key 鉴权返回结构化余额结果,并在主请求会话中重新绑定 user/api_key 后再校验状态、过期和锁定信息 - 为 management/user token 前缀认证引入独立会话与结果回绑,避免跨会话对象写入失效 - 为 Usage 余额检查补充结构化返回,统一透出 remaining 与欠费/不可用文案映射 - 为用户与管理端活跃请求查询增加 maintain_status 开关,避免轮询指定 id 时误触发状态修复 - 重写 user_me usage 汇总逻辑,支持 group_by=None 的粗粒度聚合 - 修正 provider 维度成功率与平均响应时间统计,基于 success_count 和成功响应耗时汇总计算 - 前端 Usage 轮询由 setInterval 改为串行 setTimeout,避免并发轮询叠加 - 为 StatsAggregator 增加按本地日期批量计算百分位能力,替代逐天 fan-out 查询 - 为混合统计查询合并连续实时日期区间,并批量读取 StatsDaily,减少逐日查询次数 - 为用户日统计增加批量聚合入口,替代逐用户循环聚合 - 为系统配置导出改用 selectinload 预加载 provider 关联数据,减少 N+1 查询 - 为管理员用户列表增加钱包批量查询,避免逐用户回表 - 为调度器增加 provider 轻量引用预过滤,先按 allowed_providers 缩小范围再加载完整 provider 图 - 为 CandidateBuilder 增加 provider refs/provider_ids 查询能力,保留分页顺序 - 为模型缓存增加 provider_model_mappings 索引缓存与 model_mappings 规则缓存,减少重复全量扫描 - 为请求候选中间态改为 flush/batch commit,降低 pending/streaming 状态切换的事务往返 - 为钱包访问结果补充 balance_snapshot,并抽取余额快照复用逻辑 - 补充 pipeline、auth、admin users、user_me usage、stats aggregator、model cache、 scheduler、wallet、request candidate 等回归与契约测试
This commit is contained in:
182
tests/services/test_stats_aggregator_optimization.py
Normal file
182
tests/services/test_stats_aggregator_optimization.py
Normal file
@@ -0,0 +1,182 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date, datetime, time, timedelta, timezone
|
||||
from types import SimpleNamespace
|
||||
from typing import Any, cast
|
||||
|
||||
import pytest
|
||||
|
||||
from src.models.database import StatsDaily, StatsUserDaily
|
||||
from src.services.system.stats_aggregator import (
|
||||
AggregatedStats,
|
||||
StatsAggregatorService,
|
||||
query_stats_hybrid,
|
||||
)
|
||||
from src.services.system.time_range import TimeRangeParams
|
||||
|
||||
|
||||
class _FakeQuery:
|
||||
def __init__(self, *, all_result: list[Any] | None = None) -> None:
|
||||
self._all_result = all_result if all_result is not None else []
|
||||
|
||||
def filter(self, *_args: object, **_kwargs: object) -> _FakeQuery:
|
||||
return self
|
||||
|
||||
def group_by(self, *_args: object, **_kwargs: object) -> _FakeQuery:
|
||||
return self
|
||||
|
||||
def all(self) -> list[Any]:
|
||||
return self._all_result
|
||||
|
||||
|
||||
class _HybridQuerySession:
|
||||
def __init__(self, stats_daily_rows: list[SimpleNamespace]) -> None:
|
||||
self._stats_daily_rows = stats_daily_rows
|
||||
self.stats_daily_query_count = 0
|
||||
|
||||
def query(self, entity: object) -> _FakeQuery:
|
||||
if entity is StatsDaily:
|
||||
self.stats_daily_query_count += 1
|
||||
return _FakeQuery(all_result=self._stats_daily_rows)
|
||||
raise AssertionError(f"Unexpected query entity: {entity}")
|
||||
|
||||
|
||||
class _BatchUserStatsSession:
|
||||
def __init__(
|
||||
self, existing_rows: list[StatsUserDaily], aggregated_rows: list[SimpleNamespace]
|
||||
) -> None:
|
||||
self._responses: list[list[Any]] = [list(existing_rows), list(aggregated_rows)]
|
||||
self.added: list[StatsUserDaily] = []
|
||||
self.commit_count = 0
|
||||
|
||||
def query(self, *_entities: object) -> _FakeQuery:
|
||||
if not self._responses:
|
||||
raise AssertionError("Unexpected extra query")
|
||||
return _FakeQuery(all_result=self._responses.pop(0))
|
||||
|
||||
def add(self, row: StatsUserDaily) -> None:
|
||||
self.added.append(row)
|
||||
|
||||
def commit(self) -> None:
|
||||
self.commit_count += 1
|
||||
|
||||
|
||||
def test_query_stats_hybrid_batches_statsdaily_lookup_and_merges_realtime_ranges(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
today = datetime.now(timezone.utc).date()
|
||||
historical_cached_day = today - timedelta(days=4)
|
||||
historical_missing_day = today - timedelta(days=3)
|
||||
realtime_day = today
|
||||
|
||||
cached_row = SimpleNamespace(
|
||||
date=datetime.combine(historical_cached_day, time.min, tzinfo=timezone.utc),
|
||||
total_requests=10,
|
||||
success_requests=9,
|
||||
error_requests=1,
|
||||
input_tokens=100,
|
||||
output_tokens=50,
|
||||
cache_creation_tokens=5,
|
||||
cache_read_tokens=3,
|
||||
cache_creation_cost=1.2,
|
||||
cache_read_cost=0.8,
|
||||
total_cost=3.5,
|
||||
actual_total_cost=3.0,
|
||||
avg_response_time_ms=200.0,
|
||||
)
|
||||
db = _HybridQuerySession(stats_daily_rows=[cached_row])
|
||||
|
||||
calls: list[tuple[datetime, datetime]] = []
|
||||
|
||||
def _fake_aggregate_usage_range(
|
||||
_db: object,
|
||||
start_utc: datetime,
|
||||
end_utc: datetime,
|
||||
filters: object | None = None, # noqa: ARG001
|
||||
) -> AggregatedStats:
|
||||
calls.append((start_utc, end_utc))
|
||||
return AggregatedStats(total_requests=1, success_requests=1)
|
||||
|
||||
class _FakeParams:
|
||||
def get_complete_utc_dates(self) -> tuple[list[date], None, None]:
|
||||
return [historical_cached_day, historical_missing_day, realtime_day], None, None
|
||||
|
||||
monkeypatch.setattr(
|
||||
"src.services.system.stats_aggregator.aggregate_usage_range",
|
||||
_fake_aggregate_usage_range,
|
||||
)
|
||||
|
||||
result = query_stats_hybrid(cast(Any, db), cast(Any, _FakeParams()))
|
||||
|
||||
assert db.stats_daily_query_count == 1
|
||||
assert calls == [
|
||||
(
|
||||
datetime.combine(historical_missing_day, time.min, tzinfo=timezone.utc),
|
||||
datetime.combine(
|
||||
historical_missing_day + timedelta(days=1), time.min, tzinfo=timezone.utc
|
||||
),
|
||||
),
|
||||
(
|
||||
datetime.combine(realtime_day, time.min, tzinfo=timezone.utc),
|
||||
datetime.combine(realtime_day + timedelta(days=1), time.min, tzinfo=timezone.utc),
|
||||
),
|
||||
]
|
||||
assert result.total_requests == 12
|
||||
assert result.success_requests == 11
|
||||
|
||||
|
||||
def test_aggregate_user_daily_stats_batch_updates_all_users_in_two_queries() -> None:
|
||||
target_day = datetime(2026, 3, 1, tzinfo=timezone.utc)
|
||||
aggregated_rows = [
|
||||
SimpleNamespace(
|
||||
user_id="user-1",
|
||||
username="alice",
|
||||
total_requests=4,
|
||||
error_requests=1,
|
||||
input_tokens=20,
|
||||
output_tokens=8,
|
||||
cache_creation_tokens=2,
|
||||
cache_read_tokens=1,
|
||||
total_cost=1.5,
|
||||
)
|
||||
]
|
||||
db = _BatchUserStatsSession(existing_rows=[], aggregated_rows=aggregated_rows)
|
||||
|
||||
result = StatsAggregatorService.aggregate_user_daily_stats_batch(
|
||||
cast(Any, db),
|
||||
target_day,
|
||||
["user-1", "user-2"],
|
||||
commit=True,
|
||||
)
|
||||
|
||||
assert len(result) == 2
|
||||
assert db.commit_count == 1
|
||||
assert len(db.added) == 2
|
||||
|
||||
user_one = next(row for row in result if row.user_id == "user-1")
|
||||
user_two = next(row for row in result if row.user_id == "user-2")
|
||||
|
||||
assert user_one.username == "alice"
|
||||
assert user_one.total_requests == 4
|
||||
assert user_one.success_requests == 3
|
||||
assert user_one.total_cost == 1.5
|
||||
|
||||
assert user_two.total_requests == 0
|
||||
assert user_two.success_requests == 0
|
||||
assert user_two.error_requests == 0
|
||||
assert user_two.total_cost == 0.0
|
||||
|
||||
|
||||
def test_compute_percentiles_by_local_day_returns_sqlite_fallback_without_queries() -> None:
|
||||
db = SimpleNamespace(bind=SimpleNamespace(dialect=SimpleNamespace(name="sqlite")))
|
||||
time_range = TimeRangeParams(
|
||||
start_date=date(2026, 3, 1),
|
||||
end_date=date(2026, 3, 3),
|
||||
timezone="Asia/Singapore",
|
||||
)
|
||||
|
||||
result = StatsAggregatorService.compute_percentiles_by_local_day(cast(Any, db), time_range)
|
||||
|
||||
assert [row["date"] for row in result] == ["2026-03-01", "2026-03-02", "2026-03-03"]
|
||||
assert all(row["p50_response_time_ms"] is None for row in result)
|
||||
assert all(row["p50_first_byte_time_ms"] is None for row in result)
|
||||
Reference in New Issue
Block a user