Skip to content

Commit

Permalink
Add active hooks to metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
mdboom committed Jul 10, 2024
1 parent 08d0d9e commit e9f41e8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
10 changes: 6 additions & 4 deletions pyperf/_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,16 @@ def _compute_values(self, values, nvalue,

task_func = self.task_func

hook_managers = []
hook_managers = {}
for hook in get_selected_hooks(args.hook):
try:
hook_managers.append(hook())
hook_managers[hook.__name__] = hook()
except HookError as e:
print(f"ERROR setting up hook '{hook.__name__}:'", file=sys.stderr)
print(str(e), file=sys.stderr)
sys.exit(1)
if len(hook_managers):
self.metadata["hooks"] = ", ".join(hook_managers.keys())

index = 1
inner_loops = self.inner_loops
Expand All @@ -78,7 +80,7 @@ def _compute_values(self, values, nvalue,
break

with contextlib.ExitStack() as stack:
for hook in hook_managers:
for hook in hook_managers.values():
stack.enter_context(hook)
raw_value = task_func(self, self.loops)

Expand Down Expand Up @@ -117,7 +119,7 @@ def _compute_values(self, values, nvalue,

index += 1

for hook in hook_managers:
for hook in hook_managers.values():
hook.teardown(self.metadata)

def collect_metadata(self):
Expand Down
1 change: 1 addition & 0 deletions pyperf/tests/test_perf_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,7 @@ def test_hook(self):
bench = pyperf.Benchmark.load(tmp_name)
metadata = bench.get_metadata()
assert metadata.get("_test_hook", 0) > 0
assert metadata.get("hooks", None) == "_test_hook"


class TestConvert(BaseTestCase, unittest.TestCase):
Expand Down

0 comments on commit e9f41e8

Please sign in to comment.