Also record CPU time of subprocesses.

This commit is contained in:
Itamar Turner-Trauring 2023-09-07 17:53:48 -04:00
parent 6aa6c63b05
commit a497b8d86f
2 changed files with 20 additions and 4 deletions

View File

@ -5,11 +5,12 @@ The number of nodes is parameterized via a --number-of-nodes CLI option added
to pytest.
"""
import sys
from resource import getrusage, RUSAGE_CHILDREN
from shutil import which, rmtree
from tempfile import mkdtemp
from contextlib import contextmanager
from time import time
from psutil import Process
import pytest
import pytest_twisted
@ -114,13 +115,27 @@ class Benchmarker:
@contextmanager
def record(self, capsys: pytest.CaptureFixture[str], name, **parameters):
"""Record the timing of running some code, if it succeeds."""
process = Process()
def get_children_cpu_time():
cpu = 0
for subprocess in process.children():
usage = subprocess.cpu_times()
cpu += usage.system + usage.user
return cpu
start_cpu = get_children_cpu_time()
start = time()
yield
elapsed = time() - start
# For now we just print the outcome:
end_cpu = get_children_cpu_time()
elapsed_cpu = end_cpu - start_cpu
# FOR now we just print the outcome:
parameters = " ".join(f"{k}={v}" for (k, v) in parameters.items())
with capsys.disabled():
print(f"\nBENCHMARK RESULT: {name} {parameters} elapsed {elapsed} secs\n")
print(
f"\nBENCHMARK RESULT: {name} {parameters} elapsed={elapsed:.3} (secs) CPU={elapsed_cpu:.3} (secs)\n"
)
@pytest.fixture(scope="session")

View File

@ -435,7 +435,8 @@ setup(name="tahoe-lafs", # also set in __init__.py
"paramiko < 2.9",
"pytest-timeout",
# Does our OpenMetrics endpoint adhere to the spec:
"prometheus-client == 0.11.0"
"prometheus-client == 0.11.0",
"psutil",
] + tor_requires + i2p_requires,
"tor": tor_requires,
"i2p": i2p_requires,