Skip to content

Commit eff9d82

Browse files
Julien Danjoumergify[bot]
andauthored
test(profiling): add a test for multiprocessing (#2934)
We test fork(), but we don't test higher level frameworks. Most Python frameworks using multiple processes are based on `multiprocessing`. Add a simple test that shows we do collect profiles for subprocesses when using multiprocessing. Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent f04a6ec commit eff9d82

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import multiprocessing
2+
import sys
3+
import time
4+
5+
6+
def f():
7+
time.sleep(1)
8+
9+
10+
if __name__ == "__main__":
11+
multiprocessing.set_start_method(sys.argv[1])
12+
13+
p = multiprocessing.Process(target=f)
14+
p.start()
15+
print(p.pid)
16+
p.join()

tests/profiling/test_main.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import multiprocessing
12
import os
3+
import sys
24

35
import pytest
46

@@ -62,3 +64,32 @@ def test_fork_gevent(monkeypatch):
6264
monkeypatch.setenv("DD_PROFILING_API_TIMEOUT", "0.1")
6365
stdout, stderr, exitcode, pid = call_program("python", os.path.join(os.path.dirname(__file__), "gevent_fork.py"))
6466
assert exitcode == 0
67+
68+
69+
atleast_py37 = sys.version_info[:2] >= (3, 7)
70+
71+
if atleast_py37:
72+
methods = multiprocessing.get_all_start_methods()
73+
else:
74+
methods = []
75+
76+
77+
@pytest.mark.parametrize(
78+
"method",
79+
methods,
80+
)
81+
def test_multiprocessing(method, tmp_path, monkeypatch):
82+
filename = str(tmp_path / "pprof")
83+
monkeypatch.setenv("DD_PROFILING_OUTPUT_PPROF", filename)
84+
monkeypatch.setenv("DD_PROFILING_ENABLED", "1")
85+
monkeypatch.setenv("DD_PROFILING_UPLOAD_INTERVAL", "0.1")
86+
stdout, stderr, exitcode, pid = call_program(
87+
"ddtrace-run",
88+
"python",
89+
os.path.join(os.path.dirname(__file__), "_test_multiprocessing.py"),
90+
method,
91+
)
92+
assert exitcode == 0, (stdout, stderr)
93+
child_pid = stdout.decode().strip()
94+
utils.check_pprof_file(filename + "." + str(pid) + ".1")
95+
utils.check_pprof_file(filename + "." + str(child_pid) + ".1")

0 commit comments

Comments
 (0)