Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions scenarios/python_basic_memory_3.11/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions scenarios/python_basic_memory_3.11/expected_profile.json
Original file line number Diff line number Diff line change
@@ -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
}
36 changes: 36 additions & 0 deletions scenarios/python_basic_memory_3.11/main.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions scenarios/python_basic_memory_3.11/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ddtrace==4.0.0