Skip to content

Commit

Permalink
Add more memory_info in memory_diff
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Nov 22, 2024
1 parent 5090ee8 commit 6f78625
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
2 changes: 1 addition & 1 deletion acceptance_tests/tests/tests/test_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_headers(app_connection):

def _check_leak_there(response):
print("response=" + json.dumps(response, indent=4))
leaked = {v[0]: v[2] for v in response}
leaked = {v[0]: v[2] for v in response["objgraph.growth"]}
assert "c2cwsgiutils_app.services.LeakedObject" in leaked, leaked.keys()
assert leaked["c2cwsgiutils_app.services.LeakedObject"] == 1

Expand Down
21 changes: 20 additions & 1 deletion c2cwsgiutils/debug/_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import gc
import logging
import os
import re
import time
from collections.abc import Mapping
Expand All @@ -8,6 +9,7 @@
from typing import Any, Callable, cast

import objgraph
import psutil
import pyramid.config
import pyramid.request
import pyramid.response
Expand Down Expand Up @@ -87,6 +89,9 @@ def _dump_memory_diff(request: pyramid.request.Request) -> list[Any]:
gc.collect(i)

objgraph.growth(limit=limit, peak_stats=peak_stats, shortnames=False)
process = psutil.Process(os.getpid())
mem_before = process.memory_info()
start_time = time.time()

response = None
try:
Expand All @@ -96,12 +101,26 @@ def _dump_memory_diff(request: pyramid.request.Request) -> list[Any]:
except HTTPException as ex:
_LOG.debug("response was %s", str(ex))

elapsed_time = time.time() - start_time
del response

for i in range(3):
gc.collect(i)

return objgraph.growth(limit=limit, peak_stats=peak_stats, shortnames=False) # type: ignore
mem_after = process.memory_info()
return {
"memory.growth": {
"rss_kb": (mem_after.rss - mem_before.rss) / 1024,
"vms_kb": (mem_after.vms - mem_before.vms) / 1024,
"shared_kb": (mem_after.shared - mem_before.shared) / 1024,
"text_kb": (mem_after.text - mem_before.text) / 1024,
"lib_kb": (mem_after.lib - mem_before.lib) / 1024,
"data_kb": (mem_after.data - mem_before.data) / 1024,
"dirty_kb": (mem_after.dirty - mem_before.dirty) / 1024,
},
"elapsed_time": elapsed_time,
"objgraph.growth": objgraph.growth(limit=limit, peak_stats=peak_stats, shortnames=False), # type: ignore
}


def _sleep(request: pyramid.request.Request) -> pyramid.response.Response:
Expand Down
38 changes: 34 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ waitress = { version = "3.0.1", optional = true }
scikit-image = { version = "0.24.0", optional = true }
prometheus-client = { version = "0.21.0", optional = true}
pyramid_mako = { version = "1.1.0", optional = true}
psutil = { version = "6.1.0", optional = true}

[tool.poetry.extras]
standard = [
Expand All @@ -118,7 +119,7 @@ standard = [
"pyramid_mako",
]
alembic = ["alembic"]
debug = ["objgraph"]
debug = ["objgraph", "psutil"]
oauth2 = ["pyjwt", "requests-oauthlib"]
sentry = ["sentry-sdk"]
dev = ["waitress"]
Expand All @@ -141,6 +142,7 @@ all = [
"alembic",
# debug
"objgraph",
"psutil",
# oauth2
"pyjwt",
"requests-oauthlib",
Expand Down

0 comments on commit 6f78625

Please sign in to comment.