Skip to content

Bump up the total number of ProgressSample items. - #286

Merged
willmcgugan merged 1 commit into
Textualize:masterfrom
lovelydinosaur:patch-1
Sep 16, 2020
Merged

Bump up the total number of ProgressSample items.#286
willmcgugan merged 1 commit into
Textualize:masterfrom
lovelydinosaur:patch-1

Conversation

@lovelydinosaur

Copy link
Copy Markdown
Contributor

When working with rich download progress bars with httpx we were finding the transfer speed jumping around all over the place.

For example, this snippet...

importtempfileimporthttpximportrich.progresswithtempfile.NamedTemporaryFile() asdownload_file:
url="https://speed.hetzner.de/100MB.bin"withhttpx.stream("GET", url) asresponse:
total=int(response.headers["Content-Length"])
withrich.progress.Progress(
"[progress.percentage]{task.percentage:>3.0f}%",
rich.progress.BarColumn(bar_width=None),
rich.progress.DownloadColumn(),
rich.progress.TransferSpeedColumn(),
) asprogress:
download_task=progress.add_task("Download", total=total)
forchunkinresponse.iter_bytes():
download_file.write(chunk)
progress.update(download_task, completed=response.num_bytes_downloaded)

Was resulting in this...

rich-progress

Have now narrowed down the cause:

When calculating transfer speeds, rich stores a ProgressSample every time the progress is updated.
It then averages out the transfer speed based on the progress samples.

The maximum window size of samples that are used is currently set to 30 seconds, or 20 samples, whichever is smaller.
That "maximum of 20 samples" is actually pretty small. (For example reading chunks of 16KB at a download speed of 16MB/s will result in 1000 samples/second)

This PR changes that window size to be 30 seconds, or 1000 samples, whichever is smaller.

Type of changes

  • Bug fix
  • New feature
  • Documentation / docstrings
  • Tests
  • Other - UX

Checklist

  • I've run the latest black with default args on new code.
  • I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate.
  • I've added tests for new code.
  • I accept that @willmcgugan may be pedantic in the code review.

Description

Please describe your changes here. If this fixes a bug, please link to the issue, if possible.

When working with `rich` download progress bars with `httpx` we were finding the transfer speed jumping around all over the place.
For example, this snippet...
```python
import tempfile
import httpx
import rich.progress
with tempfile.NamedTemporaryFile() as download_file:
url = "https://speed.hetzner.de/100MB.bin"
with httpx.stream("GET", url) as response:
total = int(response.headers["Content-Length"])
with rich.progress.Progress(
"[progress.percentage]{task.percentage:>3.0f}%",
rich.progress.BarColumn(bar_width=None),
rich.progress.DownloadColumn(),
rich.progress.TransferSpeedColumn(),
) as progress:
download_task = progress.add_task("Download", total=total)
for chunk in response.iter_bytes():
download_file.write(chunk)
progress.update(download_task, completed=response.num_bytes_downloaded)
```
Was resulting in this...
...
Have now narrowed down the cause:
When calculating transfer speeds, `rich` stores a ProgressSample every time the progress is updated.
It then averages out the transfer speed based on the progress samples.
The maximum window size of samples that are used is currently set to 30 seconds, or 20 samples, whichever is smaller.
That "maximum of 20 samples" is actually pretty small. (For example reading chunks of 16KB at a download speed of 16MB/s will result in 1000 samples/second)
This PR changes that window size to be 30 seconds, or 1000 samples, whichever is smaller.
@codecov

codecovBot commented Sep 16, 2020

Copy link
Copy Markdown

Codecov Report

Merging #286 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@ Coverage Diff @@## master #286 +/- ##
=======================================
Coverage 98.72% 98.72% =======================================
Files 50 50 Lines 4078 4078 =======================================
Hits 4026 4026 Misses 52 52 
FlagCoverage Δ
#unittests98.72% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted FilesCoverage Δ
rich/console.py100.00% <ø> (ø)
rich/progress.py94.66% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a27a3ee...ecd7e51. Read the comment docs.

@willmcgugan

Copy link
Copy Markdown
Member

Sounds reasonable. Thanks.

@willmcgugan
willmcgugan merged commit 45ea141 into Textualize:masterSep 16, 2020
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@lovelydinosaur@willmcgugan