aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_stats.py
blob: 81acdbacf897436ff644b1cb953543a0ec35040a (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
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

import unittest

import deckit.modules.stats as stats


class Stats(unittest.TestCase):
    def test_state_shape(self):
        st = stats.make({}).state()
        for key in ("cpu", "memUsed", "memTotal", "net", "uptime", "host"):
            self.assertIn(key, st)
        self.assertIsInstance(st["cpu"], int)

    def test_build_discovers_stats(self):
        from deckit.modules import build
        names = [m["name"] for m in build({"modules": {}}).names()]
        self.assertIn("stats", names)


class Intel(unittest.TestCase):
    def test_parse_bytes(self):
        self.assertEqual(stats._parse_bytes("12G"), 12 * 1024 ** 3)
        self.assertEqual(stats._parse_bytes("512M"), 512 * 1024 ** 2)
        self.assertEqual(stats._parse_bytes(2048), 2048)
        self.assertIsNone(stats._parse_bytes("nope"))

    def test_vram_scan_counts_each_client_once(self):
        import tempfile
        root = Path(tempfile.mkdtemp())
        rows = [
            ("100", "3", "drm-driver:\txe\ndrm-client-id:\t7\ndrm-resident-vram0:\t1024 MiB\n"),
            ("100", "4", "drm-driver:\txe\ndrm-client-id:\t7\ndrm-resident-vram0:\t1024 MiB\n"),
            ("200", "5", "drm-driver:\txe\ndrm-client-id:\t8\ndrm-resident-vram0:\t512 MiB\n"),
            ("300", "6", "drm-driver:\ti915\ndrm-client-id:\t9\ndrm-resident-vram0:\t999 MiB\n"),
        ]
        for pid, fd, body in rows:
            d = root / pid / "fdinfo"
            d.mkdir(parents=True, exist_ok=True)
            (d / fd).write_text(body)
        self.assertEqual(stats._intel_vram_used(root), (1024 + 512) * 1024 ** 2)


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