diff options
-rw-r--r-- | morphlib/util.py | 15 | ||||
-rw-r--r-- | morphlib/util_tests.py | 15 |
2 files changed, 30 insertions, 0 deletions
diff --git a/morphlib/util.py b/morphlib/util.py index 430fdc20..4558d8b8 100644 --- a/morphlib/util.py +++ b/morphlib/util.py @@ -44,3 +44,18 @@ def indent(string, spaces=4): lines = ['%*s%s' % (spaces, '', line) for line in lines] return '\n'.join(lines) + + +def make_concurrency(cores=None): + '''Return the number of concurrent jobs for make. + + This will be given to make as the -j argument. + + ''' + + n = multiprocessing.cpu_count() if cores is None else cores + # Experimental results (ref. Kinnison) says a factor of 1.5 + # gives about the optimal result for build times, since much of + # builds are I/O bound, not CPU bound. + return max(int(n * 1.5 + 0.5), 1) + diff --git a/morphlib/util_tests.py b/morphlib/util_tests.py index 6637f571..e65d4507 100644 --- a/morphlib/util_tests.py +++ b/morphlib/util_tests.py @@ -42,3 +42,18 @@ class IndentTests(unittest.TestCase): self.assertEqual(morphlib.util.indent('foo\nbar\n'), ' foo\n bar') + +class MakeConcurrencyTests(unittest.TestCase): + + def test_returns_2_for_1_core(self): + self.assertEqual(morphlib.util.make_concurrency(cores=1), 2) + + def test_returns_3_for_2_cores(self): + self.assertEqual(morphlib.util.make_concurrency(cores=2), 3) + + def test_returns_5_for_3_cores(self): + self.assertEqual(morphlib.util.make_concurrency(cores=3), 5) + + def test_returns_6_for_4_cores(self): + self.assertEqual(morphlib.util.make_concurrency(cores=4), 6) + |