feat: estimate the time remaining from bytes and observed rate
Build and publish container / build (pull_request) Successful in 2m13s
Build and publish container / build (pull_request) Successful in 2m13s
rsync reports each file's size with %l as it completes, which is all an estimate needs: bytes done over time elapsed is the same arithmetic rsync would do internally, and requires nothing it does not already print. The scan pass now sums those sizes as well as counting files, so both a percentage and an estimate have a real denominator. The rate is measured over a trailing thirty seconds rather than the whole run, so it follows a device that slows down instead of averaging the slowdown away -- which for a card reader that thermally throttles, or a USB link that renegotiates after an hour, is the difference between a useful estimate and a reassuring one. Below two seconds no rate is reported at all. The first handful of files arrive microseconds apart, and dividing by that window produces a rate in the gigabytes per second and an estimate of zero, which is worse than showing nothing. Directory entries are excluded from the byte total as well as the file count. rsync reports them with a 4096 inode size, which across six thousand album directories is several megabytes of transfer that never happens.
This commit is contained in:
@@ -19,9 +19,13 @@ class Terminal(io.StringIO):
|
||||
return True
|
||||
|
||||
|
||||
def run(lines, total=0, out=None):
|
||||
def run(lines, total=0, out=None, bytes_expected=0):
|
||||
out = out or NotATerminal()
|
||||
rsync_progress.main(["--total", str(total)], stream=io.StringIO(lines), out=out)
|
||||
rsync_progress.main(
|
||||
["--total", str(total), "--bytes", str(bytes_expected)],
|
||||
stream=io.StringIO(lines),
|
||||
out=out,
|
||||
)
|
||||
return out.getvalue()
|
||||
|
||||
|
||||
@@ -40,15 +44,15 @@ def test_directories_are_not_counted():
|
||||
"""rsync reports them too, and counting them puts the percentage past 100."""
|
||||
output = run("Artist/\nArtist/Album/\nArtist/Album/track.mp3\n", total=1)
|
||||
|
||||
assert "1 / 1" in output
|
||||
assert "1/1" in output
|
||||
assert "100%" in output
|
||||
|
||||
|
||||
def test_the_percentage_tracks_the_total():
|
||||
output = run("".join(f"A/B/{i}.mp3\n" for i in range(5)), total=10)
|
||||
|
||||
assert "5 / 10" in output
|
||||
assert " 50%" in output
|
||||
assert "5/10" in output
|
||||
assert "50%" in output
|
||||
|
||||
|
||||
def test_without_a_total_it_counts_instead_of_guessing():
|
||||
@@ -64,7 +68,7 @@ def test_a_final_line_is_always_printed():
|
||||
output = run("A/B/one.mp3\n", total=1)
|
||||
|
||||
assert output.endswith("\n")
|
||||
assert "1 / 1" in output
|
||||
assert "1/1" in output
|
||||
|
||||
|
||||
def test_nothing_transferred_still_reports():
|
||||
@@ -102,3 +106,75 @@ def test_long_labels_are_trimmed_from_the_left(text, width, expected):
|
||||
if len(text) > width:
|
||||
assert trimmed.startswith("…")
|
||||
assert text.endswith(trimmed.lstrip("…"))
|
||||
|
||||
|
||||
def test_the_size_and_path_are_parsed():
|
||||
assert rsync_progress.parse("5000 Artist/Album/Track.mp3\n") == (
|
||||
5000,
|
||||
"Artist/Album/Track.mp3",
|
||||
)
|
||||
|
||||
|
||||
def test_a_filename_containing_spaces_survives():
|
||||
"""Splitting on every space would lose most of the library."""
|
||||
assert rsync_progress.parse("1234 Artist/An Album/A Track With Spaces.mp3") == (
|
||||
1234,
|
||||
"Artist/An Album/A Track With Spaces.mp3",
|
||||
)
|
||||
|
||||
|
||||
def test_a_bare_path_is_tolerated():
|
||||
"""In case this is fed --out-format='%n' by something older."""
|
||||
assert rsync_progress.parse("Artist/Album/Track.mp3") == (0, "Artist/Album/Track.mp3")
|
||||
|
||||
|
||||
def test_directory_sizes_do_not_inflate_the_total():
|
||||
"""rsync reports directories with a 4096 inode size, which is several
|
||||
megabytes of nothing across six thousand albums."""
|
||||
output = run("4096 Artist/\n4096 Artist/Album/\n5000 Artist/Album/t.mp3\n", total=1)
|
||||
|
||||
assert "4.9 KiB" in output
|
||||
assert "12" not in output.split("Artist")[0]
|
||||
|
||||
|
||||
def test_a_rate_is_not_reported_until_it_means_something():
|
||||
"""The first files arrive microseconds apart and would give a rate in the
|
||||
gigabytes per second and an ETA of zero."""
|
||||
rate = rsync_progress.Rate()
|
||||
rate.add(100.0, 0)
|
||||
rate.add(100.5, 5_000_000)
|
||||
|
||||
assert rate.per_second() == 0.0
|
||||
|
||||
|
||||
def test_a_rate_over_a_long_enough_window_is_reported():
|
||||
rate = rsync_progress.Rate()
|
||||
rate.add(100.0, 0)
|
||||
rate.add(110.0, 10_000_000)
|
||||
|
||||
assert rate.per_second() == pytest.approx(1_000_000)
|
||||
|
||||
|
||||
def test_the_window_forgets_the_distant_past():
|
||||
"""So the estimate follows a device that slows down rather than averaging
|
||||
the slowdown away."""
|
||||
rate = rsync_progress.Rate(window=30.0)
|
||||
for second in range(0, 100, 10):
|
||||
rate.add(float(second), second * 1_000_000)
|
||||
rate.add(200.0, 100_000_000)
|
||||
|
||||
assert rate.samples[0][0] >= 90.0
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("seconds", "expected"),
|
||||
[(0, "0s"), (45, "45s"), (60, "1m00s"), (1092, "18m12s"), (7500, "2h05m")],
|
||||
)
|
||||
def test_durations_read_without_arithmetic(seconds, expected):
|
||||
assert rsync_progress.human_duration(seconds) == expected
|
||||
|
||||
|
||||
def test_a_summary_is_printed_at_the_end():
|
||||
output = run("5000000 A/B/one.mp3\n", total=1, bytes_expected=5000000)
|
||||
|
||||
assert "copied 4.8 MiB in" in output
|
||||
|
||||
Reference in New Issue
Block a user