from pathlib import Path from benchmark import Scenario, build_command, build_scenarios, parse_threads, render_report, ScenarioResult def test_build_default_matrix(): scenarios = build_scenarios( ["tiny", "small", "medium"], ["fast", "standard"], [144], [None], ) assert len(scenarios) == 6 assert scenarios[0].name == "tiny-fast-dpi144-thauto-s64min-d0p3-b0p6-u1p5-r0-rb6" assert scenarios[-1].name == "medium-standard-dpi144-thauto-s64min-d0p3-b0p6-u1p5-r0-rb6" def test_parse_threads(): assert parse_threads(["auto", "4", "4"]) == [None, 4] def test_command_contains_model_and_profile_flags(tmp_path): scenario = Scenario("small", "fast", 120, 4) command = build_command( scenario, data_dir=tmp_path / "data", output_dir=tmp_path / "output", device="cpu", device_id=0, warmup=1, rounds=2, pdf_mode="ocr", ) assert command[1].endswith("ocr.py") assert command[command.index("--model-size") + 1] == "small" assert "--no-doc-orientation-classify" in command assert "--no-textline-orientation" in command assert command[command.index("--threads") + 1] == "4" assert command[command.index("--text-det-thresh") + 1] == "0.3" assert command[command.index("--text-recognition-batch-size") + 1] == "6" def test_report_contains_comparison_table(tmp_path): scenario = Scenario("tiny", "fast", 144, None) result = ScenarioResult( scenario=scenario, status="completed", exit_code=0, wall_seconds=3.0, model_init_seconds=1.0, pure_ocr_seconds=2.0, image_inference_seconds=1.0, pdf_ocr_seconds=1.0, processed_files=2, image_files=1, pdf_files=1, ocr_pages=1, recognized_lines=10, mean_confidence=0.95, units_per_second=1.0, output_dir=str(tmp_path / "output"), log_file=str(tmp_path / "run.log"), command=["python", "ocr.py"], ) from datetime import datetime report = render_report( [result], data_dir=Path("data"), device="cpu", pdf_mode="ocr", warmup=1, rounds=1, started_at=datetime.now().astimezone(), finished_at=datetime.now().astimezone(), ) assert "# PP-OCRv6 参数测试报告" in report assert "tiny-fast-dpi144-thauto-s64min-d0p3-b0p6-u1p5-r0-rb6" in report assert "纯 OCR 耗时最短" in report