Skip to content

Commit 4c6d141

Browse files
committed
unified stats function
1 parent 2803cbb commit 4c6d141

5 files changed

Lines changed: 32 additions & 41 deletions

File tree

‎Lib/profiling/sampling/binary_reader.py‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,28 @@ def get_stats(self):
103103
return self._reader.get_stats()
104104

105105

106+
def _set_replay_stats(collector, info, sample_interval_usec):
107+
"""Pass recorded statistics to a collector using the live sampling API."""
108+
duration_sec = info.get("duration_sec")
109+
sample_rate = info.get("sample_rate")
110+
mode = info.get("mode")
111+
if (
112+
duration_sec is not None
113+
and sample_rate is not None
114+
and hasattr(collector, "set_stats")
115+
):
116+
collector.set_stats(
117+
sample_interval_usec,
118+
duration_sec,
119+
sample_rate,
120+
error_rate=info.get("error_rate"),
121+
missed_samples=info.get("missed_samples"),
122+
mode=mode,
123+
)
124+
elif hasattr(collector, "set_mode"):
125+
collector.set_mode(mode)
126+
127+
106128
def convert_binary_to_format(input_file, output_file, output_format,
107129
sample_interval_usec=None, progress_callback=None):
108130
"""Convert a binary profiling file to another format.
@@ -137,10 +159,7 @@ def convert_binary_to_format(input_file, output_file, output_format,
137159

138160
# Replay samples through collector
139161
count = reader.replay_samples(collector, progress_callback)
140-
if hasattr(collector, "set_replay_stats"):
141-
collector.set_replay_stats(info)
142-
if hasattr(collector, "set_mode"):
143-
collector.set_mode(info.get("mode"))
162+
_set_replay_stats(collector, info, interval)
144163

145164
# Export to target format
146165
collector.export(output_file)

‎Lib/profiling/sampling/cli.py‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from .gecko_collector import GeckoCollector
2323
from .jsonl_collector import JsonlCollector
2424
from .binary_collector import BinaryCollector
25-
from .binary_reader import BinaryReader
25+
from .binary_reader import BinaryReader, _set_replay_stats
2626
from .constants import (
2727
MICROSECONDS_PER_SECOND,
2828
PROFILING_MODE_ALL,
@@ -793,10 +793,7 @@ def progress_callback(current, total):
793793
)
794794

795795
count = reader.replay_samples(collector, progress_callback)
796-
if hasattr(collector, "set_replay_stats"):
797-
collector.set_replay_stats(info)
798-
if hasattr(collector, "set_mode"):
799-
collector.set_mode(info.get("mode"))
796+
_set_replay_stats(collector, info, interval)
800797
print()
801798

802799
if args.format == "pstats":

‎Lib/profiling/sampling/heatmap_collector.py‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -407,20 +407,6 @@ 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-
424410
def process_frames(self, frames, thread_id, weight=1):
425411
"""Process stack frames and count samples per line.
426412

‎Lib/profiling/sampling/stack_collector.py‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -152,20 +152,6 @@ def set_stats(self, sample_interval_usec, duration_sec, sample_rate,
152152
"mode": mode
153153
}
154154

155-
def set_replay_stats(self, info):
156-
"""Restore measured statistics stored in a binary profile."""
157-
duration_sec = info.get("duration_sec")
158-
sample_rate = info.get("sample_rate")
159-
if duration_sec is None or sample_rate is None:
160-
return
161-
self.set_stats(
162-
self.sample_interval_usec,
163-
duration_sec,
164-
sample_rate,
165-
error_rate=info.get("error_rate"),
166-
missed_samples=info.get("missed_samples"),
167-
mode=self.stats.get("mode"),
168-
)
169155
def set_mode(self, mode):
170156
self.stats["mode"] = mode
171157

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
try:
1414
import _remote_debugging # noqa: F401
1515
from profiling.sampling import gecko_collector
16+
from profiling.sampling.binary_reader import _set_replay_stats
1617
from profiling.sampling.pstats_collector import PstatsCollector
1718
from profiling.sampling.stack_collector import (
1819
CollapsedStackCollector,
@@ -1218,12 +1219,12 @@ def test_flamegraph_collector_stats_accumulation(self):
12181219
def test_flamegraph_collector_restores_replay_stats(self):
12191220
"""Replay restores measured values from binary metadata."""
12201221
collector = FlamegraphCollector(1000)
1221-
collector.set_replay_stats({
1222+
_set_replay_stats(collector, {
12221223
"duration_sec": 1.25,
12231224
"sample_rate": 4.0,
12241225
"error_rate": 2.5,
12251226
"missed_samples": 1.5,
1226-
})
1227+
}, 1000)
12271228

12281229
self.assertEqual(collector.stats["duration_sec"], 1.25)
12291230
self.assertEqual(collector.stats["sample_rate"], 4.0)
@@ -1233,10 +1234,12 @@ def test_flamegraph_collector_restores_replay_stats(self):
12331234
def test_flamegraph_collector_leaves_legacy_replay_stats_unavailable(self):
12341235
collector = FlamegraphCollector(1000)
12351236
original_stats = collector.stats.copy()
1236-
collector.set_replay_stats({
1237+
original_stats["mode"] = PROFILING_MODE_CPU
1238+
_set_replay_stats(collector, {
12371239
"duration_sec": None,
12381240
"sample_rate": None,
1239-
})
1241+
"mode": PROFILING_MODE_CPU,
1242+
}, 1000)
12401243
self.assertEqual(collector.stats, original_stats)
12411244

12421245
def test_flamegraph_collector_per_thread_stats(self):

0 commit comments

Comments
 (0)