v1.3.19 测速时长默认10秒+下拉10档(10/30/..190s)

This commit is contained in:
3970260761 committed 2026-11-08 01:37:53 +08:00
1 parent 417379d6f4
commit 942eab8503
3 files changed
+90 -37

No files matched your search

+81 -31
View File
@@ -16,7 +16,7 @@ PORT = int(os.environ.get("PORT", "7031"))
# ---------- CardAuth hub (免费形态: 安装统计/在线/公告/版本推送,失败开放) ----------
HUB_URL = os.environ.get("HUB_URL", "https://auth.88531.cn/api").rstrip("/")
HUB_PROJECT_ID = os.environ.get("HUB_PROJECT_ID", "12")
HUB_VERSION = os.environ.get("SITE_VERSION", "1.3.18")
HUB_VERSION = os.environ.get("SITE_VERSION", "1.3.19")
HUB = {"ok": False, "install_id": None, "latest": "", "update_available": False,
"update_required": False, "download_url": "", "changelog": "",
"announcements": [], "popup": [], "ts": 0}
@@ -509,34 +509,62 @@ def get_percpu_mhz():
# ---------- 各页测速: CPU/内存/硬盘/网络(非破坏,失败返回error) ----------
import urllib.request as _urlreq
def _bench_cpu():
import math
N = 200000
t0 = time.perf_counter()
def _bench_cpu(dur=10):
try:
dur = max(1, min(200, int(dur)))
except Exception:
dur = 10
N_total = 0
x = 0
for i in range(N):
x += (i * i) % 10007
dt = max(time.perf_counter() - t0, 1e-6)
return {"ms": int(round(dt * 1000)), "kops": int(round(N / dt / 1000)),
"score": int(round(N / dt / 1000)), "detail": "20万次整数运算耗时"}
def _bench_mem():
SZ = 32 * 1024 * 1024
t0 = time.perf_counter()
batch = 200000
while True:
for i in range(batch):
x += (i * i) % 10007
N_total += batch
if time.perf_counter() - t0 >= dur:
break
dt = max(time.perf_counter() - t0, 1e-6)
return {"ms": int(round(dt * 1000)), "kops": int(round(N_total / dt / 1000)),
"score": int(round(N_total / dt / 1000)), "dur": round(dt, 1),
"detail": "整数运算持续约%s秒" % dur}
def _bench_mem(dur=10):
try:
dur = max(1, min(200, int(dur)))
except Exception:
dur = 10
SZ = 32 * 1024 * 1024
b = bytearray(SZ)
b[:] = b"\x5a" * SZ
t0 = time.perf_counter()
w_bytes = 0
while True:
b[:] = b"\x5a" * SZ
w_bytes += SZ
if time.perf_counter() - t0 >= dur / 2.0:
break
dt_w = max(time.perf_counter() - t0, 1e-6)
t0 = time.perf_counter()
s = sum(b[::4096])
r_bytes = 0
s = 0
while True:
s += sum(b[::4096])
r_bytes += SZ
if time.perf_counter() - t0 >= dur / 2.0:
break
dt_r = max(time.perf_counter() - t0, 1e-6)
del b
return {"write_mbs": round(SZ / dt_w / 1048576, 1),
"read_mbs": round(SZ / dt_r / 1048576, 1),
"size_mb": 32, "detail": "32MB 内存写/读", "checksum": s % 997}
return {"write_mbs": round(w_bytes / dt_w / 1048576, 1),
"read_mbs": round(r_bytes / dt_r / 1048576, 1),
"size_mb": 32, "dur": dur, "detail": "内存写/读持续约%s秒" % dur, "checksum": s % 997}
def _bench_disk():
SZ_MB = 32
data = os.urandom(4 * 1024 * 1024)
def _bench_disk(dur=10):
try:
dur = max(1, min(200, int(dur)))
except Exception:
dur = 10
CHUNK = 4 * 1024 * 1024
data = os.urandom(CHUNK)
path = ""
for cand in ("/host/vol1/.fnos_resmon_bench.tmp",
"/host/.fnos_resmon_bench.tmp", "/tmp/.fnos_resmon_bench.tmp"):
@@ -550,22 +578,34 @@ def _bench_disk():
if not path:
return {"ok": False, "error": "无可写目录"}
try:
w_bytes = 0
t0 = time.perf_counter()
with open(path, "wb") as f:
for _ in range(SZ_MB // 4):
while True:
f.write(data)
w_bytes += CHUNK
if time.perf_counter() - t0 >= dur / 2.0:
break
f.flush()
os.fsync(f.fileno())
dt_w = max(time.perf_counter() - t0, 1e-6)
t0 = time.perf_counter()
r_bytes = 0
with open(path, "rb") as f:
while f.read(4 * 1024 * 1024):
pass
while True:
ch = f.read(CHUNK)
if not ch:
f.seek(0)
continue
r_bytes += len(ch)
if time.perf_counter() - t0 >= dur / 2.0:
break
dt_r = max(time.perf_counter() - t0, 1e-6)
return {"write_mbs": round(SZ_MB / dt_w, 1),
"read_mbs": round(SZ_MB / dt_r, 1),
"size_mb": SZ_MB, "file": path.replace("/host", "") or "/",
"detail": "32MB 文件写(含落盘)/读"}
return {"write_mbs": round(w_bytes / dt_w / 1048576, 1),
"read_mbs": round(r_bytes / dt_r / 1048576, 1),
"size_mb": round(w_bytes / 1048576, 0), "dur": dur,
"file": path.replace("/host", "") or "/",
"detail": "文件写(含落盘)/读持续约%s秒" % dur}
except Exception as e:
return {"ok": False, "error": str(e)}
finally:
@@ -615,8 +655,18 @@ def _bench_net():
@app.route("/api/bench/<kind>", methods=["POST"])
def api_bench(kind):
try:
dur = 10
try:
j0 = request.get_json(silent=True) or {}
if isinstance(j0, dict) and "dur" in j0:
dur = int(j0["dur"])
elif request.values.get("dur"):
dur = int(request.values.get("dur"))
except Exception:
dur = 10
dur = max(1, min(200, dur))
if kind == "cpu":
r = _bench_cpu()
r = _bench_cpu(dur)
p = _read_perf()
r.update({"ok": True, "mode": p.get("mode", ""),
"governor": p.get("governor", ""), "epp": p.get("epp", ""),
@@ -625,10 +675,10 @@ def api_bench(kind):
"capped": p.get("capped", False),
"mhz_avg": (sum(get_percpu_mhz()) // len(get_percpu_mhz()) if get_percpu_mhz() else 0)})
elif kind == "mem":
r = _bench_mem()
r = _bench_mem(dur)
r["ok"] = True
elif kind == "disk":
r = _bench_disk()
r = _bench_disk(dur)
if "ok" not in r:
r["ok"] = True
elif kind == "net":