4646from queue import Queue
4747from sys import exit , stderr , stdin , stdout
4848from threading import Event , Lock
49- from time import monotonic
49+ from time import monotonic , time_ns
5050
5151
5252__version__ = '0.0.3'
@@ -233,10 +233,11 @@ def do_checksum(file_path, hash_output_stream, start_offset, end_offset, show_pr
233233
234234 def read_worker ():
235235 # Only one will run
236- nonlocal source_end_offset , show_progress
236+ nonlocal source_end_offset
237237 show_progress_total_size = None
238- show_progress_last_output_time = monotonic ()
238+ show_progress_last_output_time_ns = time_ns ()
239239 show_progress_last_output_pct = 0
240+ show_progress_last_output_pos = start_offset
240241 try :
241242 with ExitStack () as stack :
242243 if file_path == '-' :
@@ -252,7 +253,7 @@ def read_worker():
252253 f .seek (0 )
253254 except OSError :
254255 # Probably `[Errno 29] Illegal seek` when reading from pipe e.g. from pv command
255- show_progress = False
256+ show_progress_total_size = None
256257 else :
257258 show_progress_total_size = end_offset - start_offset
258259
@@ -302,13 +303,23 @@ def read_worker():
302303 del block_data_batch
303304
304305 if show_progress :
305- show_progress_pct = 100 * (block_pos - start_offset ) / show_progress_total_size
306- if monotonic () - show_progress_last_output_time >= 60 or show_progress_pct - show_progress_last_output_pct >= 5 :
307- show_progress_last_output_time = monotonic ()
306+ if show_progress_total_size :
307+ show_progress_pct = (100 * (block_pos - start_offset ) / show_progress_total_size )
308+ else :
309+ show_progress_pct = None # e.g. when processing a pipe we don't know the total size beforehand
310+ now_ns = time_ns ()
311+ if abs (now_ns - show_progress_last_output_time_ns ) >= 60e9 or (show_progress_pct and show_progress_pct - show_progress_last_output_pct >= 5 ):
312+ elapsed_s = (now_ns - show_progress_last_output_time_ns ) / 1e9
313+ bytes_since_last = block_pos - show_progress_last_output_pos
314+ speed_mb_s = bytes_since_last / 2 ** 20 / elapsed_s if elapsed_s > 0 else 0
315+ show_progress_last_output_time_ns = now_ns
308316 show_progress_last_output_pct = show_progress_pct
317+ show_progress_last_output_pos = block_pos
318+ show_progress_total_size_str = f'{ show_progress_total_size / 2 ** 30 :.2f} GB' if show_progress_total_size else '?'
319+ show_progress_pct_str = f'{ show_progress_pct :5.2f} %' if show_progress_pct else '? %'
309320 print (
310- f'Checksum progress: { block_pos / 2 ** 30 :7.2f} GB / { show_progress_total_size / 2 ** 30 :.2f } GB '
311- f' ({ show_progress_pct :5 .2f} %) ' ,
321+ f'Checksum progress: { block_pos / 2 ** 30 :7.2f} GB / { show_progress_total_size_str } '
322+ f' ({ show_progress_pct_str } ) { speed_mb_s :7 .2f} MB/s ' ,
312323 file = stderr ,
313324 flush = True )
314325
0 commit comments