|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import argparse |
| 6 | +import tempfile |
| 7 | +import tarfile |
| 8 | +import glob |
| 9 | +import urllib.request |
| 10 | +import math |
| 11 | +import subprocess |
| 12 | +import shutil |
| 13 | + |
| 14 | +TATUM_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 15 | + |
| 16 | + |
| 17 | +tests = { |
| 18 | + 'basic': [os.path.join(TATUM_ROOT, 'test', 'basic')], |
| 19 | + 'mcnc20': ["http://www.eecg.utoronto.ca/~kmurray/tatum/golden_results/mcnc20_tatum_golden.tar.gz"], |
| 20 | + #'vtr': [ |
| 21 | + |
| 22 | + #], |
| 23 | + #'titan_other': [ |
| 24 | + |
| 25 | + #], |
| 26 | + #'titan': [ |
| 27 | + |
| 28 | + #], |
| 29 | + #'tau15_unit_delay': [ |
| 30 | + |
| 31 | + #], |
| 32 | +} |
| 33 | + |
| 34 | +def parse_args(): |
| 35 | + parser = argparse.ArgumentParser() |
| 36 | + |
| 37 | + parser.add_argument("test_names", |
| 38 | + help="Test to run", |
| 39 | + nargs='+', |
| 40 | + choices=tests.keys()) |
| 41 | + |
| 42 | + parser.add_argument("--tatum_test_exec", |
| 43 | + help="Path to tatum test executable (Default: from path)", |
| 44 | + default=None) |
| 45 | + |
| 46 | + parser.add_argument("--tatum_nworkers", |
| 47 | + help="How many parallel workers each tatum invokation should use. (Default: tatum default)", |
| 48 | + type=int, |
| 49 | + default=None) |
| 50 | + |
| 51 | + parser.add_argument("--tatum_nserial", |
| 52 | + help="How serial runs tatum should perform per test. (Default: %(default))", |
| 53 | + type=int, |
| 54 | + default=3) |
| 55 | + |
| 56 | + parser.add_argument("--tatum_nparallel", |
| 57 | + help="How parallel runs tatum should perform per test. (Default: %(default))", |
| 58 | + type=int, |
| 59 | + default=6) |
| 60 | + |
| 61 | + parser.add_argument("--debug", |
| 62 | + help="Show directory path and don't clean-up", |
| 63 | + default=False, |
| 64 | + action='store_true') |
| 65 | + |
| 66 | + return parser.parse_args() |
| 67 | + |
| 68 | +def main(): |
| 69 | + args = parse_args() |
| 70 | + |
| 71 | + if args.tatum_test_exec: |
| 72 | + args.tatum_test_exec = os.path.abspath(args.tatum_test_exec) |
| 73 | + else: |
| 74 | + args.tatum_test_exec = "tatum_test" #From path |
| 75 | + |
| 76 | + num_failures = 0 |
| 77 | + for test_name in args.test_names: |
| 78 | + for test_url in tests[test_name]: |
| 79 | + work_dir = tempfile.mkdtemp(prefix="tatum_reg_test") |
| 80 | + try: |
| 81 | + test_files = download_extract_test(args, work_dir, test_url) |
| 82 | + num_failures += run_test(args, work_dir, test_name, test_files) |
| 83 | + finally: |
| 84 | + if not args.debug: |
| 85 | + shutil.rmtree(work_dir) |
| 86 | + else: |
| 87 | + print(work_dir) |
| 88 | + |
| 89 | + sys.exit(num_failures) |
| 90 | + |
| 91 | +def run_test(args, work_dir, test_name, test_files): |
| 92 | + num_failed = 0 |
| 93 | + |
| 94 | + os.chdir(work_dir) |
| 95 | + |
| 96 | + print("Running {} ({} tests)".format(test_name, len(test_files))) |
| 97 | + for test_file in test_files: |
| 98 | + |
| 99 | + num_failed += run_single_test(args, work_dir, test_file) |
| 100 | + |
| 101 | + return num_failed; |
| 102 | + |
| 103 | +def run_single_test(args, work_dir, test_file): |
| 104 | + num_failed = 0 |
| 105 | + |
| 106 | + benchmark_name, ext = os.path.splitext(os.path.basename(test_file)) |
| 107 | + |
| 108 | + log = benchmark_name + '.log' |
| 109 | + |
| 110 | + cmd = [] |
| 111 | + cmd += [args.tatum_test_exec] |
| 112 | + cmd += ['--verify', "1"] |
| 113 | + |
| 114 | + cmd += ['--num_serial', str(args.tatum_nserial)] |
| 115 | + cmd += ['--num_parallel', str(args.tatum_nparallel)] |
| 116 | + |
| 117 | + if args.tatum_nworkers: |
| 118 | + cmd += ['--num_workers', str(args.tatum_nworkers)] |
| 119 | + |
| 120 | + cmd += [test_file] |
| 121 | + |
| 122 | + print(" {}".format(test_file), end='') |
| 123 | + |
| 124 | + if args.debug: |
| 125 | + print() |
| 126 | + print(" cmd: {} log: {}".format(' '.join(cmd), log)) |
| 127 | + |
| 128 | + with open(log, 'w') as outfile: |
| 129 | + retcode = subprocess.call(cmd, stdout=outfile, stderr=outfile) |
| 130 | + |
| 131 | + if retcode == 0: |
| 132 | + print(" PASSED") |
| 133 | + else: |
| 134 | + num_failed += 1 |
| 135 | + print(" FAILED") |
| 136 | + |
| 137 | + #Print log if failed |
| 138 | + with open(log, 'r') as log_file: |
| 139 | + for line in log: |
| 140 | + print("\t{}".format(line), end='') |
| 141 | + |
| 142 | + return num_failed |
| 143 | + |
| 144 | +def download_extract_test(args, work_dir, test_url): |
| 145 | + |
| 146 | + test_files = [] |
| 147 | + |
| 148 | + if '.tar' in test_url: |
| 149 | + #A tar file of benchmark files |
| 150 | + benchmark_tar = os.path.join(work_dir, os.path.basename(test_url)) |
| 151 | + |
| 152 | + get_url(test_url, benchmark_tar) |
| 153 | + |
| 154 | + with tarfile.TarFile.open(benchmark_tar, mode="r|*") as tar_file: |
| 155 | + tar_file.extractall(path=work_dir) |
| 156 | + |
| 157 | + test_files += glob.glob("{}/*.tatum".format(work_dir)) |
| 158 | + test_files += glob.glob("{}/*/*.tatum".format(work_dir)) |
| 159 | + else: |
| 160 | + #A directory of benchmark files |
| 161 | + |
| 162 | + test_files += glob.glob("{}/*.tatum".format(test_url)) |
| 163 | + test_files += glob.glob("{}/*/*.tatum".format(test_url)) |
| 164 | + |
| 165 | + return test_files |
| 166 | + |
| 167 | +def get_url(url, filename): |
| 168 | + if '://' in url: |
| 169 | + download_url(url, filename) |
| 170 | + else: |
| 171 | + shutl.copytree(url, filename) |
| 172 | + |
| 173 | +def download_url(url, filename): |
| 174 | + """ |
| 175 | + Downloads the specifed url to filename |
| 176 | + """ |
| 177 | + print("Downloading {} to {}".format(url, filename)) |
| 178 | + urllib.request.urlretrieve(url, filename, reporthook=download_progress_callback) |
| 179 | + |
| 180 | +def download_progress_callback(block_num, block_size, expected_size): |
| 181 | + """ |
| 182 | + Callback for urllib.urlretrieve which prints a dot for every percent of a file downloaded |
| 183 | + """ |
| 184 | + total_blocks = int(math.ceil(expected_size / block_size)) |
| 185 | + progress_increment = int(math.ceil(total_blocks / 100)) |
| 186 | + |
| 187 | + if block_num % progress_increment == 0: |
| 188 | + print(".", end='', flush=False) |
| 189 | + if block_num*block_size >= expected_size: |
| 190 | + print("") |
| 191 | + |
| 192 | +if __name__ == "__main__": |
| 193 | + main() |
0 commit comments