Skip to content

Commit 6d84ac3

Browse files
feat(profiling): add memory allocator testing variants
1 parent e806495 commit 6d84ac3

File tree

2 files changed

+84
-14
lines changed

2 files changed

+84
-14
lines changed

riotfile.py

Lines changed: 83 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,85 @@ def select_pys(min_version: str = MIN_PYTHON_VERSION, max_version: str = MAX_PYT
7979
return [version_to_str(version) for version in SUPPORTED_PYTHON_VERSIONS if min_version <= version <= max_version]
8080

8181

82+
def create_memory_allocator_venvs() -> List[Venv]:
83+
"""Create venv variants for testing different Python memory allocators."""
84+
import ctypes
85+
86+
allocator_configs = [
87+
{
88+
"name_suffix": "pymalloc",
89+
"env": {"PYTHONMALLOC": "pymalloc"},
90+
"description": "Python's default small object allocator"
91+
},
92+
{
93+
"name_suffix": "malloc",
94+
"env": {"PYTHONMALLOC": "malloc"},
95+
"description": "System malloc allocator"
96+
},
97+
{
98+
"name_suffix": "malloc-debug",
99+
"env": {"PYTHONMALLOC": "malloc_debug"},
100+
"description": "System malloc with debug hooks"
101+
},
102+
{
103+
"name_suffix": "pymalloc-debug",
104+
"env": {"PYTHONMALLOC": "pymalloc_debug"},
105+
"description": "Python malloc with debug hooks"
106+
}
107+
]
108+
109+
# Conditionally add jemalloc variant if the library is available
110+
jemalloc_names = [
111+
"libjemalloc.so.2", # Ubuntu/Debian
112+
"libjemalloc.so.1", # Older versions
113+
"libjemalloc.so", # Generic
114+
"libjemalloc.dylib", # macOS (though less common)
115+
]
116+
117+
for lib_name in jemalloc_names:
118+
try:
119+
# Try to load jemalloc library (OSError is raised if not found)
120+
ctypes.CDLL(lib_name)
121+
122+
allocator_configs.append({
123+
"name_suffix": "jemalloc",
124+
"env": {
125+
"PYTHONMALLOC": "malloc",
126+
"LD_PRELOAD": lib_name
127+
},
128+
"description": "jemalloc high-performance allocator"
129+
})
130+
131+
break
132+
except (OSError, AttributeError):
133+
# Library not found or ctypes issue - try next name
134+
continue
135+
136+
venvs = []
137+
for config in allocator_configs:
138+
venvs.append(Venv(
139+
name=f"profile-v2-{config['name_suffix']}",
140+
command="python -m tests.profiling.run pytest -v --no-cov --capture=no --benchmark-disable {cmdargs} tests/profiling_v2",
141+
pys=select_pys(),
142+
env=config["env"],
143+
pkgs=_profile_v2_pkgs
144+
))
145+
146+
return venvs
147+
148+
149+
# Common package configuration for profiling v2 tests
150+
_profile_v2_pkgs = {
151+
"gunicorn": latest,
152+
"jsonschema": latest,
153+
"lz4": latest,
154+
"pytest-cpp": latest,
155+
"pytest-benchmark": latest,
156+
"py-cpuinfo": "~=8.0.0",
157+
"pytest-asyncio": "==0.21.1",
158+
"pytest-randomly": latest,
159+
}
160+
82161
# Common venv configurations for appsec threats testing
83162
_appsec_threats_iast_variants = [
84163
Venv(
@@ -3291,26 +3370,17 @@ def select_pys(min_version: str = MIN_PYTHON_VERSION, max_version: str = MAX_PYT
32913370
env={
32923371
"DD_PROFILING_ENABLE_ASSERTS": "1",
32933372
"CPUCOUNT": "12",
3373+
# Default values for memory allocator testing - can be overridden with --pass-env
3374+
"PYTHONMALLOC": "pymalloc",
32943375
},
3295-
pkgs={
3296-
"gunicorn": latest,
3297-
"jsonschema": latest,
3298-
"lz4": latest,
3299-
"pytest-cpp": latest,
3300-
#
3301-
# pytest-benchmark depends on cpuinfo which dropped support for Python<=3.6 in 9.0
3302-
# See https://github.com/workhorsy/py-cpuinfo/issues/177
3303-
"pytest-benchmark": latest,
3304-
"py-cpuinfo": "~=8.0.0",
3305-
"pytest-asyncio": "==0.21.1",
3306-
"pytest-randomly": latest,
3307-
},
3376+
pkgs=_profile_v2_pkgs,
33083377
venvs=[
33093378
Venv(
33103379
command="python -m pytest {cmdargs} tests/profiling_v2/test_uwsgi.py",
33113380
pys=select_pys(),
33123381
pkgs={"uwsgi": "<2.0.30"},
33133382
),
3383+
*create_memory_allocator_venvs(),
33143384
# Python 3.8 + 3.9
33153385
Venv(
33163386
pys=["3.8", "3.9"],

tests/profiling_v2/collector/test_memalloc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_memory_collector(tmp_path):
5050
pprof_prefix = str(tmp_path / test_name)
5151
output_filename = pprof_prefix + "." + str(os.getpid())
5252

53-
ddup.config(
53+
ddup.config( # pyright: ignore[reportCallIssue]
5454
service=test_name,
5555
version="test",
5656
env="test",

0 commit comments

Comments
 (0)