Add basic parallel execution capabilities to readelf tests
authorEli Bendersky <eliben@google.com>
Tue, 6 Mar 2018 20:59:47 +0000 (12:59 -0800)
committerEli Bendersky <eliben@google.com>
Tue, 6 Mar 2018 20:59:47 +0000 (12:59 -0800)
test/run_readelf_tests.py

index 9620810d753c39ae9b113d508431ea04db50359b..7f4631b646e7804db9a830958f94b0bf1cb630be 100755 (executable)
@@ -8,13 +8,15 @@
 # This code is in the public domain
 #-------------------------------------------------------------------------------
 import argparse
-import os
-import sys
-import re
 from difflib import SequenceMatcher
 import logging
+from multiprocessing import Pool
+import os
 import platform
+import re
+import sys
 import time
+
 from utils import run_exe, is_in_rootdir, dump_output_to_temp_files
 
 # Make it possible to run this file from the root dir of pyelftools without
@@ -36,6 +38,7 @@ else:
     if not os.path.exists(READELF_PATH):
         READELF_PATH = 'readelf'
 
+
 def discover_testfiles(rootdir):
     """ Discover test files in the given directory. Yield them one by one.
     """
@@ -76,7 +79,7 @@ def run_test_on_file(filename, verbose=False):
                 exe_path, ' '.join(args)))
             t1 = time.time()
             rc, stdout = run_exe(exe_path, args)
-            testlog.info("....elapsed: %s" % (time.time() - t1,))
+            if verbose: testlog.info("....elapsed: %s" % (time.time() - t1,))
             if rc != 0:
                 testlog.error("@@ aborting - '%s' returned '%s'" % (exe_path, rc))
                 return False
@@ -84,7 +87,7 @@ def run_test_on_file(filename, verbose=False):
         if verbose: testlog.info('....comparing output...')
         t1 = time.time()
         rc, errmsg = compare_output(*stdouts)
-        testlog.info("....elapsed: %s" % (time.time() - t1,))
+        if verbose: testlog.info("....elapsed: %s" % (time.time() - t1,))
         if rc:
             if verbose: testlog.info('.......................SUCCESS')
         else:
@@ -186,15 +189,22 @@ def main():
         usage='usage: %(prog)s [options] [file] [file] ...',
         prog='run_readelf_tests.py')
     argparser.add_argument('files', nargs='*', help='files to run tests on')
+    argparser.add_argument(
+        '--parallel', action='store_true',
+        help='run tests in parallel; always runs all tests w/o verbose')
     argparser.add_argument('-V', '--verbose',
                            action='store_true', dest='verbose',
-                           help='Verbose output')
+                           help='verbose output')
     argparser.add_argument(
         '-k', '--keep-going',
         action='store_true', dest='keep_going',
         help="Run all tests, don't stop at the first failure")
     args = argparser.parse_args()
 
+    if args.parallel:
+        if args.verbose or args.keep_going == False:
+            print('WARNING: parallel mode disables verbosity and always keeps going')
+
     if args.verbose:
         testlog.info('Running in verbose mode')
         testlog.info('Python executable = %s' % sys.executable)
@@ -208,12 +218,19 @@ def main():
     else:
         filenames = sorted(discover_testfiles('test/testfiles_for_readelf'))
 
-    failures = 0
-    for filename in filenames:
-        if not run_test_on_file(filename, verbose=args.verbose):
-            failures += 1
-            if not args.keep_going:
-                break
+    if len(filenames) > 1 and args.parallel:
+        pool = Pool()
+        results = pool.map(
+            run_test_on_file,
+            filenames)
+        failures = results.count(False)
+    else:
+        failures = 0
+        for filename in filenames:
+            if not run_test_on_file(filename, verbose=args.verbose):
+                failures += 1
+                if not args.keep_going:
+                    break
 
     if failures == 0:
         testlog.info('\nConclusion: SUCCESS')