|
| 1 | +"""Tests for per-batch ingestion progress/throughput metrics (Issue #727).""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import time |
| 6 | +from unittest.mock import MagicMock |
| 7 | + |
| 8 | +import pytest |
| 9 | +from prometheus_client import REGISTRY |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture(autouse=True) |
| 13 | +def _reset_batch_metrics(): |
| 14 | + """Reset batch metric counters before each test so tests are order-independent.""" |
| 15 | + INGESTION_BATCH_LEDGERS.labels(status="processed")._value.set(0.0) |
| 16 | + INGESTION_BATCH_LEDGERS.labels(status="skipped")._value.set(0.0) |
| 17 | + INGESTION_BATCH_LEDGERS.labels(status="error")._value.set(0.0) |
| 18 | + INGESTION_BATCH_THROUGHPUT._value.set(0.0) |
| 19 | + INGESTION_BATCH_DURATION_SECONDS._sum.set(0.0) |
| 20 | + |
| 21 | +from astroml.ingestion.batch_metrics import BatchMetricsRecorder |
| 22 | +from astroml.ingestion.metrics import ( |
| 23 | + INGESTION_BATCH_DURATION_SECONDS, |
| 24 | + INGESTION_BATCH_LEDGERS, |
| 25 | + INGESTION_BATCH_THROUGHPUT, |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +class _Outcome: |
| 30 | + def __init__(self, status: str) -> None: |
| 31 | + self.status = status |
| 32 | + |
| 33 | + |
| 34 | +class TestBatchMetricsRecorder: |
| 35 | + def test_records_processed_and_skipped(self) -> None: |
| 36 | + recorder = BatchMetricsRecorder() |
| 37 | + recorder.start() |
| 38 | + recorder.observe(_Outcome("processed")) |
| 39 | + recorder.observe(_Outcome("processed")) |
| 40 | + recorder.observe(_Outcome("skipped")) |
| 41 | + recorder.finish() |
| 42 | + |
| 43 | + assert ( |
| 44 | + INGESTION_BATCH_LEDGERS.labels(status="processed")._value.get() == 2.0 |
| 45 | + ) |
| 46 | + assert INGESTION_BATCH_LEDGERS.labels(status="skipped")._value.get() == 1.0 |
| 47 | + |
| 48 | + def test_records_error_outcome(self) -> None: |
| 49 | + recorder = BatchMetricsRecorder() |
| 50 | + recorder.start() |
| 51 | + recorder.observe(_Outcome("error")) |
| 52 | + recorder.finish() |
| 53 | + |
| 54 | + assert INGESTION_BATCH_LEDGERS.labels(status="error")._value.get() == 1.0 |
| 55 | + |
| 56 | + def test_throughput_is_non_negative(self) -> None: |
| 57 | + recorder = BatchMetricsRecorder() |
| 58 | + recorder.start() |
| 59 | + recorder.observe(_Outcome("processed")) |
| 60 | + recorder.finish() |
| 61 | + |
| 62 | + throughput = INGESTION_BATCH_THROUGHPUT._value.get() |
| 63 | + assert throughput >= 0.0 |
| 64 | + |
| 65 | + def test_duration_observed(self) -> None: |
| 66 | + recorder = BatchMetricsRecorder() |
| 67 | + recorder.start() |
| 68 | + time.sleep(0.01) |
| 69 | + recorder.finish() |
| 70 | + |
| 71 | + # Histogram exposes samples through _sum and _count |
| 72 | + assert INGESTION_BATCH_DURATION_SECONDS._sum.get() >= 0.01 |
| 73 | + |
| 74 | + def test_start_resets_counters(self) -> None: |
| 75 | + recorder = BatchMetricsRecorder() |
| 76 | + recorder.start() |
| 77 | + recorder.observe(_Outcome("processed")) |
| 78 | + recorder.finish() |
| 79 | + |
| 80 | + recorder.start() |
| 81 | + recorder.finish() |
| 82 | + |
| 83 | + # The second batch has no new processed observations, so the counter |
| 84 | + # should still be 1.0 (counters are monotonic). |
| 85 | + assert INGESTION_BATCH_LEDGERS.labels(status="processed")._value.get() == 1.0 |
| 86 | + |
| 87 | + |
| 88 | +class TestIngestionServiceBatchMetrics: |
| 89 | + def test_emits_batch_metrics_after_batch_boundary(self, tmp_path) -> None: |
| 90 | + from astroml.ingestion.service import IngestionService |
| 91 | + from astroml.ingestion.state import StateStore |
| 92 | + |
| 93 | + state_path = tmp_path / "state.json" |
| 94 | + store = StateStore(str(state_path)) |
| 95 | + service = IngestionService(store) |
| 96 | + |
| 97 | + list(service.ingest_stream(start_ledger=1, end_ledger=5, batch_size=5)) |
| 98 | + |
| 99 | + assert INGESTION_BATCH_LEDGERS.labels(status="processed")._value.get() == 5.0 |
| 100 | + assert INGESTION_BATCH_THROUGHPUT._value.get() >= 0.0 |
| 101 | + |
| 102 | + def test_emits_batch_metrics_for_partial_final_batch(self, tmp_path) -> None: |
| 103 | + from astroml.ingestion.service import IngestionService |
| 104 | + from astroml.ingestion.state import StateStore |
| 105 | + |
| 106 | + state_path = tmp_path / "state.json" |
| 107 | + store = StateStore(str(state_path)) |
| 108 | + service = IngestionService(store) |
| 109 | + |
| 110 | + list(service.ingest_stream(start_ledger=1, end_ledger=3, batch_size=5)) |
| 111 | + |
| 112 | + assert INGESTION_BATCH_LEDGERS.labels(status="processed")._value.get() == 3.0 |
0 commit comments