aboutsummaryrefslogtreecommitdiffziptar.gz
path: root/scripts/test_thread_cpu.py
blob: f60a2d0228ebd88d9b41639ae51996f769f9c8d5 (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
import sys
import unittest
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))
import thread_cpu as tc


class ThreadCpuTests(unittest.TestCase):
    def test_groups_by_name_and_counts_new_threads_from_zero(self):
        before = {"1": ("main", 100), "2": ("worker", 10)}
        after = {"1": ("main", 150), "2": ("worker", 20), "3": ("worker", 30)}
        rows = tc.per_name(before, after, seconds=1.0, hz=100)
        self.assertEqual(rows[0], ("main", 50.0, 1))
        self.assertEqual(rows[1], ("worker", 40.0, 2))  # 10 + 30 ticks

    def test_samples_this_process(self):
        import os
        threads = tc.sample(os.getpid())
        self.assertIn(str(os.getpid()), threads)


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