Skip to content

Commit 30ea6c9

Browse files
simplify warmup
1 parent e5d56d5 commit 30ea6c9

1 file changed

Lines changed: 8 additions & 40 deletions

File tree

unittests/test_importers_performance.py

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from unittest.mock import patch
44

55
from crum import impersonate
6+
from django.contrib.contenttypes.models import ContentType
67
from django.utils import timezone
78

89
from dojo.decorators import dojo_async_task_counter
@@ -39,6 +40,12 @@ def setUp(self):
3940
self.system_settings(enable_webhooks_notifications=False)
4041
self.system_settings(enable_product_grade=False)
4142
self.system_settings(enable_github=False)
43+
# Warm up ContentType cache for relevant models. This is needed if we want to be able to run the test in isolation
44+
# As part of the test suite the ContentTYpe ids will already be cached and won't affect the query count.
45+
# But if we run the test in isolation, the ContentType ids will not be cached and will result in more queries.
46+
# By warming up the cache here, these queries are executed before we start counting queries
47+
for model in [Development_Environment, Dojo_User, Endpoint, Endpoint_Status, Engagement, Finding, Product, Product_Type, User]:
48+
ContentType.objects.get_for_model(model)
4249

4350
@contextmanager
4451
def assertNumAsyncTask(self, num):
@@ -80,45 +87,6 @@ def import_reimport_performance(self, expected_num_queries1, expected_num_async_
8087
lead, _ = User.objects.get_or_create(username="admin")
8188
environment, _ = Development_Environment.objects.get_or_create(name="Development")
8289

83-
# first we do a bogus import to make sure any caches are loaded.
84-
# without this the number of queries will be higher as the audit log will load content_type ids from the db
85-
86-
engagement_dummy, _created = Engagement.objects.get_or_create(
87-
name="Test Create Dummy Engagement",
88-
product=product,
89-
target_start=timezone.now(),
90-
target_end=timezone.now(),
91-
)
92-
import_options = {
93-
"user": lead,
94-
"lead": lead,
95-
"scan_date": None,
96-
"environment": environment,
97-
"minimum_severity": "Info",
98-
"active": True,
99-
"verified": True,
100-
"sync": True,
101-
"scan_type": NPM_AUDIT_SCAN_TYPE,
102-
"engagement": engagement_dummy,
103-
}
104-
importer = DefaultImporter(**import_options)
105-
test, _, _len_new_findings, _len_closed_findings, _, _, _ = importer.process_scan(NPM_AUDIT_NO_VULN_FILENAME.open(encoding="utf-8"))
106-
107-
# if True:
108-
# exit(0) # this is to make sure the importers are not run when running the tests in the IDE, as it will take too long
109-
110-
finding = Finding.objects.filter(test=test).first()
111-
112-
endpoint = Endpoint.objects.create(host="foo.bar", product=product)
113-
Endpoint_Status.objects.create(
114-
finding=finding,
115-
endpoint=endpoint,
116-
)
117-
118-
finding.endpoints.add(endpoint)
119-
finding.title = "Dummy finding to ensure audit log is created"
120-
finding.save()
121-
12290
# first import the subset which missed one finding and a couple of endpoints on some of the findings
12391
with (
12492
self.subTest("import1"), impersonate(Dojo_User.objects.get(username="admin")),
@@ -185,7 +153,7 @@ def import_reimport_performance(self, expected_num_queries1, expected_num_async_
185153

186154
def test_import_reimport_reimport_performance(self):
187155
self.import_reimport_performance(
188-
expected_num_queries1=602,
156+
expected_num_queries1=604,
189157
expected_num_async_tasks1=15,
190158
expected_num_queries2=489,
191159
expected_num_async_tasks2=23,

0 commit comments

Comments
 (0)