Skip to content

Tracking cache requests #1566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
improve logs formatting, fix linting
  • Loading branch information
betolink committed Apr 11, 2024
commit d1b5c70585b55f0c522d03bba1ec61b221eb20b7
16 changes: 14 additions & 2 deletions fsspec/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,28 @@ def _reset_stats(self) -> None:
self.miss_count = 0
self.total_requested_bytes = 0

def _log_stats(self) -> str:
"""Return a formatted string of the cache statistics."""
if self.hit_count == 0 and self.miss_count == 0:
# a cache that does nothing, this is for logs only
return ""
return " , %s: %d hits, %d misses, %d total requested bytes" % (
self.name,
self.hit_count,
self.miss_count,
self.total_requested_bytes,
)

def __repr__(self) -> str:
# TODO: use rich for better formatting
return f"""
cache type : {self.__class__.__name__}
<{self.__class__.__name__}:
block size : {self.blocksize}
block count : {self.nblocks}
file size : {self.size}
cache hits : {self.hit_count}
cache misses: {self.miss_count}
total requested bytes: {self.total_requested_bytes}
total requested bytes: {self.total_requested_bytes}>
"""


Expand Down
6 changes: 2 additions & 4 deletions fsspec/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -1847,13 +1847,11 @@ def read(self, length=-1):
out = self.cache._fetch(self.loc, self.loc + length)

logger.debug(
"%s read: %i - %i, total: %i, cache hits: %i, cache misses: %i",
"%s read: %i - %i %s",
self,
self.loc,
self.loc + length,
self.cache.total_requested_bytes,
self.cache.hit_count,
self.cache.miss_count,
self.cache._log_stats(),
)
self.loc += len(out)
return out
Expand Down
4 changes: 1 addition & 3 deletions fsspec/tests/test_caches.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ def test_cache_getitem(Cache_imp):


def test_block_cache_lru():
"""
BlockCache is a cache that stores blocks of data and uses LRU to evict
"""
# BlockCache is a cache that stores blocks of data and uses LRU to evict
block_size = 4
cache = BlockCache(
block_size, letters_fetcher, len(string.ascii_letters), maxblocks=2
Expand Down