feat(workbench): 工程量公式、另存为导出与字段编辑历史等多项功能
This commit is contained in:
140
CostReview/app/tests/test_perf_baseline.py
Normal file
140
CostReview/app/tests/test_perf_baseline.py
Normal file
@@ -0,0 +1,140 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""数据库性能 / 压测基线(工具代码,不碰软件本体)。
|
||||
|
||||
设计原则(见 输出/测试对账与修复报告_20261017.md 的独立意见):
|
||||
- 软基线:实测数值打印出来(pytest -s 可见),超阈值时**标黄警告但不硬失败**,
|
||||
避免用已知性能缺陷(sid=17 那 15230 清单打开 >32s 不稳定)反过来卡死日常提交。
|
||||
- 规模可用环境变量 PR_PERF_BILLS 调(默认 3000;要复现 15230 案例设 15000)。
|
||||
- 全部走 api_harness 的临时库,生产库零接触。
|
||||
|
||||
覆盖场景:
|
||||
1. 大工程导入吞吐(后台 /api/admin/import,写 history.db)
|
||||
2. 打开大工程解析耗时(/api/audit,写 session.db,原 >32s 病灶)
|
||||
3. 清单加载耗时(/api/session/rows,读 session.db)
|
||||
4. 锁竞争回归:导入大工程(写 history.db)期间,并发读 /api/admin/batches 不得死等
|
||||
—— 直接守住 2026-10-10 的「按钮全点不动」锁库死等修复。
|
||||
"""
|
||||
import os
|
||||
import time
|
||||
import threading
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from api_harness import META, fixture_file, server # noqa: F401
|
||||
|
||||
# 软阈值(秒)——仅告警,不失败
|
||||
SOFT_IMPORT_SEC = 30.0
|
||||
SOFT_AUDIT_SEC = 30.0
|
||||
SOFT_ROWS_SEC = 5.0
|
||||
SOFT_READ_SEC = 8.0 # 锁竞争下单次读不得死等(> 即疑似死等)
|
||||
|
||||
DEFAULT_BILLS = int(os.environ.get("PR_PERF_BILLS", "3000"))
|
||||
|
||||
|
||||
def _heavy_client(server):
|
||||
# 重型调用放宽超时(默认 harness client 是 60s,大工程解析可能更久)
|
||||
return httpx.Client(base_url="http://127.0.0.1:%d" % server["port"], timeout=300)
|
||||
|
||||
|
||||
def _report(name, secs, n=None, soft=None):
|
||||
if n:
|
||||
rate = (n / secs) if secs else 0
|
||||
line = "[perf] %-22s %.2fs (%d 项, %.0f 项/s)" % (name, secs, n, rate)
|
||||
else:
|
||||
line = "[perf] %-22s %.2fs" % (name, secs)
|
||||
print(line)
|
||||
if soft is not None and secs > soft:
|
||||
print(" ⚠ 超过软阈值 %.1fs —— 性能回归预警(不阻塞提交)" % soft)
|
||||
return secs
|
||||
|
||||
|
||||
def _big_bills(scale, prefix):
|
||||
return [("%s%05d" % (prefix, i), "清单项%d" % i, "项目特征%d" % i, "项", i + 1, 100.0 + i)
|
||||
for i in range(scale)]
|
||||
|
||||
|
||||
@pytest.mark.perf
|
||||
def test_large_project_import_and_open(server):
|
||||
scale = DEFAULT_BILLS
|
||||
name = "性能基线工程"
|
||||
bills = _big_bills(scale, "B")
|
||||
# 注意:make_cos4 的 lmes 各字段须为字符串(quoteattr 要求)
|
||||
lmes = [("L0001", "水泥", "P.O42.5", "t", "385.0", "435.05"),
|
||||
("L0002", "钢筋", "Φ16", "t", "3880.0", "3996.4")]
|
||||
fp = fixture_file(server, "perf_large.cos4", name, bills, lmes)
|
||||
|
||||
cl = _heavy_client(server)
|
||||
|
||||
# 1) 导入后台(写 history.db)
|
||||
t0 = time.time()
|
||||
r = cl.post("/api/admin/import",
|
||||
json=dict(META, path=str(fp), name=name, file_kind=1)).json()
|
||||
import_sec = time.time() - t0
|
||||
assert r.get("ok") is True, r
|
||||
_report("import(后台导入)", import_sec, scale, SOFT_IMPORT_SEC)
|
||||
|
||||
# 2) 打开工程→解析(写 session.db,原 >32s 病灶)
|
||||
t0 = time.time()
|
||||
r2 = cl.post("/api/audit", json=dict(META, path=str(fp), name=name)).json()
|
||||
audit_sec = time.time() - t0
|
||||
assert r2.get("ok") is True, r2
|
||||
sid = r2["session_id"]
|
||||
_report("audit(打开解析)", audit_sec, scale, SOFT_AUDIT_SEC)
|
||||
|
||||
# 3) 清单加载(读 session.db)
|
||||
t0 = time.time()
|
||||
rows = cl.get("/api/session/rows",
|
||||
params=dict(session_id=sid, type="bill", page=1, page_size=200)).json()
|
||||
rows_sec = time.time() - t0
|
||||
# /api/session/rows 返回体为 {total, rows},无 ok 字段
|
||||
assert "rows" in rows and "total" in rows, rows
|
||||
_report("session/rows(清单加载)", rows_sec, rows.get("total"), SOFT_ROWS_SEC)
|
||||
|
||||
cl.close()
|
||||
|
||||
|
||||
@pytest.mark.perf
|
||||
def test_read_not_starved_under_write(server):
|
||||
"""锁竞争回归:导入大工程(写 history.db)期间,并发读 /api/admin/batches 不得死等。"""
|
||||
scale = max(DEFAULT_BILLS, 1000)
|
||||
name = "锁竞争工程"
|
||||
bills = _big_bills(scale, "C")
|
||||
fp = fixture_file(server, "perf_lock.cos4", name, bills)
|
||||
|
||||
cl = _heavy_client(server)
|
||||
|
||||
err = {}
|
||||
|
||||
def _writer():
|
||||
try:
|
||||
rr = cl.post("/api/admin/import",
|
||||
json=dict(META, path=str(fp), name=name, file_kind=1)).json()
|
||||
err["ok"] = rr.get("ok")
|
||||
except Exception as e: # noqa: BLE001
|
||||
err["exc"] = repr(e)
|
||||
|
||||
t = threading.Thread(target=_writer, daemon=True)
|
||||
t.start()
|
||||
|
||||
max_lat = 0.0
|
||||
samples = 0
|
||||
deadline = time.time() + 90
|
||||
while time.time() < deadline and t.is_alive():
|
||||
t0 = time.time()
|
||||
try:
|
||||
cl.get("/api/admin/batches", params=dict(page=1, page_size=10)).json()
|
||||
except Exception as e: # noqa: BLE001
|
||||
err.setdefault("read_exc", repr(e))
|
||||
max_lat = max(max_lat, time.time() - t0)
|
||||
samples += 1
|
||||
t.join(timeout=30)
|
||||
|
||||
_report("read-under-write 最大读延迟", max_lat, samples, SOFT_READ_SEC)
|
||||
assert max_lat < SOFT_READ_SEC, (
|
||||
"锁竞争下读延迟 %.2fs 超过阈值 %.1fs,疑似死等(回归 #锁竞争修复);"
|
||||
"若确为性能问题请排查 _open_ro 短超时是否生效" % (max_lat, SOFT_READ_SEC))
|
||||
if err:
|
||||
print("[perf] writer/reader 异常(不影响延迟断言):", err)
|
||||
|
||||
cl.close()
|
||||
Reference in New Issue
Block a user