| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""Benchmark tests for ANTA.""" |
| 5 |
|
| 6 |
import asyncio |
| 7 |
import logging |
| 8 |
from collections import defaultdict |
| 9 |
from unittest.mock import patch |
| 10 |
|
| 11 |
import pytest |
| 12 |
import respx |
| 13 |
from pytest_codspeed import BenchmarkFixture |
| 14 |
|
| 15 |
from anta.catalog import AntaCatalog |
| 16 |
from anta.inventory import AntaInventory |
| 17 |
from anta.result_manager import ResultManager |
| 18 |
from anta.result_manager.models import AntaTestStatus |
| 19 |
from anta.runner import main |
| 20 |
|
| 21 |
from .utils import collect, collect_commands |
| 22 |
|
| 23 |
logger = logging.getLogger(__name__) |
| 24 |
|
| 25 |
|
| 26 |
def test_anta_dry_run( |
| 27 |
benchmark: BenchmarkFixture, |
| 28 |
catalog: AntaCatalog, |
| 29 |
inventory: AntaInventory, |
| 30 |
request: pytest.FixtureRequest, |
| 31 |
session_results: defaultdict[str, ResultManager], |
| 32 |
) -> None: |
| 33 |
"""Benchmark ANTA in Dry-Run Mode.""" |
| 34 |
|
| 35 |
logging.disable() |
| 36 |
|
| 37 |
results = session_results[request.node.callspec.id] |
| 38 |
|
| 39 |
|
| 40 |
@benchmark |
| 41 |
def _() -> None: |
| 42 |
results.reset() |
| 43 |
catalog.clear_indexes() |
| 44 |
asyncio.run(main(results, inventory, catalog, dry_run=True)) |
| 45 |
|
| 46 |
logging.disable(logging.NOTSET) |
| 47 |
|
| 48 |
if len(results.results) != len(inventory) * len(catalog.tests): |
| 49 |
pytest.fail(f"Expected {len(inventory) * len(catalog.tests)} tests but got {len(results.results)}", pytrace=False) |
| 50 |
bench_info = f"\n--- ANTA NRFU Dry-Run Benchmark Information ---\nTest count: {len(results.results)}\n-----------------------------------------------" |
| 51 |
logger.info(bench_info) |
| 52 |
|
| 53 |
|
| 54 |
@patch("anta.models.AntaTest.collect", collect) |
| 55 |
@patch("anta.device.AntaDevice.collect_commands", collect_commands) |
| 56 |
@pytest.mark.dependency(name="anta_benchmark", scope="package") |
| 57 |
@respx.mock |
| 58 |
def test_anta( |
| 59 |
benchmark: BenchmarkFixture, |
| 60 |
catalog: AntaCatalog, |
| 61 |
inventory: AntaInventory, |
| 62 |
request: pytest.FixtureRequest, |
| 63 |
session_results: defaultdict[str, ResultManager], |
| 64 |
) -> None: |
| 65 |
"""Benchmark ANTA.""" |
| 66 |
|
| 67 |
logging.disable() |
| 68 |
|
| 69 |
results = session_results[request.node.callspec.id] |
| 70 |
|
| 71 |
|
| 72 |
@benchmark |
| 73 |
def _() -> None: |
| 74 |
results.reset() |
| 75 |
catalog.clear_indexes() |
| 76 |
asyncio.run(main(results, inventory, catalog)) |
| 77 |
|
| 78 |
logging.disable(logging.NOTSET) |
| 79 |
|
| 80 |
bench_info = ( |
| 81 |
"\n--- ANTA NRFU Benchmark Information ---\n" |
| 82 |
f"Test results: {len(results.results)}\n" |
| 83 |
f"Success: {results.get_total_results({AntaTestStatus.SUCCESS})}\n" |
| 84 |
f"Failure: {results.get_total_results({AntaTestStatus.FAILURE})}\n" |
| 85 |
f"Skipped: {results.get_total_results({AntaTestStatus.SKIPPED})}\n" |
| 86 |
f"Error: {results.get_total_results({AntaTestStatus.ERROR})}\n" |
| 87 |
f"Unset: {results.get_total_results({AntaTestStatus.UNSET})}\n" |
| 88 |
"---------------------------------------" |
| 89 |
) |
| 90 |
logger.info(bench_info) |
| 91 |
assert results.get_total_results({AntaTestStatus.ERROR}) == 0 |
| 92 |
assert results.get_total_results({AntaTestStatus.UNSET}) == 0 |