Skip to content

Commit daa1374

Browse files
committed
test redhat importer performance by profiling
Signed-off-by: ziad <[email protected]> Signed-off-by: ziad <[email protected]>
1 parent 226a7ad commit daa1374

File tree

2 files changed

+33
-35
lines changed

2 files changed

+33
-35
lines changed

vulnerabilities/management/commands/import.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
# See https://github.com/nexB/vulnerablecode for support or download.
77
# See https://aboutcode.org for more information about nexB OSS projects.
88
#
9-
import threading
109
import traceback
1110

1211
from django.core.management.base import BaseCommand
@@ -42,10 +41,6 @@ def handle(self, *args, **options):
4241
self.import_data(IMPORTERS_REGISTRY.values())
4342
return
4443

45-
if options["all_in_parallel"]:
46-
self.import_data_in_parallel(IMPORTERS_REGISTRY.values())
47-
return
48-
4944
sources = options["sources"]
5045
if not sources:
5146
raise CommandError('Please provide at least one importer to run or use "--all".')
@@ -85,36 +80,6 @@ def import_data(self, importers):
8580
if failed_importers:
8681
raise CommandError(f"{len(failed_importers)} failed!: {','.join(failed_importers)}")
8782

88-
def import_data_in_parallel(self, importers):
89-
failed_importers = []
90-
thread_list = []
91-
for importer in importers:
92-
self.stdout.write(f"Importing data using {importer.qualified_name}")
93-
try:
94-
thread = threading.Thread(
95-
target=ImportRunner(importer).run(), name=importer.qualified_name
96-
)
97-
thread.start()
98-
thread_list.append(thread)
99-
except Exception:
100-
failed_importers.append(importer.qualified_name)
101-
traceback.print_exc()
102-
self.stdout.write(
103-
self.style.ERROR(
104-
f"Failed to run importer {importer.qualified_name}. Continuing..."
105-
)
106-
)
107-
for thread in thread_list:
108-
thread.join()
109-
110-
success_list = {value for value in IMPORTERS_REGISTRY if value not in failed_importers}
111-
if success_list:
112-
self.stdout.write(
113-
self.style.SUCCESS(f"Successfully imported data using {success_list} ")
114-
)
115-
if failed_importers:
116-
raise CommandError(f"{len(failed_importers)} failed!: {','.join(failed_importers)}")
117-
11883

11984
def validate_importers(sources):
12085
importers = []
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#
2+
#
3+
# Copyright (c) nexB Inc. and others. All rights reserved.
4+
# VulnerableCode is a trademark of nexB Inc.
5+
# SPDX-License-Identifier: Apache-2.0
6+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
7+
# See https://github.com/nexB/vulnerablecode for support or download.
8+
# See https://aboutcode.org for more information about nexB OSS projects.
9+
#
10+
import pytest
11+
12+
# this import are used in the script
13+
from vulnerabilities.importers import redhat
14+
15+
script = """for i, data in enumerate(redhat.RedhatImporter().advisory_data()):
16+
if 1 == 100:
17+
break"""
18+
19+
20+
@pytest.mark.skip("Use only for local profiling")
21+
@pytest.mark.django_db
22+
class TestImporter:
23+
def test_redhat_importer_performance_profiling(self):
24+
print_profiling_status(script, "redhat.txt")
25+
26+
27+
def print_profiling_status(test_py, stats_file, top=50):
28+
import cProfile as profile
29+
import pstats
30+
31+
profile.runctx(test_py, globals(), locals(), stats_file)
32+
p = pstats.Stats(stats_file)
33+
p.sort_stats("time").print_stats(top)

0 commit comments

Comments
 (0)