// portcv 运行期性能观测量自检(**实测帧率** + **单帧耗时分布**)—— 新增,独立于既有自检。 // // 运行: // ctest --test-dir build -R portcv_rate_selftest --output-on-failure // ./build/portcv_rate_selftest // // 覆盖(对应 docs/technical_route.md §6.4 / §5.1): // [1] `RateMeter` 窗口语义:样本不足 → 0;窗口内节拍 → fps;越界丢老样本 // [2] `LatencyStats` 分位数:p50/p95/p99 与 numpy.percentile 默认口径(最近秩 + 线性插值)对拍 // [3] 环形覆盖 / NaN / 负值 / 容量 0 等边界:绝不把脏值带进分位数 // [4] `low_fps_flag` 与 Python `portcv/service/runner.py`(`0.0 < fps < low_fps`)**同表对拍** // [5] `fps_to_x10` 编码:四舍五入 + 饱和 + 非正值/NaN → 0(= 未就绪) // [6] 端到端口径演练:15 fps 主循环 200 帧 → 实测 fps / p95 / p99 / LOW_FPS 与"跑不动"对照 // // 为什么值得单独一组用例:`fps_x10` 从"配置值"改成"测量值"后,PLC 侧对 // "视觉跟得上吗"的判断就压在这两个量上;现场没有编译环境时,改错一个符号位都要等下一次 // 交叉编译才能发现。这里把口径钉死,`ctest` 一秒出结果。 // // 说明:断言描述用 ASCII(GBK 控制台下中文会乱码,影响现场排错),源码注释保持中文。 #include #include #include #include #include "portcv/rate_stats.hpp" #include "portcv/schema.hpp" namespace { int g_pass = 0; int g_fail = 0; void check(bool ok, const std::string& what) { if (ok) { ++g_pass; std::printf(" [OK] %s\n", what.c_str()); } else { ++g_fail; std::printf(" [FAIL] %s\n", what.c_str()); } } void check_eq(long long got, long long want, const std::string& what) { if (got == want) { ++g_pass; std::printf(" [OK] %s (=%lld)\n", what.c_str(), got); } else { ++g_fail; std::printf(" [FAIL] %s: got=%lld want=%lld\n", what.c_str(), got, want); } } void check_near(double got, double want, double tol, const std::string& what) { if (std::fabs(got - want) <= tol) { ++g_pass; std::printf(" [OK] %s (%.4f)\n", what.c_str(), got); } else { ++g_fail; std::printf(" [FAIL] %s: got=%.4f want=%.4f tol=%.4f\n", what.c_str(), got, want, tol); } } // ---------------------------------------------------------------- [1] 帧率滑窗 void test_rate_meter() { std::printf("[1] RateMeter: sliding-window measured fps\n"); portcv::RateMeter r(2.0); check_eq(static_cast(r.samples()), 0, "empty -> 0 samples"); check_near(r.fps(), 0.0, 1e-12, "empty -> fps 0 (nothing measured yet)"); check_near(r.window_s(), 2.0, 1e-12, "window = 2.0 s"); r.tick(100.0); check_eq(static_cast(r.samples()), 1, "1 tick -> 1 sample"); check_near(r.fps(), 0.0, 1e-12, "1 tick -> fps 0 (needs 2 samples)"); r.tick(100.5); check_near(r.fps(), 2.0, 1e-12, "2 ticks 0.5 s apart -> 2 fps"); // 窗口 = 1 s:5 拍 0/0.25/0.5/0.75/1.0 → 4 个间隔 / 1 s = 4 fps portcv::RateMeter w1(1.0); for (int i = 0; i < 5; ++i) { w1.tick(static_cast(i) * 0.25); } check_eq(static_cast(w1.samples()), 5, "window 1.0 keeps 5 ticks"); check_near(w1.fps(), 4.0, 1e-12, "window 1.0 -> 4 fps"); // 再走一拍:最老的 0.0 出窗(1.25-0.0 > 1.0),0.25 仍在窗内 → 仍是 4 fps w1.tick(1.25); check_eq(static_cast(w1.samples()), 5, "oldest sample dropped, size back to 5"); check_near(w1.fps(), 4.0, 1e-12, "steady 4 fps across the window edge"); portcv::RateMeter r10(2.0); for (int i = 0; i < 21; ++i) { r10.tick(static_cast(i) * 0.1); } check_eq(static_cast(r10.samples()), 21, "0..2.0 s @10 Hz -> 21 samples"); check_near(r10.fps(), 10.0, 1e-9, "0..2.0 s @10 Hz -> 10 fps"); portcv::RateMeter r30(2.0); for (int i = 0; i < 31; ++i) { r30.tick(static_cast(i) / 30.0); } check_near(r30.fps(), 30.0, 1e-9, "30 Hz stream -> 30 fps (measured, not configured)"); check_eq(static_cast(r30.samples()), 31, "30 Hz stream -> 31 samples in the 2 s window"); // 时钟没走(同一 ts 采两次)→ 不能报 inf portcv::RateMeter dup(2.0); dup.tick(10.0); dup.tick(10.0); check_near(dup.fps(), 0.0, 1e-12, "span 0 -> fps 0 (no inf/NaN)"); check_eq(static_cast(dup.samples()), 2, "span 0 keeps both samples"); // 长空档(相机卡住 3 s)→ 只剩最后 1 个样本 → 回到"未测出" portcv::RateMeter gap(2.0); for (int i = 0; i < 10; ++i) { gap.tick(static_cast(i) * 0.1); } gap.tick(3.0); check_eq(static_cast(gap.samples()), 1, "3 s stall leaves 1 sample"); check_near(gap.fps(), 0.0, 1e-12, "3 s stall -> fps 0 (stale window flushed)"); gap.reset(); check_eq(static_cast(gap.samples()), 0, "reset() clears samples"); check_near(gap.fps(), 0.0, 1e-12, "reset() -> fps 0"); const portcv::RateMeter bad(0.0); check_near(bad.window_s(), 2.0, 1e-12, "non-positive window falls back to 2.0 s"); } // ---------------------------------------------------------------- [2] 分位数 void test_latency_quantiles() { std::printf("[2] LatencyStats: p50/p95/p99 (numpy.percentile default flavour)\n"); portcv::LatencyStats s(256); check(s.empty(), "empty -> empty()"); check_eq(static_cast(s.size()), 0, "empty -> 0 samples"); check_eq(static_cast(s.total()), 0, "empty -> total 0"); check_near(s.mean(), 0.0, 1e-12, "empty -> mean 0"); check_near(s.quantile(0.50), 0.0, 1e-12, "empty -> p50 0"); check_near(s.quantile(0.95), 0.0, 1e-12, "empty -> p95 0"); check_near(s.max(), 0.0, 1e-12, "empty -> max 0"); check_near(s.min(), 0.0, 1e-12, "empty -> min 0"); // 1..10 ms:mean 5.5;p50 = 5.5;p95 = 9.55;p99 = 9.91(最近秩 + 线性插值) for (int i = 1; i <= 10; ++i) { s.add(static_cast(i)); } check_eq(static_cast(s.size()), 10, "10 samples kept"); check_eq(static_cast(s.total()), 10, "total counts every frame"); check_near(s.mean(), 5.5, 1e-12, "1..10 mean = 5.5"); check_near(s.quantile(0.50), 5.5, 1e-12, "1..10 p50 = 5.5"); check_near(s.quantile(0.95), 9.55, 1e-9, "1..10 p95 = 9.55"); check_near(s.quantile(0.99), 9.91, 1e-9, "1..10 p99 = 9.91"); check_near(s.quantile(0.0), 1.0, 1e-12, "q<=0 clamps to min"); check_near(s.quantile(1.0), 10.0, 1e-12, "q>=1 clamps to max"); check_near(s.max(), 10.0, 1e-12, "max = 10"); check_near(s.min(), 1.0, 1e-12, "min = 1"); // 长尾:均值看不出问题,p95/p99 才看得出 —— 这正是 §6.4 要求打点的原因 portcv::LatencyStats tail(256); for (int i = 1; i <= 100; ++i) { tail.add(static_cast(i)); } for (int i = 0; i < 3; ++i) { tail.add(5000.0); } check_near(tail.quantile(0.50), 52.0, 1e-9, "long tail: p50 stays 52 ms"); check(tail.quantile(0.95) > tail.quantile(0.50) * 1.5, "long tail: p95 >> p50 (the mean would hide it)"); check_near(tail.quantile(1.0), 5000.0, 1e-12, "long tail: max = 5000 ms"); check(tail.mean() < tail.quantile(0.99) / 4.0, "long tail: mean is dominated by the fast frames"); } // ---------------------------------------------------------------- [3] 环形覆盖与脏值 void test_ring_and_edges() { std::printf("[3] LatencyStats: ring overwrite / NaN / capacity edges\n"); portcv::LatencyStats s(4); for (int i = 1; i <= 4; ++i) { s.add(static_cast(i)); } check_eq(static_cast(s.size()), 4, "cap 4 filled"); check_near(s.mean(), 2.5, 1e-12, "cap 4: 1..4 mean 2.5"); s.add(5.0); // 覆盖最旧的 1.0 check_eq(static_cast(s.size()), 4, "cap 4: size stays 4 after overflow"); check_eq(static_cast(s.total()), 5, "cap 4: total still counts frame 5"); check_near(s.mean(), 3.5, 1e-12, "cap 4: window mean = (5+2+3+4)/4 = 3.5"); check_near(s.min(), 2.0, 1e-12, "cap 4: oldest 1.0 was dropped"); check_near(s.max(), 5.0, 1e-12, "cap 4: max = 5.0"); check_near(s.quantile(0.50), 3.5, 1e-12, "cap 4: p50 of {2,3,4,5} = 3.5"); const std::vector asc = s.sorted(); check_eq(static_cast(asc.size()), 4, "sorted() returns the window"); check(asc[0] <= asc[1] && asc[1] <= asc[2] && asc[2] <= asc[3], "sorted() ascending"); portcv::LatencyStats dirty(8); dirty.add(std::nan("")); dirty.add(-5.0); dirty.add(3.0); check_eq(static_cast(dirty.size()), 3, "dirty values still counted as samples"); check_near(dirty.quantile(0.0), 0.0, 1e-12, "NaN / -5 clamped to 0 (min)"); check_near(dirty.mean(), 1.0, 1e-12, "NaN / -5 clamp -> mean (0+0+3)/3 = 1"); check_near(dirty.max(), 3.0, 1e-12, "max unaffected by dirty samples"); portcv::LatencyStats tiny(0); // 容量 0 → 退回 1(不崩、不发散) check_eq(static_cast(tiny.capacity()), 1, "capacity 0 falls back to 1"); tiny.add(7.0); tiny.add(9.0); check_eq(static_cast(tiny.size()), 1, "cap 1 keeps the newest sample only"); check_near(tiny.mean(), 9.0, 1e-12, "cap 1 mean = newest sample"); check_eq(static_cast(tiny.total()), 2, "cap 1 total = 2"); tiny.reset(); check_eq(static_cast(tiny.size()), 0, "reset() -> 0 samples"); check_eq(static_cast(tiny.total()), 0, "reset() -> total 0"); check_near(tiny.quantile(0.5), 0.0, 1e-12, "reset() -> p50 0"); } // ---------------------------------------------------------------- [4] LOW_FPS 判据 void test_low_fps_flag() { std::printf("[4] low_fps_flag: parity with runner.py `0.0 < fps < low_fps`\n"); struct Case { double fps; double thr; bool want; }; const Case cases[] = { {0.0, 3.0, false}, // 还没测出来 → 不置位(避免启动瞬间误报) {0.001, 3.0, true}, // 测出来了、但确实慢 → 置位 {2.999, 3.0, true}, {3.0, 3.0, false}, // 边界取严:等于阈值不算低 {10.0, 3.0, false}, {5.0, 0.0, false}, // 阈值 0 = 关闭该告警(Python 侧同样恒为假) {0.0, 0.0, false}, {-1.0, 3.0, false}, // 异常值不能触发告警 {1.0, -3.0, false}, // 阈值 ≤ 0 = 关闭 }; for (const Case& c : cases) { char buf[96]; std::snprintf(buf, sizeof(buf), "fps=%.3f thr=%.1f -> %s", c.fps, c.thr, c.want ? "1" : "0"); check(portcv::low_fps_flag(c.fps, c.thr) == c.want, buf); } // 位本身也得对得上:Python `StatusBits.LOW_FPS = 1 << 3` check_eq(static_cast(portcv::StatusBits::LOW_FPS), 8, "StatusBits::LOW_FPS == 1<<3"); portcv::VisionSnapshot snap; snap.flags = 0; snap.set_flag(portcv::StatusBits::LOW_FPS, true); check_eq(static_cast(snap.flags), 8, "set_flag(LOW_FPS) -> flags == 0x0008"); check(snap.has_flag(portcv::StatusBits::LOW_FPS), "has_flag(LOW_FPS) == true"); snap.set_flag(portcv::StatusBits::LOW_FPS, false); check_eq(static_cast(snap.flags), 0, "clear -> flags == 0"); check(!snap.has_flag(portcv::StatusBits::LOW_FPS), "has_flag(LOW_FPS) == false after clear"); } // ---------------------------------------------------------------- [5] 寄存器编码 void test_fps_to_x10() { std::printf("[5] fps_to_x10: encode the measured fps for the register\n"); struct Case { double fps; long long want; }; const Case cases[] = { {0.0, 0}, {-1.0, 0}, {0.001, 0}, {0.04, 0}, {0.05, 1}, {0.06, 1}, {0.5, 5}, {14.94, 149}, {14.95, 150}, {14.96, 150}, {15.0, 150}, {15.04, 150}, {30.0, 300}, {6553.0, 65530}, {6553.6, 65535}, {6554.0, 65535}, {1e9, 65535}, }; for (const Case& c : cases) { char buf[80]; std::snprintf(buf, sizeof(buf), "fps=%.3f -> fps_x10=%lld", c.fps, c.want); check_eq(static_cast(portcv::fps_to_x10(c.fps)), c.want, buf); } check_eq(static_cast(portcv::fps_to_x10(std::nan(""))), 0, "NaN fps -> 0 (never a garbage large value)"); } // ---------------------------------------------------------------- [6] 端到端口径演练 void test_loop_scenario() { std::printf("[6] end-to-end shape: a 15 fps loop of 200 frames\n"); portcv::RateMeter rate(2.0); portcv::LatencyStats lat(256); for (int i = 0; i < 200; ++i) { rate.tick(static_cast(i) / 15.0); lat.add(60.0 + static_cast(i % 7) * 0.5); } check_near(rate.fps(), 15.0, 1e-9, "measured fps = 15.0 (not run.max_fps)"); check(rate.samples() >= 29 && rate.samples() <= 32, "2 s window holds ~30 samples at 15 fps"); check_eq(static_cast(portcv::fps_to_x10(rate.fps())), 150, "fps_x10 = 150"); check_eq(static_cast(lat.size()), 200, "200 frames fit in the 256-slot window"); check_near(lat.mean(), 61.485, 1e-9, "mean frame time = 61.485 ms (29/29/29/29/28/28/28 split)"); check_near(lat.quantile(0.50), 61.5, 1e-9, "p50 = 61.5 ms"); check_near(lat.quantile(0.95), 63.0, 1e-9, "p95 = 63.0 ms"); check_near(lat.quantile(0.99), 63.0, 1e-9, "p99 = 63.0 ms"); check_near(lat.max(), 63.0, 1e-12, "max = 63.0 ms"); check(!portcv::low_fps_flag(rate.fps(), 3.0), "15 fps >= 3 fps -> LOW_FPS stays 0"); // 同一套代码换一块"跑不动"的板子:1.5 fps → 位必须置起来 portcv::RateMeter slow(2.0); portcv::LatencyStats slow_lat(256); for (int i = 0; i < 30; ++i) { slow.tick(static_cast(i) / 1.5); slow_lat.add(120.0 + static_cast(i % 5) * 20.0); } check_near(slow.fps(), 1.5, 1e-9, "slow board -> measured 1.5 fps"); check(portcv::low_fps_flag(slow.fps(), 3.0), "1.5 fps < 3 fps -> LOW_FPS=1 (PLC must reject)"); check_eq(static_cast(portcv::fps_to_x10(slow.fps())), 15, "1.5 fps -> fps_x10 = 15"); check(slow_lat.quantile(0.99) > slow_lat.quantile(0.50), "degraded board: p99 > p50 (visible in the report)"); std::printf(" report shape: fps=%.1f (x10=%u) p50=%.1f p95=%.1f p99=%.1f max=%.1f LOW_FPS=%d\n", slow.fps(), static_cast(portcv::fps_to_x10(slow.fps())), slow_lat.quantile(0.50), slow_lat.quantile(0.95), slow_lat.quantile(0.99), slow_lat.max(), portcv::low_fps_flag(slow.fps(), 3.0) ? 1 : 0); } } // namespace int main() { std::printf("portcv rate-stats selftest (technical_route.md 6.4)\n"); test_rate_meter(); test_latency_quantiles(); test_ring_and_edges(); test_low_fps_flag(); test_fps_to_x10(); test_loop_scenario(); std::printf("\n%d passed / %d failed\n", g_pass, g_fail); return g_fail == 0 ? 0 : 1; }