Skip to content

Commit

Permalink
Merge pull request #108 from glensc/measure-speed
Browse files Browse the repository at this point in the history
Feature: Measure speed of downloads
  • Loading branch information
glensc authored May 12, 2024
2 parents dfce371 + 9ff10c1 commit 977dd10
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ name = "pypi"

[packages]
fuse-python = "==1.0.7"
humanize = "==4.9.0"
plexapi = "==4.15.7"
requests-cache = "==1.1.1"
sentry-sdk = "==2.1.1"
Expand Down
11 changes: 10 additions & 1 deletion Pipfile.lock

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

23 changes: 23 additions & 0 deletions plexfuse/measure_speed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from __future__ import annotations

from contextlib import contextmanager
from time import monotonic
from typing import TYPE_CHECKING

from humanize import naturalsize

if TYPE_CHECKING:
from pathlib import Path


@contextmanager
def measure_speed(part: Path):
start = monotonic()
print(f"Downloading: {part}")
yield
now = monotonic()
file_size = part.stat().st_size
rate = file_size / (now - start)
speed = naturalsize(rate, binary=True)

print(f"Downloaded in {speed}/s: {part}")
8 changes: 5 additions & 3 deletions plexfuse/vfs/ChunkedFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import TYPE_CHECKING

from plexfuse.cache.FileCache import FileCache
from plexfuse.measure_speed import measure_speed

if TYPE_CHECKING:
from plexfuse.plex.PlexApi import PlexApi
Expand Down Expand Up @@ -44,10 +45,11 @@ def cache_path(self, path: str, chunk_number: int):
cache_path = file.parent / filename

if not cache_path.exists():
print(f"Downloading: {cache_path}")
base_offset = self.base_offset(chunk_number)
self.plex.download_part(path, cache_path,
offset=base_offset, size=self.chunk_size)

with measure_speed(cache_path):
self.plex.download_part(path, cache_path,
offset=base_offset, size=self.chunk_size)

return cache_path

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ authors = [
requires-python = ">= 3.11"
dependencies = [
"fuse-python",
"humanize>=4.9.0",
"plexapi>=4.15",
"requests-cache>=1.1",
"websocket-client>=1.7.0",
Expand Down

0 comments on commit 977dd10

Please sign in to comment.