Files
tracy/extra/mcp/test_thread_zone_stats.py
Alan TseandClaude Sonnet 5.5 bbb4eebc61 feat(mcp): get zone stats for a single thread
get_all_zone_stats mixes every thread, so the main-thread cost of a
zone could not be separated from worker threads. get_thread_zone_stats
returns the same per-name stats for the zones recorded on one thread,
and its per-thread sums match get_all_zone_stats exactly.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L3DnWkXakGRgz9C8BaLM47
2026-10-27 10:04:47 -07:00

38 lines
1.1 KiB
Python

"""Needs a saved trace:
TRACY_TEST_TRACE=/path/to.tracy python -m pytest extra/mcp/test_thread_zone_stats.py
"""
import os
import time
import pytest
import tracy_mcp
TRACE = os.environ.get("TRACY_TEST_TRACE")
pytestmark = pytest.mark.skipif(not TRACE, reason="TRACY_TEST_TRACE not set")
@pytest.fixture(scope="module")
def worker():
server = tracy_mcp.tracy_server
w = server.create_worker_from_file(server.open_file(TRACE))
while not w.is_background_done():
time.sleep(0.1)
yield w
w.shutdown()
def test_threads_partition_the_global_stats(worker):
counts, totals = {}, {}
for thread in worker.get_threads():
for name, stats in worker.get_thread_zone_stats(thread["id"]).items():
counts[name] = counts.get(name, 0) + stats.count
totals[name] = totals.get(name, 0) + stats.total
for name, stats in worker.get_all_zone_stats().items():
assert counts.get(name, 0) == stats.count
assert totals.get(name, 0) == stats.total
def test_unknown_thread_is_empty(worker):
assert worker.get_thread_zone_stats(2**40) == {}