Skip to content

Commit ce321d4

Browse files
[python_flask] Initial commit
1 parent 35c644a commit ce321d4

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
ARG BASE_IMAGE="prof-python-3.11"
2+
FROM $BASE_IMAGE
3+
4+
# Copy the Python target into the container
5+
COPY ./scenarios/python_flask_3.11/requirements.txt /app/
6+
RUN chmod 644 /app/*
7+
8+
# Set the working directory to the location of the program
9+
WORKDIR /app
10+
11+
RUN pip install -r requirements.txt
12+
13+
COPY ./scenarios/python_flask_3.11/app.py /app/
14+
15+
ENV DD_PROFILING_ENABLED=true
16+
ENV DD_TRACE_ENABLED=false
17+
ENV DD_TRACE_DEBUG=false
18+
ENV DD_PROFILING_OUTPUT_PPROF="/app/data/profiles"
19+
ENV EXECUTION_TIME_SEC="5"
20+
21+
# Run the program when the container starts
22+
CMD ["ddtrace-run", "flask", "run", "--host=0.0.0.0", "--port=8000"]
1.49 KB
Binary file not shown.

scenarios/python_flask_3.11/app.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import os
2+
import signal
3+
import time
4+
from threading import Thread
5+
6+
import requests
7+
from flask import Flask
8+
9+
app = Flask(__name__)
10+
11+
12+
def _make_requests() -> None:
13+
start = time.monotonic()
14+
while time.monotonic() - start < 5.0:
15+
try:
16+
requests.get("http://localhost:8000", timeout=1)
17+
except Exception as e: # noqa: PERF203,BLE001
18+
print(f"Error making request: {e}")
19+
20+
requests.get("http://localhost:8000/stop", timeout=1)
21+
22+
23+
def compute_big_number() -> int:
24+
start = time.monotonic()
25+
while time.monotonic() - start < 0.5:
26+
x = 0
27+
for i in range(1000000):
28+
x += i
29+
return x
30+
31+
32+
@app.route("/")
33+
def hello_world() -> str:
34+
x = compute_big_number()
35+
return f"<p>Hello, World! {x}</p>"
36+
37+
38+
@app.route("/stop")
39+
def stop() -> str:
40+
os.kill(os.getpid(), signal.SIGINT)
41+
return "Stopping..."
42+
43+
44+
t = Thread(target=_make_requests, name="Requester")
45+
t.daemon = True
46+
t.start()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"test_name": "python_flask_3.11",
3+
"scale_by_duration": false,
4+
"pprof-regex": "",
5+
"stacks": [
6+
{
7+
"profile-type": "wall-time",
8+
"pprof-regex": "",
9+
"stack-content": [
10+
{
11+
"regular_expression": ".*_make_requests.*",
12+
"percent": 33,
13+
"error_margin": 5,
14+
"labels": [
15+
{
16+
"key": "thread name",
17+
"values": [
18+
"Requester"
19+
]
20+
}
21+
]
22+
}
23+
]
24+
},
25+
{
26+
"profile-type": "wall-time",
27+
"pprof-regex": "",
28+
"stack-content": [
29+
{
30+
"regular_expression": ".*hello_world;compute_big_number.*",
31+
"percent": 33,
32+
"error_margin": 5
33+
}
34+
]
35+
}
36+
]
37+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ddtrace
2+
requests
3+
Flask

0 commit comments

Comments
 (0)