django-DefectDojo icon indicating copy to clipboard operation
django-DefectDojo copied to clipboard

[performance] import-scan: 3,000 Generic findings take ~4 min synchronously in the request (2 writes + 2 auditlog rows + ~6 commits per finding)

Open Ludocius opened this issue 4 weeks ago • 0 comments

Description

POST /api/v2/import-scan/ processes the whole report synchronously inside the HTTP request. For a Generic Findings Import JSON of 3,000 findings (2.8 MB, one endpoint each) the request took 272.8 s and 238.0 s on two runs (~12 findings/s), holding one uWSGI worker for 4 minutes; nginx only tolerates this because uwsgi_read_timeout is set to 1800 s. Most of the time is per-finding database work rather than parsing:

  • each new finding is written twice (INSERT + a full-row UPDATE with every column: 6,000 UPDATE "dojo_finding" SET "title"=..., "date"=... for 6,000 new findings),
  • two auditlog_logentry rows per finding (12,639 inserts),
  • one SELECT COUNT(*) + one SELECT on dojo_endpoint per finding for the endpoint lookup, with UPPER() comparisons that cannot use the plain index,
  • ~6 COMMITs per finding (36,703 commits for 6,000 findings): each finding, endpoint, status, tag assignment and audit entry is its own transaction,
  • 30,833 FOR KEY SHARE lock checks on dojo_finding.

Relevant code: 2.50.1 dojo/importers/default_importer.py (process_findings loop: finding.save() -> endpoints -> tags -> second save(dedupe_option=False)), dev dojo/importers/default_importer.py#L120-L160 (same loop; only deduplication is dispatched to Celery).

Test setup: official image defectdojo/defectdojo-django:2.50.1 + defectdojo-nginx:2.50.1, upstream docker-compose.yml (Postgres 17.6, Redis 7.2, 4 uWSGI processes x 4 threads), Docker Desktop on Windows 11. Data: 6,000 findings created through /api/v2/import-scan/ (two Generic Findings Import reports of 3,000 findings, one endpoint each) plus 30 empty products. Query counts come from django.test.utils.CaptureQueriesContext around django.test.Client requests executed inside the uwsgi container; load numbers from hey -z 20s -c 20 against nginx; memory/CPU from docker stats.

PoC

$ bash scripts/import.sh <token> 1          # curl -F [email protected] .../api/v2/import-scan/
201 238.109108
uwsgi log: POST /api/v2/import-scan/ => generated 1513 bytes in 272788 msecs (HTTP/1.1 201)
uwsgi log: POST /api/v2/import-scan/ => generated 1513 bytes in 238047 msecs (HTTP/1.1 201)

pg_stat_statements after 6000 imported findings:
findings=6000  auditlog_logentry=12639  endpoint_status=6000  COMMIT calls=36703
12639 | INSERT INTO "auditlog_logentry" (...)
 6000 | UPDATE "dojo_finding" SET "title"=$1, "date"=$2, ...          (second write of each new finding)
 6000 | SELECT COUNT(*) FROM "dojo_endpoint" WHERE UPPER(protocol)=UPPER($1) AND UPPER(host)=UPPER($2) ...
 5400 | SELECT "dojo_endpoint".* FROM "dojo_endpoint" WHERE UPPER(protocol)=UPPER($1) ...
docker stats during import: uwsgi cpu=52% mem=447MiB  postgres cpu=56%  celeryworker cpu=25%

DD-PERF-IMPORT

Before / After

Before (per-finding loop, simplified from DefaultImporter.process_findings)

for unsaved_finding in parsed_findings:
    finding = self.construct_finding(unsaved_finding)        # save() #1 -> INSERT + auditlog
    self.process_endpoints(finding, ...)                     # COUNT + SELECT on dojo_endpoint per finding
    self.process_tags(finding)
    finding.save(dedupe_option=False)                        # save() #2 -> full-row UPDATE + auditlog
    new_findings.append(finding)
self.update_import_history(...)

After: batch the write path and keep the request short

# 1. one transaction per chunk instead of ~6 per finding
for chunk in chunked(parsed_findings, 200):
    with transaction.atomic():
        for unsaved_finding in chunk:
            finding = self.construct_finding(unsaved_finding)
            self.process_endpoints(finding, endpoint_cache)      # 3. endpoints resolved once per report (dict keyed by normalized protocol/host/port/path)
            self.process_tags(finding)
            finding.save(dedupe_option=False, update_fields=changed_fields)   # 2. only the columns touched after the INSERT
# 4. audit the import, not every row: skip auditlog inside the importer and write one LogEntry per Test_Import
# 5. return 202 + import id from the view and run the loop in a Celery task, so the uWSGI worker is free right after the upload

Any one of 1-4 is a large constant-factor win on its own; 5 removes the 4-minute request entirely and makes the HTTP timeout configuration irrelevant.

Measured on the 2.50.1 image (commit 8292998); the same code is present on dev at 237458c. Links above point to both.

Ludocius avatar Nov 07 '26 04:11 Ludocius