-
Notifications
You must be signed in to change notification settings - Fork 79
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
Make hooks work in bench_command subprocesses #197
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ | |
If resource.getrusage() is available: compute the maximum RSS memory in bytes | ||
per process and writes it into stdout as a second line. | ||
""" | ||
import contextlib | ||
import json | ||
import os | ||
import subprocess | ||
import sys | ||
|
@@ -91,6 +93,27 @@ def bench_process(loops, args, kw, profile_filename=None): | |
return (dt, max_rss) | ||
|
||
|
||
def load_hooks(metadata): | ||
hook_names = [] | ||
while "--hook" in sys.argv: | ||
hook_idx = sys.argv.index("--hook") | ||
hook_name = sys.argv[hook_idx + 1] | ||
hook_names.append(hook_name) | ||
del sys.argv[hook_idx] | ||
del sys.argv[hook_idx] | ||
|
||
if len(hook_names): | ||
# Only import pyperf if we know we have hooks | ||
import pyperf._hooks | ||
|
||
hook_managers = pyperf._hooks.instantiate_selected_hooks(hook_names) | ||
metadata["hooks"] = ", ".join(hook_managers.values()) | ||
else: | ||
hook_managers = {} | ||
|
||
return hook_managers | ||
|
||
|
||
def main(): | ||
# Make sure that the pyperf module wasn't imported | ||
if 'pyperf' in sys.modules: | ||
|
@@ -111,6 +134,9 @@ def main(): | |
else: | ||
profile_filename = None | ||
|
||
metadata = {} | ||
hook_managers = load_hooks(metadata) | ||
|
||
loops = int(sys.argv[1]) | ||
args = sys.argv[2:] | ||
|
||
|
@@ -125,15 +151,21 @@ def main(): | |
kw['stdout'] = devnull | ||
kw['stderr'] = subprocess.STDOUT | ||
|
||
dt, max_rss = bench_process(loops, args, kw, profile_filename) | ||
with contextlib.ExitStack() as stack: | ||
for hook in hook_managers.values(): | ||
stack.enter_context(hook) | ||
dt, max_rss = bench_process(loops, args, kw, profile_filename) | ||
|
||
if devnull is not None: | ||
devnull.close() | ||
|
||
for hook in hook_managers.values(): | ||
hook.teardown(metadata) | ||
|
||
# Write timing in seconds into stdout | ||
print(dt) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mdboom Would you like to extract printing data logic into a single function and then add a comment about the function from the parsing metadata logic at bench_command to explain why we parse that way? |
||
if max_rss: | ||
print(max_rss) | ||
print(max_rss or -1) | ||
print(json.dumps(metadata)) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please explain about the parsing logic.
see: https://github.com/psf/pyperf/pull/197/files#r1718097565 :)