bpo-34060: Report system load when running test suite for Windows (GH-8357)
While Windows exposes the system processor queue length, the raw value
used for load calculations on Unix systems, it does not provide an API
to access the averaged value. Hence to calculate the load we must track
and average it ourselves. We can't use multiprocessing or a thread to
read it in the background while the tests run since using those would
conflict with test_multiprocessing and test_xxsubprocess.
Thus, we use Window's asynchronous IO API to run the tracker in the
background with it sampling at the correct rate. When we wish to access
the load we check to see if there's new data on the stream, if there is,
we update our load values.
diff --git a/Lib/test/libregrtest/main.py b/Lib/test/libregrtest/main.py
index 32ac440..18ef6d0 100644
--- a/Lib/test/libregrtest/main.py
+++ b/Lib/test/libregrtest/main.py
@@ -146,8 +146,8 @@
line = f"[{line}] {test}"
# add the system load prefix: "load avg: 1.80 "
- if hasattr(os, 'getloadavg'):
- load_avg_1min = os.getloadavg()[0]
+ if self.getloadavg:
+ load_avg_1min = self.getloadavg()
line = f"load avg: {load_avg_1min:.2f} {line}"
# add the timestamp prefix: "0:01:05 "
@@ -616,6 +616,19 @@
self.list_cases()
sys.exit(0)
+ self.getloadavg = None
+ # If we're on windows and this is the parent runner (not a worker),
+ # report the load average.
+ if hasattr(os, 'getloadavg'):
+ def getloadavg_1m():
+ return os.getloadavg()[0]
+ self.getloadavg = getloadavg_1m
+ elif sys.platform == 'win32' and (self.ns.worker_args is None):
+ from test.libregrtest.win_utils import WindowsLoadTracker
+
+ load_tracker = WindowsLoadTracker()
+ self.getloadavg = load_tracker.getloadavg
+
self.run_tests()
self.display_result()