From c9abc70deb1dc728390d9a06ecb2dbc6268362f2 Mon Sep 17 00:00:00 2001 From: Ben Hagen Date: Fri, 8 Nov 2019 14:55:20 +0100 Subject: [PATCH] Add job speed --- ocopy/cli/ocopy.py | 4 +--- ocopy/copy.py | 7 +++++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/ocopy/cli/ocopy.py b/ocopy/cli/ocopy.py index 1f9ece2..8fff321 100755 --- a/ocopy/cli/ocopy.py +++ b/ocopy/cli/ocopy.py @@ -55,15 +55,13 @@ def cli(overwrite: bool, verify: bool, skip_existing: bool, source: str, destina if len(destinations) != len({get_mount(d) for d in destinations}): click.secho(f"Destinations should all be on different drives.", fg="yellow") - start = time.time() job = CopyJob(Path(source), destinations, overwrite=overwrite, verify=verify, skip_existing=skip_existing) with click.progressbar(job.progress, length=100, item_show_func=lambda name: name) as progress: for _ in progress: pass - stop = time.time() - click.echo(f"\n{size / 1000 / 1000 / (stop - start):.2f} MB/s") + click.echo(f"\n{job.speed / 1000 / 1000:.2f} MB/s") if job.skipped_files: click.secho( diff --git a/ocopy/copy.py b/ocopy/copy.py index 972946a..cbdf556 100755 --- a/ocopy/copy.py +++ b/ocopy/copy.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 import datetime +import time from concurrent.futures import as_completed from concurrent.futures.thread import ThreadPoolExecutor from dataclasses import dataclass @@ -234,6 +235,7 @@ def __init__( self.total_done = 0 self.current_item = None self.finished = False + self._start_time = time.time() if auto_start: self.start() @@ -256,6 +258,11 @@ def _progress_reader(self): def percent_done(self) -> int: return round(100 / self.todo_size * self.total_done) + @property + def speed(self) -> float: + now = time.time() + return (self.total_done / 2 if self.verify else self.total_done) / (now - self._start_time) + @property def progress(self) -> str: for i in range(1, 101):