diff --git a/scenarios/python_basic_memory_3.11/Dockerfile b/scenarios/python_basic_memory_3.11/Dockerfile new file mode 100644 index 0000000..a45952f --- /dev/null +++ b/scenarios/python_basic_memory_3.11/Dockerfile @@ -0,0 +1,20 @@ +ARG BASE_IMAGE="prof-python-3.11" +FROM $BASE_IMAGE + +# Copy the Python target into the container +COPY ./scenarios/python_basic_memory_3.11/requirements.txt /app/requirements.txt +RUN chmod 644 /app/* + +RUN pip install -r /app/requirements.txt + +COPY ./scenarios/python_basic_memory_3.11/main.py /app/main.py + +# Set the working directory to the location of the program +WORKDIR /app + +ENV EXECUTION_TIME_SEC="3" +ENV DD_TRACE_DEBUG=false + +# Run the program when the container starts +CMD python main.py +# CMD ddprof -l notice --preset cpu_live_heap python main.py diff --git a/scenarios/python_basic_memory_3.11/expected_profile.json b/scenarios/python_basic_memory_3.11/expected_profile.json new file mode 100644 index 0000000..9407060 --- /dev/null +++ b/scenarios/python_basic_memory_3.11/expected_profile.json @@ -0,0 +1,47 @@ +{ + "test_name": "python_memory_basic", + "pprof-regex": "", + "stacks": [ + { + "profile-type": "alloc-samples", + "pprof-regex": "", + "stack-content": [ + { + "regular_expression": "^\u003cmodule\u003e;run;allocate_memory$", + "percent": 40, + "error_margin": 5, + "labels": [ + { + "key": "thread name", + "values": [ + "MainThread" + ], + "values_regex": "" + } + ] + } + ] + }, + { + "profile-type": "alloc-samples", + "pprof-regex": "", + "stack-content": [ + { + "regular_expression": "^\u003cmodule\u003e;run;allocate_memory_2$", + "percent": 60, + "error_margin": 5, + "labels": [ + { + "key": "thread name", + "values": [ + "MainThread" + ], + "values_regex": "" + } + ] + } + ] + } + ], + "scale_by_duration": false +} \ No newline at end of file diff --git a/scenarios/python_basic_memory_3.11/main.py b/scenarios/python_basic_memory_3.11/main.py new file mode 100644 index 0000000..3a10b7a --- /dev/null +++ b/scenarios/python_basic_memory_3.11/main.py @@ -0,0 +1,36 @@ +import os +import time + +from ddtrace.profiling import Profiler + + +class Target: + def __init__(self) -> None: + self.memory: list[bytearray] = [] + + def run(self, n: int) -> None: + end_time = time.monotonic() + n + while time.monotonic() < end_time: + self.allocate_memory(1024) + self.allocate_memory_2(1024) + + def allocate_memory(self, size: int) -> None: + self.memory.append(bytearray(size)) + + def allocate_memory_2(self, size: int) -> None: + self.memory.append(bytearray(3 * size)) + + +if __name__ == "__main__": + # Simple application that creates two threads with different durations: + # - MainThread runs target() for 2 seconds + # - Worker Thread-1 runs target() for 1 second + # The profiler should capture both threads with their respective durations. + prof = Profiler() + prof.start() # Should be as early as possible, eg before other imports, to ensure everything is profiled + + EXECUTION_TIME_SEC = int(os.environ.get("EXECUTION_TIME_SEC", "2")) + + Target().run(EXECUTION_TIME_SEC) + + prof.stop() diff --git a/scenarios/python_basic_memory_3.11/requirements.txt b/scenarios/python_basic_memory_3.11/requirements.txt new file mode 100644 index 0000000..2eb0acb --- /dev/null +++ b/scenarios/python_basic_memory_3.11/requirements.txt @@ -0,0 +1 @@ +ddtrace==4.0.0