diff --git a/python-libs/poulpe/benchmarks.py b/python-libs/poulpe/benchmarks.py index a48391ea250bb21f1a1f93000e4f2aaafe8df448_cHl0aG9uLWxpYnMvcG91bHBlL2JlbmNobWFya3MucHk=..da72f842c911ccfaea5d069a6422aa5e83435f6b_cHl0aG9uLWxpYnMvcG91bHBlL2JlbmNobWFya3MucHk= 100644 --- a/python-libs/poulpe/benchmarks.py +++ b/python-libs/poulpe/benchmarks.py @@ -1,6 +1,8 @@ import json import os from pathlib import Path +import platform +import resource import shutil import subprocess import sys @@ -252,6 +254,11 @@ # TODO record poulpe version in results in case results accidentally # get bogus for some poulpe version. + # XXX This is only valid once (if you only run a single benchmark), + # and returns the RSS of hyperfine + setup + multiple runs + cleanup + total_rss = self.get_memory_usage() + number_of_runs = len(r["results"][0]["exit_codes"]) + # we should store more res["time"] = {} res["time"]["user"] = r["results"][0]["user"] @@ -261,8 +268,13 @@ res["time"]["standard-deviation"] = r["results"][0]["stddev"] res["time"]["min"] = r["results"][0]["min"] res["time"]["max"] = r["results"][0]["max"] + res["memory"] = {} + res["memory"]["avg-rss-bytes"] = total_rss // number_of_runs + res["meta"] = { + "runs": number_of_runs, + } if runtime_info is not None: # XXX is there a better place to store this? res["runtime_info"] = runtime_info return res @@ -264,8 +276,23 @@ if runtime_info is not None: # XXX is there a better place to store this? res["runtime_info"] = runtime_info return res + def get_memory_usage(self): + try: + max_rss = resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrss + except resource.error: + # "Exceptional circumstance" as per the docs, still we shouldn't + # waste the results because we failed to find the memory usage + max_rss = 0 + + # Linux and *BSD return the value in KibiBytes, Darwin flavors in bytes + if platform.system() == "Darwin": + max_rss.ru_maxrss + else: + max_rss = max_rss * 1024 + return max_rss + def _time_command( self, bin_env_path, @@ -383,7 +410,12 @@ pass r = subprocess.run( - time_cmd, cwd=cwd, env=env, stdout=stdout, stderr=stderr + time_cmd, + cwd=cwd, + env=env, + stdout=stdout, + stderr=stderr, + start_new_session=True, ) if r.returncode != 0: raise errors.BenchmarkRunFailure(