Skip to content

Instantly share code, notes, and snippets.

@cesarkawakami
Created July 26, 2013 07:40
Show Gist options
  • Save cesarkawakami/6086997 to your computer and use it in GitHub Desktop.
Save cesarkawakami/6086997 to your computer and use it in GitHub Desktop.
import pyramid_zodbconn
import ZODB.serialize
def unwrap(request):
conn = pyramid_zodbconn.get_connection(request)
db = conn.db()
storage = db.storage
storage = getattr(storage, "_BlobStorage__storage", storage)
return conn, db, storage
def size(obj, fs, skip=()):
skip = set(x._p_oid for x in skip)
total = 0
stack = [obj._p_oid]
seen = set(stack)
while stack:
cur_oid = stack.pop()
if cur_oid in skip:
continue
data, _ = fs.load(cur_oid)
total += len(data)
for neighbor_oid in [x for x in ZODB.serialize.referencesf(data) if not x in seen]:
seen.add(neighbor_oid)
stack.append(neighbor_oid)
return total
def cute_size(n):
n = float(n)
suffix = ["B", "KB", "MB", "GB", "TB"]
current_index = 0
n = float(n)
while n >= 1024:
n /= 1024
current_index += 1
return "{:.3f} {}".format(n, suffix[current_index])
def _sz(obj, fs, skip=None):
if skip is None:
try:
skip = [obj.__parent__]
except AttributeError:
skip = []
return cute_size(size(obj, fs, skip))
# total = 402 MB
# omap = 169 MB
# pathindex 65 MB
# oid_path 13 MB
# extent 44 MB
# path_oid 13 MB
# refmap 34 MB
# catalogs = 127 MB
# browser 30 MB
# system 85 MB
# user 12 MB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment