feat: wire live benchmark CLI endpoints

This commit is contained in:
Dobromir Popov
2026-07-15 10:34:20 +03:00
parent a508768e8a
commit c035bad5b7
3 changed files with 147 additions and 6 deletions

View File

@@ -5,6 +5,8 @@ from __future__ import annotations
import json
from unittest.mock import MagicMock, patch
import pytest
from meshnet_node.performance_contract import (
BENCHMARK_SCHEMA_VERSION,
DEFAULT_CONTRACT,
@@ -201,3 +203,84 @@ def test_real_model_endpoint_benchmark_uses_lane_specific_endpoints_and_shared_s
assert lane["output_tokens"] == ["mesh", "activation"]
assert report["comparisons"]["cpu"]["gguf_lane"] == "llama-cpp-gguf-cpu"
assert report["comparisons"]["gpu"]["gguf_lane"] == "llama-cpp-gguf-gpu"
def test_contract_cli_runs_live_endpoint_benchmark(tmp_path, capsys):
"""--live-endpoint mappings drive the live runner and write its report.
Tags: performance, benchmark, live, artifact
"""
contract_out = tmp_path / "performance-contract.json"
live_out = tmp_path / "artifacts" / "live-benchmark-report.json"
endpoints = {
"transformers-safetensors-cpu": "http://cpu-safetensors",
"llama-cpp-gguf-cpu": "http://cpu-gguf",
"transformers-safetensors-gpu": "http://gpu-safetensors",
"llama-cpp-gguf-gpu": "http://gpu-gguf",
}
fake_report = {"schema_version": BENCHMARK_SCHEMA_VERSION, "source": "real-model-endpoints"}
argv = ["--json-out", str(contract_out), "--live-benchmark-out", str(live_out)]
for lane_id, url in endpoints.items():
argv += ["--live-endpoint", f"{lane_id}={url}"]
with patch(
"meshnet_node.performance_contract.run_real_model_endpoint_benchmark",
return_value=fake_report,
) as runner:
assert main(argv) == 0
runner.assert_called_once_with(
endpoints,
model=DEFAULT_CONTRACT.model_target.safetensors_repo,
contract=DEFAULT_CONTRACT,
)
assert json.loads(live_out.read_text(encoding="utf-8")) == fake_report
output = capsys.readouterr().out
assert str(contract_out) in output
assert str(live_out) in output
def test_contract_cli_passes_explicit_live_model(tmp_path):
"""--live-model overrides the contract's safetensors repo default.
Tags: performance, benchmark, live
"""
live_out = tmp_path / "live-benchmark-report.json"
argv = [
"--json-out", str(tmp_path / "performance-contract.json"),
"--live-benchmark-out", str(live_out),
"--live-endpoint", "transformers-safetensors-cpu=http://cpu-safetensors",
"--live-model", "local/DeepSeek-V2-Lite-Chat-Q2_K",
]
with patch(
"meshnet_node.performance_contract.run_real_model_endpoint_benchmark",
return_value={},
) as runner:
assert main(argv) == 0
assert runner.call_args.kwargs["model"] == "local/DeepSeek-V2-Lite-Chat-Q2_K"
@pytest.mark.parametrize(
"argv",
[
["--live-endpoint", "transformers-safetensors-cpu=http://cpu"],
["--live-benchmark-out", "live-report.json"],
[
"--live-endpoint", "not-a-mapping",
"--live-benchmark-out", "live-report.json",
],
],
ids=["endpoint-without-out", "out-without-endpoint", "malformed-mapping"],
)
def test_contract_cli_rejects_incomplete_live_arguments(tmp_path, argv, capsys):
"""Live flags must arrive as a consistent LANE_ID=URL + output-path set.
Tags: performance, benchmark, live, cli
"""
with pytest.raises(SystemExit) as excinfo:
main(["--json-out", str(tmp_path / "performance-contract.json"), *argv])
assert excinfo.value.code == 2
assert "--live-" in capsys.readouterr().err