import json import subprocess import unittest from pathlib import Path ROOT = Path(__file__).resolve().parents[2] SNAPSHOT = ROOT / "snapshots" / "js-ordered-consume.dtp" SCRIPT = ROOT / "tools" / "dtp_parse.py" def walk(node): yield node for child in node.get("children", []): yield from walk(child) class DtpParserTests(unittest.TestCase): def run_parser(self, *args: str) -> dict: result = subprocess.run( ["python3", str(SCRIPT), str(SNAPSHOT), "--stdout", *args], cwd=ROOT, capture_output=True, text=True, check=False, ) self.assertEqual(result.returncode, 0, msg=result.stderr) return json.loads(result.stdout) def test_emits_machine_readable_call_tree(self): payload = self.run_parser() self.assertIn("callTree", payload) self.assertIn("hotspots", payload) self.assertIn("summary", payload) self.assertTrue(payload["callTree"]["children"]) self.assertTrue(payload["hotspots"]["inclusive"]) self.assertEqual(payload["snapshot"]["timeUnit"], "nanoseconds") self.assertLessEqual(len(payload["hotspots"]["inclusive"]), 200) self.assertLessEqual(len(payload["hotspots"]["exclusive"]), 200) self.assertIn("wallTimeMs", payload["summary"]) self.assertIn("activeTimeMs", payload["summary"]) self.assertIn("totalSamples", payload["summary"]) self.assertIn("topExclusiveMethod", payload["summary"]) node_names = [node["name"] for node in walk(payload["callTree"])] self.assertTrue(any(not name.startswith("[special:") for name in node_names)) def test_supports_hotspot_filter_and_flat_paths(self): payload = self.run_parser("--top", "7", "--filter", "Microsoft.DotNet.Cli.Program", "--flat") self.assertLessEqual(len(payload["hotspots"]["inclusive"]), 7) self.assertLessEqual(len(payload["hotspots"]["exclusive"]), 7) self.assertIn("hotPaths", payload) node_names = [node["name"] for node in walk(payload["callTree"])] self.assertTrue(any("Microsoft.DotNet.Cli.Program" in name for name in node_names)) self.assertFalse(any("Microsoft.Build.Tasks.Copy.ParallelCopyTask" == name for name in node_names)) for hotspot in payload["hotspots"]["inclusive"] + payload["hotspots"]["exclusive"]: self.assertIn("Microsoft.DotNet.Cli.Program", hotspot["name"]) for path_entry in payload["hotPaths"]: self.assertIn("Microsoft.DotNet.Cli.Program", path_entry["path"]) def test_can_include_idle_hotspots_when_requested(self): without_idle = self.run_parser("--top", "20") with_idle = self.run_parser("--top", "20", "--include-idle") without_idle_names = {entry["name"] for entry in without_idle["hotspots"]["exclusive"]} with_idle_names = {entry["name"] for entry in with_idle["hotspots"]["exclusive"]} self.assertNotIn("System.Threading.WaitHandle.WaitOneNoCheck", without_idle_names) self.assertIn("System.Threading.WaitHandle.WaitOneNoCheck", with_idle_names) if __name__ == "__main__": unittest.main()