Skip to content

Commit 2ce6f8d

Browse files
committed
handle pprof output in test for different cases.
1 parent 124bda1 commit 2ce6f8d

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

tests/profiling/test_main.py

+23-8
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,35 @@ def test_call_script_pytorch_gpu(tmp_path, monkeypatch):
4545
monkeypatch.setenv("DD_PROFILING_OUTPUT_PPROF", filename)
4646
monkeypatch.setenv("DD_PROFILING_ENABLED", "1")
4747
monkeypatch.setenv("DD_PROFILING_PYTORCH_ENABLED", "1")
48+
4849
stdout, stderr, exitcode, pid = call_program(
4950
"ddtrace-run", sys.executable, os.path.join(os.path.dirname(__file__), "simple_program_pytorch_gpu.py")
5051
)
5152

52-
output_pprof_filename = filename + "." + str(pid) + ".1"
53+
output_pprof_filename = f"{filename}.{pid}.1"
5354
print("filename: ", output_pprof_filename)
54-
with gzip.open(output_pprof_filename, "rb") as f:
55-
content = f.read()
56-
p = pprof.pprof_pb2.Profile()
57-
p.ParseFromString(content)
58-
print("profile contents: ")
59-
print(p)
6055

61-
assert exitcode == 0
56+
# Verify the file exists and is not empty
57+
assert os.path.exists(output_pprof_filename), f"File {output_pprof_filename} does not exist."
58+
assert os.path.getsize(output_pprof_filename) > 0, "Profiling output file is empty."
59+
60+
# Check file header to determine format
61+
with open(output_pprof_filename, "rb") as f:
62+
header = f.read(2)
63+
print(f"File header bytes: {header}")
64+
f.seek(0) # Reset file pointer
65+
66+
if header == b"\x1f\x8b": # Gzip header
67+
with gzip.open(output_pprof_filename, "rb") as gz:
68+
content = gz.read()
69+
else:
70+
content = f.read()
71+
72+
p = pprof.pprof_pb2.Profile()
73+
p.ParseFromString(content)
74+
print("profile contents: ")
75+
print(p)
76+
assert exitcode == 0, f"Profiler exited with code {exitcode}. Stderr: {stderr}"
6277

6378

6479
def test_call_script_pprof_output(tmp_path, monkeypatch):

0 commit comments

Comments
 (0)