Skip to content

Commit

Permalink
Merge pull request #201 from mdboom/improve-documentation-of-subproce…
Browse files Browse the repository at this point in the history
…ss-hooks

Separate subprocess writing/parsing into their own functions
  • Loading branch information
mdboom authored Sep 6, 2024
2 parents 6fa3c0b + ac49b20 commit bf02a8b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
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 worker output: %r" % output)

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):
# Write 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
print(dt, file=out)
print(max_rss or -1, file=out)
json.dump(metadata, fp=out)
print(file=out)


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

0 comments on commit bf02a8b

Please sign in to comment.