File size: 7,912 Bytes
ea8c728
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
216
217
218
import matplotlib
import pandas as pd
import pytest

matplotlib.use("Agg")

from shinka.plots import (
    plot_generation_runtime_timeline,
    plot_normalized_occupancy_over_time,
)
from shinka.plots.plot_throughput import (
    _compute_occupancy_series,
    _prepare_pool_runtime_data,
)


def _runtime_df() -> pd.DataFrame:
    return pd.DataFrame(
        [
            {
                "id": "job-a-main",
                "source_job_id": "job-a",
                "is_island_copy": False,
                "correct": True,
                "combined_score": 0.9,
                "timestamp": 10,
                "generation": 1,
                "patch_name": "patch-a",
                "model_name": "model-a",
                "timeline_lane_mode": "pool_slots",
                "pipeline_started_at": 0,
                "sampling_started_at": 0,
                "sampling_finished_at": 2,
                "evaluation_started_at": 2,
                "evaluation_finished_at": 6,
                "postprocess_started_at": 6,
                "postprocess_finished_at": 8,
                "sampling_worker_id": 1,
                "evaluation_worker_id": 1,
                "postprocess_worker_id": 1,
                "sampling_worker_capacity": 2,
                "evaluation_worker_capacity": 2,
                "postprocess_worker_capacity": 1,
            },
            {
                "id": "job-a-copy",
                "source_job_id": "job-a",
                "is_island_copy": True,
                "correct": False,
                "combined_score": 0.1,
                "timestamp": 11,
                "generation": 1,
                "patch_name": "patch-a-copy",
                "model_name": "model-a",
                "timeline_lane_mode": "pool_slots",
                "pipeline_started_at": 0,
                "sampling_started_at": 0,
                "sampling_finished_at": 2,
                "evaluation_started_at": 2,
                "evaluation_finished_at": 6,
                "postprocess_started_at": 6,
                "postprocess_finished_at": 8,
                "sampling_worker_id": 1,
                "evaluation_worker_id": 1,
                "postprocess_worker_id": 1,
                "sampling_worker_capacity": 2,
                "evaluation_worker_capacity": 2,
                "postprocess_worker_capacity": 1,
            },
            {
                "id": "job-b",
                "source_job_id": "job-b",
                "is_island_copy": False,
                "correct": True,
                "combined_score": 0.8,
                "timestamp": 20,
                "generation": 2,
                "patch_name": "patch-b",
                "model_name": "model-b",
                "timeline_lane_mode": "pool_slots",
                "pipeline_started_at": 1,
                "sampling_started_at": 1,
                "sampling_finished_at": 3,
                "evaluation_started_at": 4,
                "evaluation_finished_at": 8,
                "postprocess_started_at": 8,
                "postprocess_finished_at": 10,
                "sampling_worker_id": 2,
                "evaluation_worker_id": 2,
                "postprocess_worker_id": 1,
                "sampling_worker_capacity": 2,
                "evaluation_worker_capacity": 2,
                "postprocess_worker_capacity": 1,
            },
            {
                "id": "job-missing",
                "source_job_id": "job-missing",
                "is_island_copy": False,
                "correct": True,
                "combined_score": 0.7,
                "timestamp": 30,
                "generation": 3,
                "patch_name": "patch-missing",
                "model_name": "model-c",
                "timeline_lane_mode": "pool_slots",
                "pipeline_started_at": 2,
                "sampling_started_at": 2,
                "sampling_finished_at": 4,
                "evaluation_started_at": 5,
                "evaluation_finished_at": 9,
                "postprocess_started_at": 9,
                "postprocess_finished_at": None,
                "sampling_worker_id": 1,
                "evaluation_worker_id": 1,
                "postprocess_worker_id": 1,
                "sampling_worker_capacity": 2,
                "evaluation_worker_capacity": 2,
                "postprocess_worker_capacity": 1,
            },
        ]
    )


