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

Improvements to gas reporting #1020

Merged
merged 3 commits into from
Mar 28, 2021
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 brownie/data/default-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ console:
reports:
exclude_paths: null
exclude_contracts: null
only_include_project: true

hypothesis:
deadline: null
Expand Down
34 changes: 20 additions & 14 deletions brownie/network/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,24 +148,30 @@ def of_address(self, account: str) -> List:
"""Returns a list of transactions where account is the sender or receiver"""
return [i for i in self._list if i.receiver == account or i.sender == account]

def _gas(self, fn_name: str, gas_used: int) -> None:
if fn_name not in self.gas_profile:
self.gas_profile[fn_name] = {
"avg": gas_used,
"high": gas_used,
"low": gas_used,
"count": 1,
}
def _gas(self, fn_name: str, gas_used: int, is_success: bool) -> None:
gas = self.gas_profile.setdefault(fn_name, {})
if not gas:
gas.update(
avg=gas_used, high=gas_used, low=gas_used, count=1, count_success=0, avg_success=0
)
if is_success:
gas["count_success"] = 1
gas["avg_success"] = gas_used
return
gas = self.gas_profile[fn_name]
gas.update(
{
"avg": (gas["avg"] * gas["count"] + gas_used) // (gas["count"] + 1),
"high": max(gas["high"], gas_used),
"low": min(gas["low"], gas_used),
}
avg=(gas["avg"] * gas["count"] + gas_used) // (gas["count"] + 1),
high=max(gas["high"], gas_used),
low=min(gas["low"], gas_used),
)
gas["count"] += 1
if is_success:
count = gas["count_success"]
gas["count_success"] += 1
if not gas["avg_success"]:
gas["avg_success"] = gas_used
else:
avg = gas["avg_success"]
gas["avg_success"] = (avg * count + gas_used) // (count + 1)


class Chain(metaclass=_Singleton):
Expand Down
2 changes: 1 addition & 1 deletion brownie/network/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ def _set_from_receipt(self, receipt: Dict) -> None:
self.coverage_hash = sha1(base.encode()).hexdigest()

if self.fn_name:
state.TxHistory()._gas(self._full_name(), receipt["gasUsed"])
state.TxHistory()._gas(self._full_name(), receipt["gasUsed"], self.status == Status(1))

def _confirm_output(self) -> str:
status = ""
Expand Down
8 changes: 5 additions & 3 deletions brownie/test/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def _build_gas_profile_output():

lines = [""]

only_include_project = CONFIG.settings["reports"]["only_include_project"]
for full_name, values in sorted_gas:
contract, function = full_name.split(".", 1)

Expand All @@ -82,7 +83,8 @@ def _build_gas_profile_output():
continue
except (AttributeError, KeyError):
# filters contracts that are not part of the project
continue
if only_include_project:
continue
if contract in exclude_contracts:
continue

Expand All @@ -108,8 +110,8 @@ def _build_gas_profile_output():
values["avg"] = int(values["avg"])
values = {k: str(v).rjust(padding[k]) for k, v in values.items()}
lines.append(
f" {prefix} {fn_name} - avg: {values['avg']}"
f" low: {values['low']} high: {values['high']}"
f" {prefix} {fn_name} - avg: {values['avg']} avg (confirmed):"
f" {values['avg_success']} low: {values['low']} high: {values['high']}"
)

return lines + [""]
Expand Down
6 changes: 6 additions & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ Settings related to reports such as coverage data and gas profiles.
- SafeMath
- Owned

.. py:attribute:: only_include_project

If ``false``, reports also include contracts imported from outside the active project (such as those compiled via :func:`compile_source <main.compile_source>`).

default value: ``true``

.. _config-hypothesis:

Hypothesis
Expand Down