Skip to content

Commit b7992db

Browse files
unittests: import query/task count capture (#12716)
* prepare stack hawk scan reports * new test case with stats * add foreground testcase * produc grading * default webhooks ons * fix counts * add dummy import to warm up content_type cache * fix counts * warmup cache better * simplify warmup * warmup cache better
1 parent e0da8ac commit b7992db

6 files changed

+1070
-7
lines changed

docker/entrypoint-unit-tests-devDocker.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ echo "Unit Tests"
7373
echo "------------------------------------------------------------"
7474

7575
# Removing parallel and shuffle for now to maintain stability
76-
python3 manage.py test unittests -v 3 --keepdb --no-input --exclude-tag="non-parallel" || {
77-
exit 1;
78-
}
79-
python3 manage.py test unittests -v 3 --keepdb --no-input --tag="non-parallel" || {
80-
exit 1;
81-
}
76+
# python3 manage.py test unittests -v 3 --keepdb --no-input --exclude-tag="non-parallel" || {
77+
# exit 1;
78+
# }
79+
# python3 manage.py test unittests -v 3 --keepdb --no-input --tag="non-parallel" || {
80+
# exit 1;
81+
# }
8282

8383
# you can select a single file to "test" unit tests
84-
# python3 manage.py test unittests.tools.test_npm_audit_scan_parser.TestNpmAuditParser --keepdb -v 3
84+
python3 manage.py test unittests.test_importers_performance.TestDojoImporterPerformance --keepdb -v 3 &> /app/dev2.log
8585

8686
# or even a single method
8787
# python3 manage.py test unittests.tools.test_npm_audit_scan_parser.TestNpmAuditParser.test_npm_audit_parser_many_vuln_npm7 --keepdb -v 3

dojo/decorators.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import threading
23
from functools import wraps
34

45
from django.conf import settings
@@ -12,6 +13,47 @@
1213
logger = logging.getLogger(__name__)
1314

1415

16+
class ThreadLocalTaskCounter:
17+
def __init__(self):
18+
self._thread_local = threading.local()
19+
20+
def _get_task_list(self):
21+
if not hasattr(self._thread_local, "tasks"):
22+
self._thread_local.tasks = []
23+
return self._thread_local.tasks
24+
25+
def _get_recording(self):
26+
return getattr(self._thread_local, "recording", False)
27+
28+
def start(self):
29+
self._thread_local.recording = True
30+
self._get_task_list().clear()
31+
32+
def stop(self):
33+
self._thread_local.recording = False
34+
35+
def incr(self, task_name, model_id=None, args=None, kwargs=None):
36+
if not self._get_recording():
37+
return
38+
tasks = self._get_task_list()
39+
tasks.append({
40+
"task": task_name,
41+
"id": model_id,
42+
"args": args if args is not None else [],
43+
"kwargs": kwargs if kwargs is not None else {},
44+
})
45+
46+
def get(self):
47+
return len(self._get_task_list())
48+
49+
def get_tasks(self):
50+
return list(self._get_task_list())
51+
52+
53+
# Create a shared instance
54+
dojo_async_task_counter = ThreadLocalTaskCounter()
55+
56+
1557
def we_want_async(*args, func=None, **kwargs):
1658
from dojo.models import Dojo_User
1759
from dojo.utils import get_current_user
@@ -40,6 +82,13 @@ def __wrapper__(*args, **kwargs):
4082
from dojo.utils import get_current_user
4183
user = get_current_user()
4284
kwargs["async_user"] = user
85+
86+
dojo_async_task_counter.incr(
87+
func.__name__,
88+
args=args,
89+
kwargs=kwargs,
90+
)
91+
4392
countdown = kwargs.pop("countdown", 0)
4493
if we_want_async(*args, func=func, **kwargs):
4594
return func.apply_async(args=args, kwargs=kwargs, countdown=countdown)

unittests/dojo_test_case.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,20 @@ def system_settings(
9797
enable_jira_web_hook=False,
9898
disable_jira_webhook_secret=False,
9999
jira_webhook_secret=None,
100+
enable_github=False,
100101
enable_product_tag_inehritance=False,
102+
enable_product_grade=True,
103+
enable_webhooks_notifications=True,
101104
):
102105
ss = System_Settings.objects.get()
103106
ss.enable_jira = enable_jira
104107
ss.enable_jira_web_hook = enable_jira_web_hook
105108
ss.disable_jira_webhook_secret = disable_jira_webhook_secret
106109
ss.jira_webhook_secret = jira_webhook_secret
110+
ss.enable_github = enable_github
107111
ss.enable_product_tag_inheritance = enable_product_tag_inehritance
112+
ss.enable_product_grade = enable_product_grade
113+
ss.enable_webhooks_notifications = enable_webhooks_notifications
108114
ss.save()
109115

110116
def create_product_type(self, name, *args, description="dummy description", **kwargs):

0 commit comments

Comments
 (0)