Skip to content

Commit 2803cbb

Browse files
committed
gh-161008: Fix missing statistics in heatmap replay output
1 parent 1259f16 commit 2803cbb

4 files changed

Lines changed: 55 additions & 2 deletions

File tree

‎Lib/profiling/sampling/_heatmap_assets/heatmap_index_template.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
<div class="stat-card">
6767
<div class="stat-icon">&#9201;</div>
6868
<div class="stat-data">
69-
<span class="stat-value"><!-- DURATION --></span>
69+
<span class="stat-value"><!-- DURATION --> sec</span>
7070
<span class="stat-label">Duration</span>
7171
</div>
7272
<div class="stat-sparkline"></div>

‎Lib/profiling/sampling/heatmap_collector.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,20 @@ def set_stats(self, sample_interval_usec, duration_sec, sample_rate, error_rate=
407407
}
408408
self.stats.update(kwargs)
409409

410+
def set_replay_stats(self, info):
411+
"""Restore measured statistics stored in a binary profile."""
412+
duration_sec = info.get("duration_sec")
413+
sample_rate = info.get("sample_rate")
414+
if duration_sec is None or sample_rate is None:
415+
return
416+
self.set_stats(
417+
self.sample_interval_usec,
418+
duration_sec,
419+
sample_rate,
420+
error_rate=info.get("error_rate"),
421+
missed_samples=info.get("missed_samples"),
422+
)
423+
410424
def process_frames(self, frames, thread_id, weight=1):
411425
"""Process stack frames and count samples per line.
412426

‎Lib/test/test_profiling/test_sampling_profiler/test_binary_format.py‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
import struct
88
import tempfile
99
import unittest
10+
from types import SimpleNamespace
1011
from unittest import mock
1112
from collections import defaultdict
1213

13-
from test.support import captured_stderr
14+
from test.support import captured_stderr, captured_stdout, run_with_locale
1415

1516
try:
1617
import _remote_debugging
@@ -28,6 +29,7 @@
2829
)
2930
from profiling.sampling.binary_collector import BinaryCollector
3031
from profiling.sampling.binary_reader import BinaryReader, convert_binary_to_format
32+
from profiling.sampling.cli import _replay_with_reader
3133
from profiling.sampling.constants import PROFILING_MODE_CPU
3234
from profiling.sampling.gecko_collector import GeckoCollector
3335

@@ -1937,6 +1939,41 @@ def test_original_stats_extension_remains_readable(self):
19371939
self.assertIsNone(info["missed_samples"])
19381940

19391941

1942+
class TestBinaryReplayToHeatmap(unittest.TestCase):
1943+
@run_with_locale("LC_NUMERIC", "C")
1944+
def test_replay_with_and_without_persisted_stats(self):
1945+
frame = make_frame(__file__, 1, "hot_func")
1946+
for with_stats in (True, False):
1947+
with (
1948+
self.subTest(with_stats=with_stats),
1949+
tempfile.TemporaryDirectory() as tmp,
1950+
captured_stdout(),
1951+
):
1952+
bin_path = os.path.join(tmp, "profile.bin")
1953+
with BinaryCollector(bin_path, 2000, compression="none") as collector:
1954+
collector.collect([
1955+
make_interpreter(0, [make_thread(1, [frame])])
1956+
])
1957+
if with_stats:
1958+
collector.set_stats(
1959+
2000, 2.5, 0.4, error_rate=2.0, missed_samples=1.5
1960+
)
1961+
args = SimpleNamespace(
1962+
format="heatmap", input_file=bin_path,
1963+
outfile=tmp, diff_baseline=None,
1964+
)
1965+
with BinaryReader(bin_path) as reader:
1966+
_replay_with_reader(args, reader)
1967+
content = pathlib.Path(tmp, "index.html").read_text(
1968+
encoding="utf-8"
1969+
)
1970+
if with_stats:
1971+
self.assertIn('<span class="stat-value">2.5 sec</span>', content)
1972+
self.assertIn('<span class="stat-value">0.4</span>', content)
1973+
self.assertIn('<span class="rate-value">2.0%</span>', content)
1974+
self.assertIn('<span class="rate-value">1.5%</span>', content)
1975+
1976+
19401977
class TestBinaryReplayToJsonl(BinaryFormatTestBase):
19411978
"""Tests for binary -> JSONL replay via convert_binary_to_format."""
19421979

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix zero duration and sample rate in heatmap replay output. Patch by Timofei
2+
Ivankov.

0 commit comments

Comments
 (0)