From 06a413283f1ac5bdbb86bc629351c654176c8c49 Mon Sep 17 00:00:00 2001 From: Ben Jeffery Date: Wed, 9 Oct 2024 16:36:01 +0100 Subject: [PATCH] Address comments --- python/tests/test_provenance.py | 21 +++++++++++---------- python/tskit/provenance.py | 5 ++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/python/tests/test_provenance.py b/python/tests/test_provenance.py index 1c23162d36..b544387d55 100644 --- a/python/tests/test_provenance.py +++ b/python/tests/test_provenance.py @@ -32,7 +32,7 @@ try: import resource except ImportError: - resource = None # resource.getrusage absent on windows + resource = None # resource absent on windows import msprime @@ -221,28 +221,29 @@ def test_libraries(self): class TestGetResources: - def test_used_resources_keys(self): - resources = provenance.get_resources(_start_time) + def test_get_resources_keys(self): + resources = provenance.get_resources(0) assert "elapsed_time" in resources assert "user_time" in resources assert "sys_time" in resources if resource is not None: assert "max_memory" in resources - def test_used_resources_values(self): - resources = provenance.get_resources(_start_time) + def test_get_resources_values(self): + delta = 0.1 + resources = provenance.get_resources(time.time() - delta) assert isinstance(resources["elapsed_time"], float) assert isinstance(resources["user_time"], float) assert isinstance(resources["sys_time"], float) - assert resources["elapsed_time"] > 0.0001 - assert resources["user_time"] > 0.0001 - assert resources["sys_time"] > 0.0001 + assert resources["elapsed_time"] >= delta + assert resources["user_time"] > 0 + assert resources["sys_time"] > 0 if resource is not None: assert isinstance(resources["max_memory"], int) assert resources["max_memory"] > 1024 - def test_used_resources_platform(self): - resources = provenance.get_resources(_start_time) + def test_get_resources_platform(self): + resources = provenance.get_resources(0) if sys.platform != "darwin" and resource is not None: assert resources["max_memory"] % 1024 == 0 diff --git a/python/tskit/provenance.py b/python/tskit/provenance.py index 8e5fe40794..2c24165ae9 100644 --- a/python/tskit/provenance.py +++ b/python/tskit/provenance.py @@ -88,11 +88,10 @@ def get_resources(start_time): "sys_time": times.system + times.children_system, } if resource is not None: - # Don't report max memory on Windows. We could do this using the psutil lib, via - # psutil.Process(os.getpid()).get_ext_memory_info().peak_wset if demand exists + # Don't report max memory on Windows, we would need an external dep like psutil ret["max_memory"] = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss if sys.platform != "darwin": - ret["max_memory"] *= 1024 # Linux, freeBSD et al reports in KB, not bytes + ret["max_memory"] *= 1024 # Linux, freeBSD et al reports in KiB, not bytes return ret