Skip to content

Commit e3327f5

Browse files
committed
gh-152721: Test replay batches beyond the RLE limit
1 parent 8836caa commit e3327f5

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import struct
88
import tempfile
99
import unittest
10+
from unittest import mock
1011
from collections import defaultdict
1112

1213
from test.support import captured_stderr
@@ -1547,6 +1548,34 @@ def export(self, filename):
15471548
self.assertEqual(collector.batches, expected)
15481549

15491550

1551+
def test_rle_long_run_splits_batches(self):
1552+
# Construct a single repeat record larger than the writer's buffer.
1553+
num_samples = 8193
1554+
filename = self.create_binary_file([], compression="none")
1555+
data = bytearray(pathlib.Path(filename).read_bytes())
1556+
record = (struct.pack("=QIB", 1, 0, 0) # STACK_REPEAT
1557+
+ b"\x81\x40" # 8193 as a varint
1558+
+ b"\x01\x00" * num_samples) # delta=1, status=0
1559+
data[64:64] = record
1560+
struct.pack_into("=Q", data, 12, 0) # start timestamp
1561+
struct.pack_into("=Q", data, 28, num_samples)
1562+
struct.pack_into("=I", data, 36, 1) # thread count
1563+
for offset in (40, 48): # string and frame table offsets
1564+
old_offset = struct.unpack_from("=Q", data, offset)[0]
1565+
struct.pack_into("=Q", data, offset, old_offset + len(record))
1566+
struct.pack_into("=Q", data, len(data) - 24, len(data))
1567+
pathlib.Path(filename).write_bytes(data)
1568+
1569+
collector = mock.Mock()
1570+
with BinaryReader(filename) as reader:
1571+
count = reader.replay_samples(collector)
1572+
batches = [call.args[1] for call in collector.collect.call_args_list]
1573+
self.assertEqual(count, num_samples)
1574+
self.assertEqual([len(batch) for batch in batches], [8192, 1])
1575+
self.assertEqual([ts for batch in batches for ts in batch],
1576+
list(range(1, num_samples + 1)))
1577+
1578+
15501579
class TestBinaryStress(BinaryFormatTestBase):
15511580
"""Randomized stress tests for binary format."""
15521581

0 commit comments

Comments
 (0)