Skip to content

Commit

Permalink
feat: measure read/write duration
Browse files Browse the repository at this point in the history
  • Loading branch information
Caceresenzo committed Jan 12, 2024
1 parent 608d120 commit afd1e5a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
34 changes: 9 additions & 25 deletions readwrite/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from .constants import LOGGER
from .handlers.base import Handler

from .utils import measure_duration

class UnknownExtension(ValueError):
def __init__(self, extension: str):
Expand All @@ -24,42 +24,26 @@ def add(self, handler: Handler):
def read(self, path: str, **kwargs):
handler = self.get(path)

LOGGER.debug(
"read - handler=%s path=`%s`",
handler.name, path
)

return handler.read(path, **kwargs)
with measure_duration("read", handler.name, path):
return handler.read(path, **kwargs)

def read_as(self, path: str, extension: str, **kwargs):
handler = self.get(extension)

LOGGER.debug(
"read - handler=%s path=`%s`",
handler.name, path
)

return handler.read(path, **kwargs)
with measure_duration("read_as", handler.name, path):
return handler.read(path, **kwargs)

def write(self, x: typing.Any, path: str, **kwargs):
handler = self.get(path)

LOGGER.debug(
"write - handler=%s path=`%s`",
handler.name, path
)

handler.write(x, path, **kwargs)
with measure_duration("write", handler.name, path):
handler.write(x, path, **kwargs)

def write_as(self, x: typing.Any, path: str, extension: str, **kwargs):
handler = self.get(extension)

LOGGER.debug(
"write - handler=%s path=`%s`",
handler.name, path
)

handler.write(x, path, **kwargs)
with measure_duration("write_as", handler.name, path):
handler.write(x, path, **kwargs)

def get(self, path_or_extension: str):
try:
Expand Down
24 changes: 24 additions & 0 deletions readwrite/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import time
import contextlib

from .constants import LOGGER


@contextlib.contextmanager
def measure_duration(operation: str, handler_name: str, path: str):
start_time = time.time()

LOGGER.debug(
"%s - handler=%s path=`%s`",
operation, handler_name, path
)

yield

end_time = time.time()
duration = end_time - start_time

LOGGER.debug(
"%s - handler=%s path=`%s` duration=%.4fs",
operation, handler_name, path, duration
)

0 comments on commit afd1e5a

Please sign in to comment.