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

Separate subprocess writing/parsing into their own functions #201

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 21 additions & 9 deletions pyperf/_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@
from pyperf._worker import WorkerTask


def parse_subprocess_data(output):
# Parse the data send from the subprocess.
# It is three lines containing:
# - The runtime (in seconds)
# - max_rss (or -1, if not able to compute)
# - The metadata to add to the benchmark entry, as a JSON dictionary

rss = None
metadata = {}
try:
lines = output.splitlines()
timing = float(lines[0])
rss = int(lines[1])
metadata = json.loads(lines[2])
except ValueError:
raise ValueError("failed to parse script output: %r" % output)
mdboom marked this conversation as resolved.
Show resolved Hide resolved

return timing, rss, metadata


def bench_command(command, task, loops):
path = os.path.dirname(__file__)
script = os.path.join(path, '_process_time.py')
Expand All @@ -23,15 +43,7 @@ def bench_command(command, task, loops):
raise Exception("Command failed with exit code %s"
% proc.returncode)

rss = None
metadata = {}
try:
lines = output.splitlines()
timing = float(lines[0])
rss = int(lines[1])
metadata = json.loads(lines[2])
except ValueError:
raise ValueError("failed to parse script output: %r" % output)
timing, rss, metadata = parse_subprocess_data(output)

if rss and rss > 0:
# store the maximum
Expand Down
17 changes: 13 additions & 4 deletions pyperf/_process_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ def load_hooks(metadata):
return hook_managers


def write_data(dt, max_rss, metadata, out=sys.stdout):
# The data that is communicated back to the main orchestration process.
# It is three lines containing:
# - The runtime (in seconds)
# - max_rss (or -1, if not able to compute)
# - The metadata to add to the benchmark entry, as a JSON dictionary
# Write timing in seconds into stdout
print(dt, file=out)
print(max_rss or -1, file=out)
print(json.dumps(metadata), file=out)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you call .dump() on sys.stdout?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that makes sense.



def main():
# Make sure that the pyperf module wasn't imported
if 'pyperf' in sys.modules:
Expand Down Expand Up @@ -162,10 +174,7 @@ def main():
for hook in hook_managers.values():
hook.teardown(metadata)

# Write timing in seconds into stdout
print(dt)
print(max_rss or -1)
print(json.dumps(metadata))
write_data(dt, max_rss, metadata)


if __name__ == "__main__":
Expand Down