diff --git a/pyperf/_command.py b/pyperf/_command.py index 592bf5c0..cd09f3fa 100644 --- a/pyperf/_command.py +++ b/pyperf/_command.py @@ -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') @@ -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 diff --git a/pyperf/_process_time.py b/pyperf/_process_time.py index 199dc885..f353f757 100644 --- a/pyperf/_process_time.py +++ b/pyperf/_process_time.py @@ -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: @@ -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__":