xref: /llvm-project/lldb/test/API/functionalities/stats_api/TestStatisticsAPI.py (revision 76706090c2f672ae933798292bfa889f9e3dac3d)
1# Test the SBAPI for GetStatistics()
2
3import json
4import lldb
5from lldbsuite.test.decorators import *
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test import lldbutil
8
9
10class TestStatsAPI(TestBase):
11    NO_DEBUG_INFO_TESTCASE = True
12
13    def test_stats_api(self):
14        """
15        Test SBTarget::GetStatistics() API.
16        """
17        self.build()
18        exe = self.getBuildArtifact("a.out")
19        target = self.dbg.CreateTarget(exe)
20
21        # Test enabling/disabling stats
22        self.assertFalse(target.GetCollectingStats())
23        target.SetCollectingStats(True)
24        self.assertTrue(target.GetCollectingStats())
25        target.SetCollectingStats(False)
26        self.assertFalse(target.GetCollectingStats())
27
28        # Test the function to get the statistics in JSON'ish.
29        stats = target.GetStatistics()
30        stream = lldb.SBStream()
31        res = stats.GetAsJSON(stream)
32        debug_stats = json.loads(stream.GetData())
33        self.assertEqual(
34            "targets" in debug_stats,
35            True,
36            'Make sure the "targets" key in in target.GetStatistics()',
37        )
38        self.assertEqual(
39            "modules" in debug_stats,
40            True,
41            'Make sure the "modules" key in in target.GetStatistics()',
42        )
43        stats_json = debug_stats["targets"][0]
44        self.assertEqual(
45            "expressionEvaluation" in stats_json,
46            True,
47            'Make sure the "expressionEvaluation" key in in target.GetStatistics()["targets"][0]',
48        )
49        self.assertEqual(
50            "frameVariable" in stats_json,
51            True,
52            'Make sure the "frameVariable" key in in target.GetStatistics()["targets"][0]',
53        )
54        expressionEvaluation = stats_json["expressionEvaluation"]
55        self.assertEqual(
56            "successes" in expressionEvaluation,
57            True,
58            'Make sure the "successes" key in in "expressionEvaluation" dictionary"',
59        )
60        self.assertEqual(
61            "failures" in expressionEvaluation,
62            True,
63            'Make sure the "failures" key in in "expressionEvaluation" dictionary"',
64        )
65        frameVariable = stats_json["frameVariable"]
66        self.assertEqual(
67            "successes" in frameVariable,
68            True,
69            'Make sure the "successes" key in in "frameVariable" dictionary"',
70        )
71        self.assertEqual(
72            "failures" in frameVariable,
73            True,
74            'Make sure the "failures" key in in "frameVariable" dictionary"',
75        )
76
77    def test_command_stats_api(self):
78        """
79        Test GetCommandInterpreter::GetStatistics() API.
80        """
81        self.build()
82        exe = self.getBuildArtifact("a.out")
83        lldbutil.run_to_name_breakpoint(self, "main")
84
85        interp = self.dbg.GetCommandInterpreter()
86        result = lldb.SBCommandReturnObject()
87        interp.HandleCommand("bt", result)
88
89        stream = lldb.SBStream()
90        res = interp.GetStatistics().GetAsJSON(stream)
91        command_stats = json.loads(stream.GetData())
92
93        # Verify bt command is correctly parsed into final form.
94        self.assertEqual(command_stats["thread backtrace"], 1)
95        # Verify original raw command is not duplicatedly captured.
96        self.assertNotIn("bt", command_stats)
97        # Verify bt's regex command is not duplicatedly captured.
98        self.assertNotIn("_regexp-bt", command_stats)
99