File size: 1,271 Bytes
8457788
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import unittest

from profiling import Profiler, format_snapshot, resource_snapshot


class ProfilingTests(unittest.TestCase):
    def test_resource_snapshot_never_raises_and_returns_dict(self) -> None:
        snap = resource_snapshot()
        self.assertIsInstance(snap, dict)

    def test_format_snapshot_is_string(self) -> None:
        self.assertIsInstance(format_snapshot(resource_snapshot()), str)
        self.assertEqual(format_snapshot({}), "n/a")

    def test_profiler_records_stages_meta_and_summarizes(self) -> None:
        prof = Profiler("test")
        prof.record("extract", 0.012)
        prof.record("redact", 0.034)
        prof.mark(messages=4, engine="deterministic")

        self.assertEqual([name for name, _ in prof.stages], ["extract", "redact"])
        self.assertEqual(prof.meta["messages"], 4)
        self.assertGreaterEqual(prof.elapsed(), 0.0)
        prof.summary()  # must not raise

    def test_stage_context_manager_records_duration(self) -> None:
        prof = Profiler("test")
        with prof.stage("chart"):
            pass
        self.assertEqual(prof.stages[-1][0], "chart")
        self.assertGreaterEqual(prof.stages[-1][1], 0.0)


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