upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/scripts/test_faultbook.py
blob: 1d65b6b3e4138e856ce5c69bcd5402e39b6b8706 (plain)
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
"""Regression tests for the fault-book report, with no game files."""

from contextlib import redirect_stdout
from io import StringIO
from pathlib import Path
from tempfile import TemporaryDirectory
import unittest

from faultbook import RECORD, book


class FaultBookTest(unittest.TestCase):
    def report(self, blocks_per_frame, extra_faults=()):
        with TemporaryDirectory() as temp:
            path = Path(temp) / "capture.bin"
            with path.open("wb") as capture:
                for frame, blocks in enumerate(blocks_per_frame):
                    began = frame * 1000
                    for block in blocks:
                        capture.write(RECORD.pack(began + 100, block, 0, 0, 1, 1))
                    capture.write(RECORD.pack(began + 1000, frame, 0, 0, 1, 0))
                for offset, block in extra_faults:
                    at = len(blocks_per_frame) * 1000 + offset
                    capture.write(RECORD.pack(at, block, 0, 0, 1, 1))
            out = StringIO()
            with redirect_stdout(out):
                book(path, 1_000_000)
        return out.getvalue()

    def test_rates_use_frame_count_but_timing_uses_marker_intervals(self):
        text = self.report([range(10)] * 40)
        self.assertIn("frames: 40 over 39.0 ms (1.0 ms a frame)", text)
        self.assertIn("faults under the guards' lock, per frame 10.0", text)

    def test_hot_blocks_must_fault_in_at_least_three_quarters_of_frames(self):
        text = self.report([[1, 2], [1, 2], [1, 2], [2], []])
        self.assertIn("blocks faulting in three frames of four or more: 1,", text)

    def test_faults_at_or_after_the_last_marker_do_not_count(self):
        for offset in (0, 100):
            with self.subTest(offset=offset):
                text = self.report([[1], [1], [], []], extra_faults=[(offset, 1)])
                self.assertIn(": 7 records\n", text)
                self.assertIn("faults under the guards' lock, per frame 0.5", text)
                self.assertIn("records per frame: frame 1.0, fault 0.5", text)
                self.assertIn("blocks faulting in three frames of four or more: 0,", text)

    def test_faults_without_frame_markers_still_count(self):
        text = self.report([], extra_faults=[(100, 1)])
        self.assertIn("records per frame: fault 1.0", text)


if __name__ == "__main__":
    unittest.main()