Skip to content

pytest_park.core.history

[docs] module pytest_park.core.history

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from __future__ import annotations

from collections import defaultdict

from pytest_park.models import (
    BenchmarkCase,
    BenchmarkRun,
    MethodHistoryComparison,
    MethodHistoryPoint,
    PriorRunComparison,
    TrendPoint,
)


class HistoryAnalyzer:
    """Analyzes benchmark performance history and trends across multiple runs."""

    def __init__(self, runs: list[BenchmarkRun]) -> None:
        self.runs = runs

    def build_trends(self) -> dict[str, list[TrendPoint]]:
        """Build time-series means per case across run history."""
        series: dict[str, list[TrendPoint]] = defaultdict(list)
        for run in self.runs:
            for case in run.cases:
                series[case.case_key].append(
                    TrendPoint(run_id=run.run_id, timestamp=run.created_at, mean=case.stats.mean)
                )
        for points in series.values():
            points.sort(key=lambda item: (item.timestamp is None, item.timestamp, item.run_id))
        return dict(series)

    def build_method_history(
        self,
        method: str,
        distinct_params: list[str] | None = None,
    ) -> list[MethodHistoryPoint]:
        """Build method mean history across runs."""
        history: list[MethodHistoryPoint] = []
        for run in self.runs:
            method_cases = [case for case in run.cases if case.normalized_name == method]
            if not method_cases:
                continue

            groups: dict[str, list[BenchmarkCase]] = defaultdict(list)
            for case in method_cases:
                groups[_distinct_label(case, distinct_params)].append(case)

            for distinct_label_val, cases in groups.items():
                means = [case.stats.mean for case in cases]
                history.append(
                    MethodHistoryPoint(
                        run_id=run.run_id,
                        timestamp=run.created_at.isoformat() if run.created_at else None,
                        method=method,
                        distinct=distinct_label_val,
                        mean=sum(means) / len(means),
                    )
                )

        history.sort(key=lambda item: (item.timestamp is None, item.timestamp, item.run_id, item.distinct))
        return history

    def compare_to_reference(
        self,
        reference_run: BenchmarkRun,
        method: str,
        distinct_params: list[str] | None = None,
    ) -> list[MethodHistoryComparison]:
        """Compare method mean over runs against a fixed reference run mean."""
        reference_history = HistoryAnalyzer([reference_run]).build_method_history(method, distinct_params)
        reference_by_distinct = {point.distinct: point.mean for point in reference_history}

        compared: list[MethodHistoryComparison] = []
        for point in self.build_method_history(method, distinct_params):
            baseline = reference_by_distinct.get(point.distinct)
            if baseline is None or baseline <= 0:
                continue
            current = point.mean
            delta_pct = ((current - baseline) / baseline) * 100.0
            compared.append(
                MethodHistoryComparison(
                    run_id=point.run_id,
                    timestamp=point.timestamp,
                    method=point.method,
                    distinct=point.distinct,
                    mean=current,
                    reference_mean=baseline,
                    delta_pct=delta_pct,
                    speedup=baseline / current if current > 0 else 0.0,
                )
            )

        return compared

    def compare_to_all_prior(
        self,
        candidate_run: BenchmarkRun,
        method: str,
        distinct_params: list[str] | None = None,
    ) -> list[PriorRunComparison]:
        """Compare candidate method means against every prior run in history."""
        candidate_index = _method_mean_index(candidate_run, method, distinct_params)
        if not candidate_index:
            return []

        candidate_position = _run_index(self.runs, candidate_run)
        compared: list[PriorRunComparison] = []

        for reference_run in self.runs[:candidate_position]:
            reference_index = _method_mean_index(reference_run, method, distinct_params)
            if not reference_index:
                continue

            for distinct, candidate_mean in candidate_index.items():
                reference_mean = reference_index.get(distinct)
                if reference_mean is None or reference_mean <= 0:
                    continue
                delta_pct = ((candidate_mean - reference_mean) / reference_mean) * 100.0
                compared.append(
                    PriorRunComparison(
                        method=method,
                        candidate_run_id=candidate_run.run_id,
                        reference_run_id=reference_run.run_id,
                        distinct=distinct,
                        mean=candidate_mean,
                        reference_mean=reference_mean,
                        delta_pct=delta_pct,
                        speedup=reference_mean / candidate_mean if candidate_mean > 0 else 0.0,
                        reference_timestamp=(
                            reference_run.created_at.isoformat() if reference_run.created_at else None
                        ),
                    )
                )

        compared.sort(
            key=lambda item: (
                item.reference_timestamp is None,
                item.reference_timestamp,
                item.reference_run_id,
                item.distinct,
            )
        )
        return compared


# ---------------------------------------------------------------------------
# Module-level convenience functions
# ---------------------------------------------------------------------------


def build_trends(runs: list[BenchmarkRun]) -> dict[str, list[TrendPoint]]:
    """Build time-series means per case across run history."""
    return HistoryAnalyzer(runs).build_trends()


def build_method_history(
    runs: list[BenchmarkRun],
    method: str,
    distinct_params: list[str] | None = None,
) -> list[MethodHistoryPoint]:
    """Build method mean history across runs."""
    return HistoryAnalyzer(runs).build_method_history(method, distinct_params)


def compare_method_history_to_reference(
    runs: list[BenchmarkRun],
    reference_run: BenchmarkRun,
    method: str,
    distinct_params: list[str] | None = None,
) -> list[MethodHistoryComparison]:
    """Compare method mean over runs against reference run mean."""
    return HistoryAnalyzer(runs).compare_to_reference(reference_run, method, distinct_params)


def compare_method_to_all_prior_runs(
    runs: list[BenchmarkRun],
    candidate_run: BenchmarkRun,
    method: str,
    distinct_params: list[str] | None = None,
) -> list[PriorRunComparison]:
    """Compare candidate method means against all prior runs."""
    return HistoryAnalyzer(runs).compare_to_all_prior(candidate_run, method, distinct_params)


# ---------------------------------------------------------------------------
# Private helpers
# ---------------------------------------------------------------------------


def _distinct_label(case: BenchmarkCase, distinct_params: list[str] | None) -> str:
    if not distinct_params:
        return "all"
    bits = [f"{key}={case.params.get(key, 'n/a')}" for key in distinct_params]
    return ",".join(bits)


def _run_index(runs: list[BenchmarkRun], selected_run: BenchmarkRun) -> int:
    for index, run in enumerate(runs):
        if run.run_id == selected_run.run_id:
            return index
    raise ValueError(f"Run not found in run history: {selected_run.run_id}")


def _method_mean_index(
    run: BenchmarkRun,
    method: str,
    distinct_params: list[str] | None,
) -> dict[str, float]:
    groups: dict[str, list[float]] = defaultdict(list)
    for case in run.cases:
        if case.normalized_name != method:
            continue
        groups[_distinct_label(case, distinct_params)].append(case.stats.mean)
    return {label: sum(values) / len(values) for label, values in groups.items() if values}