Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure our histogram uses a non-zero step #113

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/news/113.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the generation of histograms when the tests performed zero-byte allocations.
4 changes: 2 additions & 2 deletions src/pytest_memray/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def histogram(
[0, 0, 0, 0, 1, 0, 0, 1, 3, 2]

"""
step = ((high - low) / bins) or low
step = ((high - low) / bins) or low or 1
dist = collections.Counter((x - low) // step for x in iterable)
return [dist[b] for b in range(bins)]

Expand All @@ -91,7 +91,7 @@ def cli_hist(data: Iterable[float], bins: int, *, log_scale: bool = True) -> str
high = max(data)
data_bins = histogram(data, low=low, high=high, bins=bins)
bar_indexes = (int(elem * (len(bars) - 1) / max(data_bins)) for elem in data_bins)
result = " ".join(bars[bar_index] for bar_index in bar_indexes)
result = "".join(bars[bar_index] for bar_index in bar_indexes)
return result


Expand Down
23 changes: 23 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from pytest_memray.utils import WriteEnabledDirectoryAction
from pytest_memray.utils import parse_memory_string
from pytest_memray.plugin import cli_hist


@pytest.mark.parametrize(
Expand Down Expand Up @@ -123,3 +124,25 @@ def test_write_enabled_dir_cannot_create(
w_dir_check(path)
finally:
tmp_path.chmod(tmp_path.stat().st_mode | write)


def test_histogram_with_zero_byte_allocations():
# GIVEN
allocations = [0, 100, 990, 1000, 50000]

# WHEN
histogram = cli_hist(allocations, bins=5)

# THEN
assert histogram == "▄ ▄█▄"


def test_histogram_with_only_zero_byte_allocations():
# GIVEN
allocations = [0, 0, 0, 0]

# WHEN
histogram = cli_hist(allocations, bins=5)

# THEN
assert histogram == "█ "