-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtest_profiling.py
More file actions
211 lines (187 loc) · 8.94 KB
/
Copy pathtest_profiling.py
File metadata and controls
211 lines (187 loc) · 8.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import os
import shutil
import sys
import tempfile
import unittest
from unittest import mock
from django.contrib.auth.models import User
from django.db import IntegrityError, transaction
from django.http import HttpResponse
from django.test import TestCase
from django.test.utils import override_settings
from django.urls import reverse
from debug_toolbar.panels.profiling import ProfilingPanel
from ..base import BaseTestCase, IntegrationTestCase
from ..views import listcomp_view, regular_view
@override_settings(
DEBUG_TOOLBAR_PANELS=["debug_toolbar.panels.profiling.ProfilingPanel"]
)
class ProfilingPanelTestCase(BaseTestCase):
panel_id = ProfilingPanel.panel_id
def test_regular_view(self):
self._get_response = lambda request: regular_view(request, "profiling")
response = self.panel.process_request(self.request)
self.panel.generate_stats(self.request, response)
self.assertIn("func_list", self.panel.get_stats())
self.assertIn("regular_view", self.panel.content)
def test_insert_content(self):
"""
Test that the panel only inserts content after generate_stats and
not the process_request.
"""
self._get_response = lambda request: regular_view(request, "profiling")
response = self.panel.process_request(self.request)
# ensure the panel does not have content yet.
self.assertNotIn("regular_view", self.panel.content)
self.panel.generate_stats(self.request, response)
self.reload_stats()
# ensure the panel renders correctly.
content = self.panel.content
self.assertIn("regular_view", content)
self.assertIn("render", content)
self.assertValidHTML(content)
# ensure traces aren't escaped
self.assertIn('<span class="djdt-path">', content)
@override_settings(DEBUG_TOOLBAR_CONFIG={"PROFILER_THRESHOLD_RATIO": 1})
def test_cum_time_threshold(self):
"""
Test that cumulative time threshold excludes calls
"""
self._get_response = lambda request: regular_view(request, "profiling")
response = self.panel.process_request(self.request)
self.panel.generate_stats(self.request, response)
# ensure the panel renders but doesn't include our function.
content = self.panel.content
self.assertIn("regular_view", content)
self.assertNotIn("render", content)
self.assertValidHTML(content)
@unittest.skipUnless(
sys.version_info < (3, 12, 0),
"Python 3.12 no longer contains a frame for list comprehensions.",
)
def test_listcomp_escaped(self):
self._get_response = lambda request: listcomp_view(request)
response = self.panel.process_request(self.request)
self.panel.generate_stats(self.request, response)
content = self.panel.content
self.assertNotIn('<span class="djdt-func"><listcomp></span>', content)
self.assertIn('<span class="djdt-func"><listcomp></span>', content)
def test_generate_stats_no_profiler(self):
"""
Test generating stats with no profiler.
"""
response = HttpResponse()
self.assertIsNone(self.panel.generate_stats(self.request, response))
def test_generate_stats_signed_path(self):
with tempfile.TemporaryDirectory() as tmpdir:
with self.settings(DEBUG_TOOLBAR_CONFIG={"PROFILER_PROFILE_ROOT": tmpdir}):
response = self.panel.process_request(self.request)
self.panel.generate_stats(self.request, response)
path = self.panel.prof_file_path
self.assertTrue(path)
# Check that it's a valid signature
filename = path
self.assertTrue(filename.endswith(".prof"))
def test_generate_stats_no_root(self):
# If PROFILER_PROFILE_ROOT is None, it should default to MEDIA_ROOT (or default storage location)
# We need to ensure we can write to it for this test to pass without logging an error.
# But wait, BaseTestCase might not set up MEDIA_ROOT.
# let's override settings to be safe, pointing MEDIA_ROOT to a temp dir.
with tempfile.TemporaryDirectory() as tmpdir:
with self.settings(
DEBUG_TOOLBAR_CONFIG={"PROFILER_PROFILE_ROOT": None},
MEDIA_ROOT=tmpdir,
STORAGES={
"default": {
"BACKEND": "django.core.files.storage.FileSystemStorage",
}
},
):
response = self.panel.process_request(self.request)
self.panel.generate_stats(self.request, response)
# Should now have a path because we fall back to default storage
self.assertTrue(hasattr(self.panel, "prof_file_path"))
self.assertTrue(self.panel.prof_file_path.endswith(".prof"))
# Verify it was written to tmpdir
full_path = os.path.join(tmpdir, self.panel.prof_file_path)
self.assertTrue(os.path.exists(full_path))
def test_generate_stats_no_root_func(self):
"""
Test generating stats using profiler without root function.
"""
response = self.panel.process_request(self.request)
self.panel.profiler.clear()
self.panel.profiler.enable()
self.panel.profiler.disable()
self.panel.generate_stats(self.request, response)
self.assertNotIn("func_list", self.panel.get_stats())
@mock.patch("django.core.files.storage.FileSystemStorage.save")
def test_generate_stats_oserror(self, mock_save):
mock_save.side_effect = OSError
with tempfile.TemporaryDirectory() as tmpdir:
with self.settings(DEBUG_TOOLBAR_CONFIG={"PROFILER_PROFILE_ROOT": tmpdir}):
response = self.panel.process_request(self.request)
with self.assertLogs(
"debug_toolbar.panels.profiling", level="ERROR"
) as cm:
self.panel.generate_stats(self.request, response)
self.assertIn("Failed to dump profiling stats", cm.output[0])
# Ensure prof_file_path is not set/updated if dump fails
self.assertFalse(hasattr(self.panel, "prof_file_path"))
@override_settings(
DEBUG=True, DEBUG_TOOLBAR_PANELS=["debug_toolbar.panels.profiling.ProfilingPanel"]
)
class ProfilingPanelIntegrationTestCase(IntegrationTestCase):
def test_view_executed_once(self):
self.assertEqual(User.objects.count(), 0)
response = self.client.get("/new_user/")
self.assertContains(response, "Profiling")
self.assertEqual(User.objects.count(), 1)
with self.assertRaises(IntegrityError), transaction.atomic():
response = self.client.get("/new_user/")
self.assertEqual(User.objects.count(), 1)
class ProfilingDownloadViewTestCase(TestCase):
def setUp(self):
self.root = tempfile.mkdtemp()
self.filename = "test.prof"
self.filepath = os.path.join(self.root, self.filename)
with open(self.filepath, "wb") as f:
f.write(b"data")
self.path = self.filename
def tearDown(self):
shutil.rmtree(self.root)
def test_download_default_storage(self):
# Verify downloading works if PROFILER_PROFILE_ROOT is unset (None),
# falling back to default storage (which often uses MEDIA_ROOT for FileSystemStorage default).
# We simulate this by setting MEDIA_ROOT to our temp dir and PROFILER_PROFILE_ROOT to None.
with override_settings(
DEBUG_TOOLBAR_CONFIG={"PROFILER_PROFILE_ROOT": None},
MEDIA_ROOT=self.root,
STORAGES={
"default": {
"BACKEND": "django.core.files.storage.FileSystemStorage",
}
},
):
url = reverse("djdt:debug_toolbar_download_prof_file")
# The file 'test.prof' exists in self.root (which is now MEDIA_ROOT)
response = self.client.get(url, {"path": self.filename})
self.assertEqual(response.status_code, 200)
self.assertEqual(list(response.streaming_content), [b"data"])
def test_download_valid(self):
with override_settings(
DEBUG_TOOLBAR_CONFIG={"PROFILER_PROFILE_ROOT": self.root}
):
url = reverse("djdt:debug_toolbar_download_prof_file")
response = self.client.get(url, {"path": self.path})
self.assertEqual(response.status_code, 200)
self.assertEqual(list(response.streaming_content), [b"data"])
def test_download_missing_file(self):
with override_settings(
DEBUG_TOOLBAR_CONFIG={"PROFILER_PROFILE_ROOT": self.root}
):
url = reverse("djdt:debug_toolbar_download_prof_file")
# Sign a filename that doesn't exist
path = "missing.prof"
response = self.client.get(url, {"path": path})
self.assertEqual(response.status_code, 404)