-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtest_profiling.py
More file actions
174 lines (150 loc) · 6.74 KB
/
Copy pathtest_profiling.py
File metadata and controls
174 lines (150 loc) · 6.74 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
import os
import shutil
import sys
import tempfile
import unittest
from django.contrib.auth.models import User
from django.core import signing
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))
@override_settings(
DEBUG_TOOLBAR_CONFIG={"PROFILER_PROFILE_ROOT": tempfile.gettempdir()}
)
def test_generate_stats_signed_path(self):
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 = signing.loads(path)
self.assertTrue(filename.endswith(".prof"))
def test_generate_stats_no_root(self):
response = self.panel.process_request(self.request)
self.panel.generate_stats(self.request, response)
# Should not have a path if root is not set
self.assertFalse(hasattr(self.panel, "prof_file_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())
@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.signed_path = signing.dumps(self.filename)
def tearDown(self):
shutil.rmtree(self.root)
def test_download_no_root_configured(self):
response = self.client.get(reverse("djdt:debug_toolbar_download_prof_file"))
self.assertEqual(response.status_code, 404)
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.signed_path})
self.assertEqual(response.status_code, 200)
self.assertEqual(list(response.streaming_content), [b"data"])
def test_download_invalid_signature(self):
with override_settings(
DEBUG_TOOLBAR_CONFIG={"PROFILER_PROFILE_ROOT": self.root}
):
url = reverse("djdt:debug_toolbar_download_prof_file")
# Tamper with the signature
response = self.client.get(url, {"path": self.signed_path + "bad"})
self.assertEqual(response.status_code, 404)
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 = signing.dumps("missing.prof")
response = self.client.get(url, {"path": path})
self.assertEqual(response.status_code, 404)