def test_prepare_pool_runtime_data_dedupes_rows_and_computes_capacities():
    prepared = _prepare_pool_runtime_data(_runtime_df())

    assert prepared is not None
    assert prepared.capacities == {"sampling": 2, "evaluation": 2, "postprocess": 1}
    assert prepared.lane_labels == [
        "Sampling W1",
        "Evaluation W1",
        "Postprocess W1",
        "Sampling W2",
        "Evaluation W2",
    ]
    assert list(prepared.rows["id"]) == ["job-a-main", "job-b"]
    assert prepared.peaks == {"sampling": 2, "evaluation": 2, "postprocess": 1}


def test_prepare_pool_runtime_data_handles_missing_optional_columns():
    runtime_df = _runtime_df().drop(
        columns=["source_job_id", "is_island_copy", "patch_name", "model_name"]
    )

    prepared = _prepare_pool_runtime_data(runtime_df)

    assert prepared is not None
    assert list(prepared.rows["id"]) == ["job-a-main", "job-a-copy", "job-b"]
    assert list(prepared.rows["source_job_id"]) == ["job-a-main", "job-a-copy", "job-b"]
    assert list(prepared.rows["patch_name"]) == ["unnamed", "unnamed", "unnamed"]
    assert list(prepared.rows["model_name"]) == ["N/A", "N/A", "N/A"]
    assert prepared.capacities == {"sampling": 2, "evaluation": 2, "postprocess": 1}


def test_compute_occupancy_series_matches_expected_utilization_stats():
    prepared = _prepare_pool_runtime_data(_runtime_df())
    assert prepared is not None

    series = _compute_occupancy_series(
        prepared.rows,
        start_key="evaluation_started_at",
        end_key="evaluation_finished_at",
        capacity=prepared.capacities["evaluation"],
    )

    assert series is not None
    assert series.total_duration == pytest.approx(6.0)
    assert series.avg_occupied == pytest.approx(8.0 / 6.0)
    assert series.utilization_pct == pytest.approx((8.0 / 12.0) * 100.0)
    assert series.full_occupancy_pct == pytest.approx((2.0 / 6.0) * 100.0)
    assert series.idle_pct == pytest.approx(0.0)


def test_plot_generation_runtime_timeline_uses_deduped_pool_rows():
    fig, ax = plot_generation_runtime_timeline(_runtime_df(), title="Runtime Timeline")

    assert fig is not None
    assert ax is not None
    assert [tick.get_text() for tick in ax.get_yticklabels()] == [
        "Sampling W1",
        "Evaluation W1",
        "Postprocess W1",
        "Sampling W2",
        "Evaluation W2",
    ]
    assert len(ax.patches) == 6
    assert {text.get_text() for text in ax.get_legend().get_texts()} == {
        "Sampling",
        "Evaluation",
        "Postprocess",
    }
    assert ax.get_legend()._ncols == 3
    assert ax.get_legend()._loc == 9
    assert {text.get_fontsize() for text in ax.get_legend().get_texts()} == {10.0}
    assert ax.get_legend().get_bbox_to_anchor()._bbox.y0 < 0


def test_plot_normalized_occupancy_over_time_adds_reference_line():
    fig, ax = plot_normalized_occupancy_over_time(
        _runtime_df(), title="Normalized Occupancy"
    )

    assert fig is not None
    assert ax is not None
    labels = [line.get_label() for line in ax.lines]
    assert labels == [
        "Sampling Occupancy",
        "Evaluation Occupancy",
        "Postprocess Occupancy",
        "100% Capacity",
    ]
    assert ax.get_ylim()[1] >= 100
    assert list(ax.lines[-1].get_ydata()) == [100, 100]
    assert ax.get_legend()._ncols == 2
    assert ax.get_legend()._loc == 9
    assert {text.get_fontsize() for text in ax.get_legend().get_texts()} == {10.0}
    assert ax.get_legend().get_bbox_to_anchor()._bbox.y0 < 0