Skip to content

Commit

Permalink
feat: profile memory usage with memray
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixq committed Jan 6, 2025
1 parent 4ac91a1 commit a83ae8f
Show file tree
Hide file tree
Showing 7 changed files with 587 additions and 259 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,10 @@ run-api-server:
cd gcp/api && docker build -f Dockerfile.esp -t osv/esp:latest .
cd gcp/api && $(install-cmd) && GOOGLE_CLOUD_PROJECT=oss-vdb $(run-cmd) python test_server.py $(HOME)/.config/gcloud/application_default_credentials.json $(ARGS)

profile-api-server:
test -f $(HOME)/.config/gcloud/application_default_credentials.json || (echo "GCP Application Default Credentials not set, try 'gcloud auth login --update-adc'"; exit 1)
cd gcp/api && docker build -f Dockerfile.esp -t osv/esp:latest .
cd gcp/api && $(install-cmd) && GOOGLE_CLOUD_PROJECT=oss-vdb-test $(run-cmd) python test_server.py $(HOME)/.config/gcloud/application_default_credentials.json $(ARGS)

# TODO: API integration tests.
all-tests: lib-tests worker-tests importer-tests alias-tests website-tests vulnfeed-tests
2 changes: 1 addition & 1 deletion docker/staging_api_test/perform_api_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import osv
import osv.logs

BASE_URL = 'https://api.test.osv.dev/v1'
BASE_URL = 'http://localhost:8080/v1'
GCP_PROJECT = 'oss-vdb-test'
BUG_DIR = './all_bugs'

Expand Down
803 changes: 554 additions & 249 deletions gcp/api/poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions gcp/api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ requests = "==2.32.3"
grpcio = "==1.64.1"
grpcio-reflection = "==1.62.2"
grpcio-health-checking = "==1.62.2"
memray = "==1.15.0"

osv = { path = "../../", develop = true }

Expand Down
17 changes: 16 additions & 1 deletion gcp/api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1411,13 +1411,28 @@ def main():
action='store_true',
default=False,
help='If set reflection is enabled to allow debugging with grpcurl.')
parser.add_argument(
'--memray',
action='store_true',
default=False,
help='If set, memray is enabled to profile memory usage.')

args = parser.parse_args()
port = args.port
if not port:
port = int(os.environ.get('PORT', '8000'))

serve(port, args.local)
if args.memray:
import memray

file_path = 'memray-' + datetime.now().strftime('%Y%m%d%H%M%S') + '.bin'

with memray.Tracker(file_path):
logging.info('Starting with memory profiler; output in -> %s', file_path)
serve(port, args.local)

else:
serve(port, args.local)


if __name__ == '__main__':
Expand Down
16 changes: 9 additions & 7 deletions gcp/api/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

_ESP_PORT = 8080
_BACKEND_PORT = 8000
_GCP_PROJECT = 'oss-vdb'
_GCP_PROJECT = 'oss-vdb-test'
_USE_MEMRAY = False


class ServerInstance:
Expand All @@ -45,17 +46,18 @@ def stop(self):
self.backend.kill()


def start_backend(port, log_path):
def start_backend(port, log_path, memray):
"""Start backend server."""
log_handle = open(log_path, 'w')
env = os.environ.copy()
env['GOOGLE_CLOUD_PROJECT'] = _GCP_PROJECT

commands = [sys.executable, 'server.py', f'--port={port}']
if memray:
commands.append('--memray')

backend_proc = subprocess.Popen(
[sys.executable, 'server.py', f'--port={port}'],
env=env,
stdout=log_handle,
stderr=subprocess.STDOUT)
commands, env=env, stdout=log_handle, stderr=subprocess.STDOUT)

return backend_proc

Expand Down Expand Up @@ -164,7 +166,7 @@ def start(credential_path,
esp = None
try:
if not no_backend:
backend = start_backend(_BACKEND_PORT, 'backend.log')
backend = start_backend(_BACKEND_PORT, 'backend.log', _USE_MEMRAY)
esp = start_esp(port, backend_port, credential_path, 'esp.log')
except Exception:
if esp:
Expand Down
2 changes: 1 addition & 1 deletion osv/osv-schema
Submodule osv-schema updated 49 files
+0 −35 .github/workflows/validate-schema.yml
+0 −2 .gitignore
+0 −7 CHANGELOG.md
+0 −114 CHARTER.md
+0 −11 README.md
+17 −98 docs/schema.md
+0 −0 tools/__init__.py
+3 −3 tools/ghsa/convert_ghsa.py
+0 −55 tools/osv-linter/cmd/osv/main.go
+0 −25 tools/osv-linter/go.mod
+0 −49 tools/osv-linter/go.sum
+0 −120 tools/osv-linter/internal/checks/checks.go
+0 −171 tools/osv-linter/internal/checks/packages.go
+0 −96 tools/osv-linter/internal/checks/ranges.go
+0 −54 tools/osv-linter/internal/checks/ranges_test.go
+0 −69 tools/osv-linter/internal/faulttolerant/http.go
+0 −203 tools/osv-linter/internal/linter.go
+0 −373 tools/osv-linter/internal/pkgchecker/ecosystems.go
+0 −39 tools/osv-linter/internal/pkgchecker/ecosystems_test.go
+0 −308 tools/osv-linter/test_data/CVE-2018-5407.json
+0 −54 tools/osv-linter/test_data/CVE-2023-41045.json
+0 −47 tools/osv-linter/test_data/GHSA-9v2f-6vcg-3hgv.json
+0 −1 tools/osv-linter/test_data/GO-2020-0001.json
+0 −1 tools/osv-linter/test_data/GO-2024-2963.json
+0 −134 tools/osv-linter/test_data/PYSEC-2023-74.json
+0 −51 tools/osv-linter/test_data/nointroduced-CVE-2023-41045.json
+0 −319 tools/osv-linter/test_data/nondistinct-CVE-2018-5407.json
+0 −47 tools/osv-linter/test_data/nopackage-GHSA-9v2f-6vcg-3hgv.json
+0 −1 tools/redhat/.gitignore
+0 −8 tools/redhat/.pylintrc
+0 −5 tools/redhat/.style.yapf
+0 −13 tools/redhat/Pipfile
+0 −662 tools/redhat/Pipfile.lock
+0 −33 tools/redhat/README.md
+0 −0 tools/redhat/__init__.py
+0 −40 tools/redhat/convert_redhat.py
+0 −0 tools/redhat/redhat_osv/__init__.py
+0 −33 tools/redhat/redhat_osv/convert_redhat_test.py
+0 −182 tools/redhat/redhat_osv/csaf.py
+0 −25 tools/redhat/redhat_osv/csaf_test.py
+0 −243 tools/redhat/redhat_osv/osv.py
+0 −49 tools/redhat/redhat_osv/osv_test.py
+0 −26 tools/redhat/setup.py
+0 −361 tools/redhat/testdata/RHSA-2024_4546.json
+0 −676 tools/redhat/testdata/rhsa-2003_315.json
+0 −7,529 tools/redhat/testdata/rhsa-2015_0008.json
+0 −570 tools/redhat/testdata/rhsa-2024_4546.json
+1 −7 validation/README.md
+10 −85 validation/schema.json

0 comments on commit a83ae8f

Please sign in to comment.