@@ -1823,3 +1823,131 @@ def test_gecko_collector_frame_format(self):
18231823 thread = profile ["threads" ][0 ]
18241824 # Should have recorded 3 functions
18251825 self .assertEqual (thread ["funcTable" ]["length" ], 3 )
1826+
1827+
1828+ class TestInternalFrameFiltering (unittest .TestCase ):
1829+ """Tests for filtering internal profiler frames from output."""
1830+
1831+ def test_filter_internal_frames (self ):
1832+ """Test that _sync_coordinator frames are filtered from anywhere in stack."""
1833+ from profiling .sampling .collector import filter_internal_frames
1834+
1835+ # Stack with _sync_coordinator in the middle (realistic scenario)
1836+ frames = [
1837+ MockFrameInfo ("user_script.py" , 10 , "user_func" ),
1838+ MockFrameInfo ("/path/to/_sync_coordinator.py" , 100 , "main" ),
1839+ MockFrameInfo ("<frozen runpy>" , 87 , "_run_code" ),
1840+ ]
1841+
1842+ filtered = filter_internal_frames (frames )
1843+ self .assertEqual (len (filtered ), 2 )
1844+ self .assertEqual (filtered [0 ].filename , "user_script.py" )
1845+ self .assertEqual (filtered [1 ].filename , "<frozen runpy>" )
1846+
1847+ def test_pstats_collector_filters_internal_frames (self ):
1848+ """Test that PstatsCollector filters out internal frames."""
1849+ collector = PstatsCollector (sample_interval_usec = 1000 )
1850+
1851+ frames = [
1852+ MockInterpreterInfo (
1853+ 0 ,
1854+ [
1855+ MockThreadInfo (
1856+ 1 ,
1857+ [
1858+ MockFrameInfo ("user_script.py" , 10 , "user_func" ),
1859+ MockFrameInfo ("/path/to/_sync_coordinator.py" , 100 , "main" ),
1860+ MockFrameInfo ("<frozen runpy>" , 87 , "_run_code" ),
1861+ ],
1862+ status = THREAD_STATUS_HAS_GIL ,
1863+ )
1864+ ],
1865+ )
1866+ ]
1867+ collector .collect (frames )
1868+
1869+ self .assertEqual (len (collector .result ), 2 )
1870+ self .assertIn (("user_script.py" , 10 , "user_func" ), collector .result )
1871+ self .assertIn (("<frozen runpy>" , 87 , "_run_code" ), collector .result )
1872+
1873+ def test_gecko_collector_filters_internal_frames (self ):
1874+ """Test that GeckoCollector filters out internal frames."""
1875+ collector = GeckoCollector (sample_interval_usec = 1000 )
1876+
1877+ frames = [
1878+ MockInterpreterInfo (
1879+ 0 ,
1880+ [
1881+ MockThreadInfo (
1882+ 1 ,
1883+ [
1884+ MockFrameInfo ("app.py" , 50 , "run" ),
1885+ MockFrameInfo ("/lib/_sync_coordinator.py" , 100 , "main" ),
1886+ ],
1887+ status = THREAD_STATUS_HAS_GIL ,
1888+ )
1889+ ],
1890+ )
1891+ ]
1892+ collector .collect (frames )
1893+
1894+ profile = collector ._build_profile ()
1895+ string_array = profile ["shared" ]["stringArray" ]
1896+
1897+ # Should not contain _sync_coordinator functions
1898+ for s in string_array :
1899+ self .assertNotIn ("_sync_coordinator" , s )
1900+
1901+ def test_flamegraph_collector_filters_internal_frames (self ):
1902+ """Test that FlamegraphCollector filters out internal frames."""
1903+ collector = FlamegraphCollector (sample_interval_usec = 1000 )
1904+
1905+ frames = [
1906+ MockInterpreterInfo (
1907+ 0 ,
1908+ [
1909+ MockThreadInfo (
1910+ 1 ,
1911+ [
1912+ MockFrameInfo ("app.py" , 50 , "run" ),
1913+ MockFrameInfo ("/lib/_sync_coordinator.py" , 100 , "main" ),
1914+ MockFrameInfo ("<frozen runpy>" , 87 , "_run_code" ),
1915+ ],
1916+ status = THREAD_STATUS_HAS_GIL ,
1917+ )
1918+ ],
1919+ )
1920+ ]
1921+ collector .collect (frames )
1922+
1923+ data = collector ._convert_to_flamegraph_format ()
1924+ strings = data .get ("strings" , [])
1925+
1926+ for s in strings :
1927+ self .assertNotIn ("_sync_coordinator" , s )
1928+
1929+ def test_collapsed_stack_collector_filters_internal_frames (self ):
1930+ """Test that CollapsedStackCollector filters out internal frames."""
1931+ collector = CollapsedStackCollector (sample_interval_usec = 1000 )
1932+
1933+ frames = [
1934+ MockInterpreterInfo (
1935+ 0 ,
1936+ [
1937+ MockThreadInfo (
1938+ 1 ,
1939+ [
1940+ MockFrameInfo ("app.py" , 50 , "run" ),
1941+ MockFrameInfo ("/lib/_sync_coordinator.py" , 100 , "main" ),
1942+ ],
1943+ status = THREAD_STATUS_HAS_GIL ,
1944+ )
1945+ ],
1946+ )
1947+ ]
1948+ collector .collect (frames )
1949+
1950+ # Check that no stack contains _sync_coordinator
1951+ for (call_tree , _ ), _ in collector .stack_counter .items ():
1952+ for filename , _ , _ in call_tree :
1953+ self .assertNotIn ("_sync_coordinator" , filename )
0 commit comments