Skip to content

Commit

Permalink
feat(staging-api-test): cleanup code and add purl query payload (#2927)
Browse files Browse the repository at this point in the history
- Remove [tools/api-performance-test] (duplicate
one)(https://github.com/google/osv.dev/compare/master...hogo6002:per_test_update?expand=1#diff-499ccd078d10e61f6680b1786bdce171fb5d06c983a13bc49f8caf3fc1be5ab5)
- Add purl payload to test purl related queries.
  • Loading branch information
hogo6002 authored Nov 27, 2024
1 parent 474fbef commit 4ee6306
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2,470 deletions.
49 changes: 46 additions & 3 deletions docker/staging_api_test/perform_api_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@
# Number of `vulnerability get` requests to send per second
VULN_QUERY_BATCH_SIZE = 50
# Number of `version query` requests to send per second
VERSION_QUERY_BATCH_SIZE = 100
VERSION_QUERY_BATCH_SIZE = 80
# Number of `package query` requests to send per second
PACKAGE_QUERY_BATCH_SIZE = 30
PACKAGE_QUERY_BATCH_SIZE = 20
# Number of `purl query` requests to send per second
PURL_QUERY_BATCH_SIZE = 30
# Number of `batch query` requests to send per second
BATCH_QUERY_BATCH_SIZE = 3
# Number of large `batch query` requests to send per second
Expand All @@ -60,6 +62,7 @@ def __init__(self, bug_dict: dict):
# If the package/ecosystem/version value is None, then add a fake value in.
self.package = bug_dict.get('project', 'foo')
self.ecosystem = bug_dict.get('ecosystem', 'foo')
self.purl = bug_dict.get('purl', 'pkg:foo/foo')

# Use the `affected fuzzy` value as the query version.
# If no 'affected fuzzy' is present, assign a default value.
Expand Down Expand Up @@ -260,6 +263,28 @@ def build_version_payload(request_id: str, bug_map: dict) -> dict:
}


def build_purl_payload(request_id: str, bug_map: dict) -> dict:
"""Builds a purl query payload
Args:
request_id:
The bug ID
bug_map:
A dict mapping bug IDs to the corresponding `SimpleBug` objects
Returns:
A dict containing package version query payload, example:
'{"package": {"purl": "pkg:golang/github.com/golang-jwt/jwt/[email protected]"}}'
"""
purl = bug_map[request_id].purl
purl_with_version = f'{purl}@{bug_map[request_id].affected_fuzzy}'

# Use random.choice to select between the two PURL options
chosen_purl = random.choice([purl, purl_with_version])

return {"package": {"purl": chosen_purl,}}


def build_batch_payload(request_ids: list,
bug_map: dict) -> dict[str, list[dict[str, any]]]:
"""Builds a batch query payload
Expand Down Expand Up @@ -294,11 +319,13 @@ def build_batch_payload(request_ids: list,
queries = []
for bug_id in batch_ids:
query = {}
query_type = random.choice(['version', 'package'])
query_type = random.choice(['version', 'package', 'purl'])
if query_type == 'version':
query = build_version_payload(bug_id, bug_map)
elif query_type == 'package':
query = build_package_payload(bug_id, bug_map)
elif query_type == 'purl':
query = build_purl_payload(bug_id, bug_map)
queries.append(query)

return {"queries": [queries]}
Expand Down Expand Up @@ -383,6 +410,21 @@ async def send_package_requests(request_ids: list, bug_map: dict) -> None:
build_package_payload)


async def send_purl_requests(request_ids: list, bug_map: dict) -> None:
"""Sends purl query requests
Args:
request_id:
The bug ID
bug_map:
A dict mapping bug IDs to the corresponding `SimpleBug` objects
"""
url = f'{BASE_URL}/query'
batch_size = PURL_QUERY_BATCH_SIZE
await make_http_requests_async(request_ids, bug_map, url, batch_size,
build_purl_payload)


async def send_vuln_requests(request_ids: list, bug_map: dict) -> None:
"""Sends vulnerability get requests
Expand Down Expand Up @@ -449,6 +491,7 @@ async def main() -> None:
send_vuln_requests(vuln_query_ids, bug_map),
send_package_requests(package_query_ids, bug_map),
send_version_requests(package_query_ids, bug_map),
send_purl_requests(package_query_ids, bug_map),
send_batch_requests(package_query_ids, bug_map, BATCH_QUERY_BATCH_SIZE),
send_batch_requests(large_batch_query_ids, bug_map,
LARGE_BATCH_QUERY_BATCH_SIZE))
Expand Down
3 changes: 2 additions & 1 deletion docker/staging_api_test/retrieve_bugs_from_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def format_bug_for_output(bug: dict[str, any]) -> dict[str, any]:
'db_id': bug['id'],
'project': affected_package.get('name', None),
'ecosystem': affected_package.get('ecosystem', None),
'affected_fuzzy': affected_fuzzy
'purl': affected_package.get('purl', None),
'affected_fuzzy': affected_fuzzy,
}


Expand Down
1 change: 0 additions & 1 deletion tools/api-performance-test/.gitignore

This file was deleted.

Loading

0 comments on commit 4ee6306

Please sign in to comment.