Skip to content

Commit

Permalink
feat: add cbor, signing, allocators
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Bluhm <dbluhm@pm.me>
  • Loading branch information
dbluhm committed Jul 6, 2024
1 parent 03f02dc commit 14ecb9e
Show file tree
Hide file tree
Showing 8 changed files with 1,394 additions and 308 deletions.
43 changes: 41 additions & 2 deletions pdm.lock

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

7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ authors = [
{name = "Daniel Bluhm", email = "dbluhm@pm.me"},
]
dependencies = []
requires-python = ">=3.9"
requires-python = ">=3.10"
readme = "README.md"
license = {text = "Apache-2.0"}

[project.optional-dependencies]
cbor = [
"cbor2>=5.6.4"
]

[build-system]
requires = ["pdm-backend"]
build-backend = "pdm.backend"
Expand Down
91 changes: 91 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import time
import tracemalloc
import linecache
import os


class Timer:
"""Timer for timing operations."""

def __init__(self):
self.start = None
self.end = None

def __enter__(self):
self.start = time.time()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.end = time.time()
return False

@property
def time(self) -> float:
if self.start is None or self.end is None:
raise ValueError("Timer is still running!")
return self.end - self.start


def display_top_memory(snapshot, key_type="lineno", limit=3):
snapshot = snapshot.filter_traces(
(
tracemalloc.Filter(False, "<frozen importlib._bootstrap>"),
tracemalloc.Filter(False, "<unknown>"),
)
)
top_stats = snapshot.statistics(key_type)

print("Top %s lines" % limit)
for index, stat in enumerate(top_stats[:limit], 1):
frame = stat.traceback[0]
# replace "/path/to/module/file.py" with "module/file.py"
filename = os.sep.join(frame.filename.split(os.sep)[-2:])
print("#%s: %s:%s: %.1f KiB" % (index, filename, frame.lineno, stat.size / 1024))
line = linecache.getline(frame.filename, frame.lineno).strip()
if line:
print(" %s" % line)

other = top_stats[limit:]
if other:
size = sum(stat.size for stat in other)
print("%s other: %.1f KiB" % (len(other), size / 1024))
total = sum(stat.size for stat in top_stats)
print("Total allocated size: %.1f KiB" % (total / 1024))


def total_memory_usage(snapshot, key_type="lineno"):
"""Extract total memory usage in KiB."""
snapshot = snapshot.filter_traces(
(
tracemalloc.Filter(False, "<frozen importlib._bootstrap>"),
tracemalloc.Filter(False, "<unknown>"),
)
)
top_stats = snapshot.statistics(key_type)
total = sum(stat.size for stat in top_stats)
return total / 1024


class MemoryTracer:
def __init__(self):
self.snapshot = None

def __enter__(self):
tracemalloc.start()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.snapshot = tracemalloc.take_snapshot()
tracemalloc.clear_traces()
return False

def display_top(self):
if self.snapshot is None:
raise ValueError("Memory tracing still running!")
display_top_memory(self.snapshot)

@property
def total(self):
if self.snapshot is None:
raise ValueError("Memory tracing still running!")
return total_memory_usage(self.snapshot)
Loading

0 comments on commit 14ecb9e

Please sign in to comment.