Files
Triforce-Source/tests/test_profile_stream.py
ruckus7382sfgsgdf 34b3683441 Triforce v0.2.0
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 19:32:48 -06:00

93 lines
4.2 KiB
Python

"""Game-free profile output and bounded input reading: python -I -B tests/test_profile_stream.py."""
import contextlib
import importlib.util
import io
import os
from pathlib import Path
import sys
import tempfile
import unittest
from unittest import mock
ROOT = Path(__file__).resolve().parents[1]
spec = importlib.util.spec_from_file_location("profile_under_test", ROOT / "scripts" / "prof.py")
prof = importlib.util.module_from_spec(spec)
spec.loader.exec_module(prof)
class IteratedFile(io.TextIOWrapper):
"""A profile may be iterated, but must not be materialized as one string or list."""
def read(self, *args):
raise AssertionError("read must not load the profile")
def readlines(self, *args):
raise AssertionError("readlines must not load the profile")
class ProfileTests(unittest.TestCase):
def capture(self, text, skip="0", only=None, maps=None):
with tempfile.TemporaryDirectory(prefix="hookshot-profile-") as folder:
if maps:
Path(folder, "hookshot.map").write_text(maps, encoding="utf-8")
source = Path(folder, "stacks.txt")
source.write_text(text, encoding="utf-8")
profile = IteratedFile(source.open("rb"), encoding="utf-8")
real_open = open
def open_input(path, *args, **kwargs):
if path == "synthetic-profile":
return profile
return real_open(path, *args, **kwargs)
args = ["prof.py", "synthetic-profile", "40", skip]
if only is not None:
args.append(only)
output = io.StringIO()
with mock.patch.object(prof, "BUILD", folder), mock.patch.object(sys, "argv", args), \
mock.patch("builtins.open", side_effect=open_input), contextlib.redirect_stdout(output):
prof.main()
self.assertTrue(profile.closed)
return output.getvalue()
def test_output_and_duplicate_frames(self):
self.assertEqual(self.capture("a.dll+1;a.dll+2;b.dll+3\na.dll+4\n\n"),
"2 samples\n-- leaf modules\n 100.0% a.dll\n-- inclusive\n"
" 100.0% a.dll\n 50.0% b.dll\n-- self\n 100.0% a.dll\n")
def test_skip_counts_physical_lines_before_filtering(self):
text = "a.dll+1\n\nb.dll+2\nc.dll+3\n"
expected = {"0.5": "2 samples", "0.75": "1 samples", "1": "0 samples", "2": "0 samples",
"-0.5": "2 samples", "-2": "3 samples", "0.1": "3 samples", "1e30": "0 samples"}
for skip, count in expected.items():
with self.subTest(skip=skip):
self.assertEqual(self.capture(text, skip).splitlines()[0], count)
self.assertIn("100.0% c.dll", self.capture(text, "0.5", "c.dll"))
self.assertTrue(self.capture(text, "0.5", "missing").startswith("0 samples\n"))
def test_empty_input(self):
for skip in ("0", "0.5"):
self.assertEqual(self.capture("", skip), "0 samples\n-- leaf modules\n-- inclusive\n-- self\n")
def test_skip_on_a_pipe_preserves_physical_line_positions(self):
for skip, expected in ((0, ["a.dll+1\n", "\n", "b.dll+2\n", "c.dll+3"]),
(0.5, ["b.dll+2\n", "c.dll+3"]), (-0.25, ["c.dll+3"])):
with self.subTest(skip=skip):
reader, writer = os.pipe()
with os.fdopen(reader, encoding="utf-8") as profile:
with os.fdopen(writer, "wb") as output:
output.write(b"a.dll+1\r\n\r\nb.dll+2\r\nc.dll+3")
with mock.patch.object(profile, "seekable", return_value=True), \
mock.patch.object(prof, "open", return_value=profile, create=True):
self.assertEqual(list(prof.profile_lines("synthetic-pipe", skip)), expected)
def test_symbol_resolution_and_filter(self):
maps = " 0001:00000000 ?draw@renderer@@ 0000000140000010 f\n"
output = self.capture("hookshot.exe+10;driver.dll+1\nother.dll+2\n", only="renderer::draw", maps=maps)
self.assertTrue(output.startswith("1 samples\n"))
self.assertIn("100.0% hookshot:renderer::draw", output)
self.assertNotIn("other.dll", output)
if __name__ == "__main__":
unittest.main()