Skip to content

Commit 6832ac0

Browse files
committed
fix heatmap
1 parent c921985 commit 6832ac0

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

Lib/profiling/sampling/heatmap_collector.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,8 @@ def process_frames(self, frames, thread_id, weight=1):
452452
next_lineno = extract_lineno(next_frame[1])
453453
self._record_call_relationship(
454454
(filename, lineno, funcname),
455-
(next_frame[0], next_lineno, next_frame[2])
455+
(next_frame[0], next_lineno, next_frame[2]),
456+
weight=weight,
456457
)
457458

458459
def _is_valid_frame(self, filename, lineno):
@@ -561,7 +562,7 @@ def _get_bytecode_data_for_line(self, filename, lineno):
561562
result.sort(key=lambda x: (-x['samples'], x['opcode']))
562563
return result
563564

564-
def _record_call_relationship(self, callee_frame, caller_frame):
565+
def _record_call_relationship(self, callee_frame, caller_frame, weight=1):
565566
"""Record caller/callee relationship between adjacent frames."""
566567
callee_filename, callee_lineno, callee_funcname = callee_frame
567568
caller_filename, caller_lineno, caller_funcname = caller_frame
@@ -587,7 +588,7 @@ def _record_call_relationship(self, callee_frame, caller_frame):
587588

588589
# Count this call edge for path analysis
589590
edge_key = (caller_key, callee_key)
590-
self.edge_samples[edge_key] += 1
591+
self.edge_samples[edge_key] += weight
591592

592593
def export(self, output_path):
593594
"""Export heatmap data as HTML files in a directory.

Lib/test/test_profiling/test_heatmap.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,21 @@ def test_process_frames_tracks_edge_samples(self):
345345
# Check that edge count is tracked
346346
self.assertGreater(len(collector.edge_samples), 0)
347347

348+
def test_process_frames_weight_applies_to_identical_samples(self):
349+
collector = HeatmapCollector(sample_interval_usec=100)
350+
351+
frames = [
352+
('callee.py', (5, 5, -1, -1), 'callee', None),
353+
('caller.py', (10, 10, -1, -1), 'caller', None),
354+
]
355+
356+
collector.process_frames(frames, thread_id=1, weight=5)
357+
358+
edge_key = (('caller.py', 10), ('callee.py', 5))
359+
self.assertEqual(collector.edge_samples[edge_key], 5)
360+
self.assertEqual(collector.line_samples[('callee.py', 5)], 5)
361+
self.assertEqual(collector.line_samples[('caller.py', 10)], 5)
362+
348363
def test_process_frames_handles_empty_frames(self):
349364
"""Test that process_frames handles empty frame list."""
350365
collector = HeatmapCollector(sample_interval_usec=100)

0 commit comments

Comments
 (0)