66 lines
2.4 KiB
Python
66 lines
2.4 KiB
Python
# -*- coding: utf-8 -*-
|
||||
|
|
"""性能预算与懒加载策略冒烟脚本(Issue #54)。
|
|||
|
|
|
|||
|
|
直接运行 ``python _sanity_check.py`` 验证:默认预算可构造、校验器对达标/超预算
|
|||
|
|
场景给出正确状态、懒加载决策器对视口内外 widget 给出 immediate/idle。零第三方依赖。
|
|||
|
|
"""
|
|||
|
|
import importlib.util
|
|||
|
|
import os
|
|||
|
|
import sys
|
|||
|
|
|
|||
|
|
# perf-budget 目录名含连字符,按文件路径加载为合法包 perf_budget。
|
|||
|
|
_PKG_DIR = os.path.dirname(os.path.abspath(__file__))
|
|||
|
|
|
|||
|
|
|
|||
|
|
def _load_pkg(name, path):
|
|||
|
|
if name in sys.modules:
|
|||
|
|
return
|
|||
|
|
spec = importlib.util.spec_from_file_location(
|
|||
|
|
name, os.path.join(path, "__init__.py"),
|
|||
|
|
submodule_search_locations=[path])
|
|||
|
|
mod = importlib.util.module_from_spec(spec)
|
|||
|
|
sys.modules[name] = mod
|
|||
|
|
spec.loader.exec_module(mod)
|
|||
|
|
|
|||
|
|
|
|||
|
|
_load_pkg("perf_budget", _PKG_DIR)
|
|||
|
|
|
|||
|
|
from perf_budget import ( # noqa: E402
|
|||
|
|
BudgetStatus, BudgetVerifier, LazyLoadPlanner, PerformanceBudget,
|
|||
|
|
ResourceMeasurement, ResourceType, WidgetLayout)
|
|||
|
|
|
|||
|
|
|
|||
|
|
def main() -> int:
|
|||
|
|
# 1) 性能预算:达标场景
|
|||
|
|
budget = PerformanceBudget.default_cockpit()
|
|||
|
|
report = BudgetVerifier(budget).verify([
|
|||
|
|
ResourceMeasurement("app.js", ResourceType.JS, 700, block_render=True),
|
|||
|
|
ResourceMeasurement("app.css", ResourceType.CSS, 250, block_render=True),
|
|||
|
|
ResourceMeasurement("doc.html", ResourceType.HTML, 150, block_render=True),
|
|||
|
|
])
|
|||
|
|
assert report.status is BudgetStatus.PASS, report.reason
|
|||
|
|
print(f"[OK] 性能预算达标:首屏 {report.first_paint_ms:.0f}ms "
|
|||
|
|
f"≤ {report.budget_ms:.0f}ms({report.reason})")
|
|||
|
|
|
|||
|
|
# 2) 超预算场景
|
|||
|
|
bad = BudgetVerifier(budget).verify([
|
|||
|
|
ResourceMeasurement("big.js", ResourceType.JS, 950, block_render=True),
|
|||
|
|
])
|
|||
|
|
assert bad.status is BudgetStatus.FAIL
|
|||
|
|
print(f"[OK] 性能预算告警:{bad.overrun[0].reason} → {bad.overrun[0].advice}")
|
|||
|
|
|
|||
|
|
# 3) 懒加载:视口内外分流
|
|||
|
|
plan = LazyLoadPlanner().plan([
|
|||
|
|
WidgetLayout("proc", "process_view", y=0, h=4, always_load=True),
|
|||
|
|
WidgetLayout("trend", "trend", y=10, h=2),
|
|||
|
|
], scroll_y_rows=0)
|
|||
|
|
assert plan.immediate_ids == ["proc"]
|
|||
|
|
assert plan.idle_ids == ["trend"]
|
|||
|
|
print(f"[OK] 懒加载:immediate={plan.immediate_ids} idle={plan.idle_ids}")
|
|||
|
|
print("性能预算与懒加载策略冒烟通过 ✅")
|
|||
|
|
return 0
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
raise SystemExit(main())
|