Skip to content

Commit 9739b2f

Browse files
chore(profiling): make memory alloc profiler tests more end-to-end (#15330)
## Description Previously, the tests directly tested the internal data-structures. Modified the tests to instead create the actual pprof, then check its data. This provides more realistic testing. It also is a first step to enabling changing the data-structures from python to native. ## Testing <!-- Describe your testing strategy or note what tests are included --> ## Risks <!-- Note any risks associated with this change, or "None" if no risks --> ## Additional Notes <!-- Any other information that would be helpful for reviewers --> --------- Co-authored-by: T. Kowalski <[email protected]>
1 parent 9ba3709 commit 9739b2f

File tree

3 files changed

+270
-128
lines changed

3 files changed

+270
-128
lines changed

ddtrace/profiling/collector/memalloc.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import threading
55
from types import TracebackType
6+
from typing import Any
67
from typing import List
78
from typing import NamedTuple
89
from typing import Optional
@@ -162,5 +163,36 @@ def test_snapshot(self) -> Tuple[MemorySample, ...]:
162163

163164
return tuple(samples)
164165

166+
def snapshot_and_parse_pprof(self, output_filename: str) -> Any:
167+
"""Export samples to profile, upload, and parse the pprof profile.
168+
169+
This is similar to test_snapshot() but exports to the profile and returns
170+
the parsed pprof profile instead of Python objects.
171+
172+
Args:
173+
output_filename: The pprof output filename prefix (without .pid.counter suffix)
174+
175+
Returns:
176+
Parsed pprof profile object (pprof_pb2.Profile)
177+
178+
Raises:
179+
ImportError: If pprof_utils is not available (only available in test environment)
180+
"""
181+
# Export samples to profile
182+
self.snapshot()
183+
184+
# Upload to write profile to disk
185+
ddup.upload()
186+
187+
# Parse the profile (only available in test environment)
188+
try:
189+
from tests.profiling.collector import pprof_utils
190+
except ImportError:
191+
raise ImportError(
192+
"pprof_utils is not available. snapshot_and_parse_pprof() is only available in test environment."
193+
)
194+
195+
return pprof_utils.parse_newest_profile(output_filename)
196+
165197
def collect(self) -> Tuple[MemorySample, ...]:
166198
return tuple()

tests/profiling/collector/pprof_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def assert_lock_events_of_type(
204204
assert all(get_label_with_key(profile.string_table, sample, "lock name") for sample in samples), (
205205
"All samples should have the label 'lock name'"
206206
)
207-
samples = {
207+
samples_dict = {
208208
profile.string_table[
209209
cast(pprof_pb2.Label, get_label_with_key(profile.string_table, sample, "lock name")).str
210210
]: sample
@@ -215,8 +215,8 @@ def assert_lock_events_of_type(
215215
key = "{}:{}".format(expected_event.filename, expected_event.linenos.create)
216216
else:
217217
key = "{}:{}:{}".format(expected_event.filename, expected_event.linenos.create, expected_event.lock_name)
218-
assert key in samples, "Expected lock event {} not found".format(key)
219-
assert_lock_event(profile, samples[key], expected_event)
218+
assert key in samples_dict, "Expected lock event {} not found".format(key)
219+
assert_lock_event(profile, samples_dict[key], expected_event)
220220

221221

222222
def assert_lock_events(

0 commit comments

Comments
 (0)