Optimizes niceness and cpulimit usage

This commit is contained in:
Javanaut
2026-04-11 16:21:17 +02:00
parent 358ef18f77
commit 52c6462fa8
7 changed files with 164 additions and 27 deletions

View File

@@ -15,6 +15,9 @@ from ffx.process import ( # noqa: E402
COMMAND_NOT_FOUND_RETURN_CODE,
COMMAND_TIMED_OUT_RETURN_CODE,
executeProcess,
getWrappedCommandSequence,
normalizeCpuPercent,
normalizeNiceness,
)
@@ -47,6 +50,39 @@ class ProcessTests(unittest.TestCase):
self.assertIn("Command timed out", err)
self.assertIn(sys.executable, err)
def test_get_wrapped_command_sequence_leaves_command_unwrapped_when_limits_disabled(self):
wrapped = getWrappedCommandSequence(
["ffmpeg", "-i", "input.mkv"],
context={"resource_limits": {"niceness": None, "cpu_percent": None}},
)
self.assertEqual(["ffmpeg", "-i", "input.mkv"], wrapped)
def test_get_wrapped_command_sequence_wraps_nice_when_configured(self):
wrapped = getWrappedCommandSequence(
["ffmpeg", "-i", "input.mkv"],
context={"resource_limits": {"niceness": 5, "cpu_percent": None}},
)
self.assertEqual(["nice", "-n", "5", "ffmpeg", "-i", "input.mkv"], wrapped)
def test_get_wrapped_command_sequence_wraps_cpulimit_around_nice_when_both_configured(self):
wrapped = getWrappedCommandSequence(
["ffmpeg", "-i", "input.mkv"],
context={"resource_limits": {"niceness": 5, "cpu_percent": 42}},
)
self.assertEqual(
["cpulimit", "-l", "42", "--", "nice", "-n", "5", "ffmpeg", "-i", "input.mkv"],
wrapped,
)
def test_normalize_niceness_accepts_disabled_sentinel(self):
self.assertIsNone(normalizeNiceness(99))
def test_normalize_cpu_percent_accepts_disabled_sentinel(self):
self.assertIsNone(normalizeCpuPercent(0))
if __name__ == "__main__":
unittest.main()