Skip to content
Open
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
17 changes: 14 additions & 3 deletions asv/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,11 +549,22 @@ def get_profile(self, benchmark_name):

Returns
-------
profile_data : pstats.Stats
Profile data
profile_data : bytes
Raw profile data. Use `get_profile_stats` for a `pstats.Stats`
object.

Raises
------
UserError
If no profile was stored for the benchmark.

"""
profile_data = self._profiles[benchmark_name]
profile_data = self._profiles.get(benchmark_name)
if not profile_data:
raise util.UserError(
f"No profile data available for benchmark '{benchmark_name}'. "
f"Re-run the benchmark with --profile to collect it."
)
profile_data = profile_data.encode('ascii')
profile_bytes = zlib.decompress(base64.b64decode(profile_data))
return profile_bytes
Expand Down
1 change: 1 addition & 0 deletions changelog.d/1614.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Raise a clear error from ``Results.get_profile`` when no profile data was stored, instead of ``KeyError`` or ``AttributeError`` (#1459).
17 changes: 17 additions & 0 deletions test/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,20 @@ def test_profile_python_commit(capsys, basic_conf):
text, err = capsys.readouterr()

assert "Profile data does not already exist" not in text


def test_get_profile_missing_data():
# A benchmark with no stored profile should report that clearly instead of
# raising KeyError or AttributeError from inside get_profile.
from asv.results import Results

results = Results.unnamed()

with pytest.raises(util.UserError, match="No profile data available"):
results.get_profile("time_absent")

# add_result only stores a profile when it is truthy, so a falsy entry is
# reachable too.
results._profiles["time_nulled"] = None
with pytest.raises(util.UserError, match="No profile data available"):
results.get_profile("time_nulled")