|
32 | 32 |
|
33 | 33 | from pathlib import Path |
34 | 34 | from utilities import SDC_Build_Utilities |
| 35 | +import multiprocessing as mp |
35 | 36 |
|
36 | 37 |
|
37 | 38 | EXAMPLES_TO_SKIP = {'basic_usage_nyse_predict.py'} |
| 39 | +TEST_TIMEOUT = 120 |
38 | 40 |
|
39 | 41 |
|
40 | | -def run_examples(sdc_utils): |
| 42 | +# keep test results global to be visible for async callbacks |
| 43 | +class TestResults(): |
41 | 44 | total = 0 |
42 | | - passed = 0 |
43 | 45 | failed = 0 |
| 46 | + passed = 0 |
44 | 47 | skipped = 0 |
45 | 48 | failed_examples = [] |
46 | 49 |
|
| 50 | + |
| 51 | +def run_single_example(path, sdc_utils): |
| 52 | + str_path = str(path) |
| 53 | + try: |
| 54 | + sdc_utils.log_info(sdc_utils.line_double) |
| 55 | + sdc_utils.run_command(f'python {str_path}') |
| 56 | + except Exception as e: |
| 57 | + raise Exception(str_path).with_traceback(e.__traceback__) |
| 58 | + |
| 59 | + return str_path |
| 60 | + |
| 61 | + |
| 62 | +def normal_handler(test_name): |
| 63 | + TestResults.passed += 1 |
| 64 | + sdc_utils.log_info(f'{test_name} PASSED') |
| 65 | + |
| 66 | + |
| 67 | +def error_handler(error): |
| 68 | + TestResults.failed += 1 |
| 69 | + test_name = str(error).split()[-1] |
| 70 | + sdc_utils.log_info(f'{test_name} FAILED') |
| 71 | + TestResults.failed_examples.append(test_name) |
| 72 | + |
| 73 | + |
| 74 | +def run_examples(sdc_utils): |
| 75 | + |
47 | 76 | os.chdir(str(sdc_utils.examples_path)) |
| 77 | + pool = mp.Pool(max(1, mp.cpu_count())) |
| 78 | + |
| 79 | + task_queue = [] |
48 | 80 | for sdc_example in Path('.').glob('**/*.py'): |
49 | | - total += 1 |
| 81 | + TestResults.total += 1 |
50 | 82 |
|
51 | 83 | if sdc_example.name in EXAMPLES_TO_SKIP: |
52 | | - skipped += 1 |
| 84 | + TestResults.skipped += 1 |
53 | 85 | continue |
54 | 86 |
|
55 | 87 | sdc_example = str(sdc_example) |
| 88 | + task_queue.append(pool.apply_async( |
| 89 | + run_single_example, |
| 90 | + [sdc_example, sdc_utils], |
| 91 | + callback=normal_handler, |
| 92 | + error_callback=error_handler |
| 93 | + )) |
| 94 | + |
| 95 | + for promise in task_queue: |
56 | 96 | try: |
57 | | - sdc_utils.log_info(sdc_utils.line_double) |
58 | | - sdc_utils.run_command(f'python {str(sdc_example)}') |
| 97 | + promise.get(TEST_TIMEOUT) |
59 | 98 | except Exception: |
60 | | - failed += 1 |
61 | | - failed_examples.append(sdc_example) |
62 | | - sdc_utils.log_info(f'{sdc_example} FAILED') |
63 | 99 | traceback.print_exc() |
64 | | - else: |
65 | | - passed += 1 |
66 | | - sdc_utils.log_info(f'{sdc_example} PASSED') |
67 | 100 |
|
68 | | - summary_msg = f'SDC examples summary: {total} RUN, {passed} PASSED, {failed} FAILED, {skipped} SKIPPED' |
| 101 | + pool.close() |
| 102 | + pool.join() |
| 103 | + |
| 104 | + summary_msg = f'SDC examples summary: {TestResults.total} RUN, {TestResults.passed} PASSED, ' \ |
| 105 | + f'{TestResults.failed} FAILED, {TestResults.skipped} SKIPPED' |
69 | 106 | sdc_utils.log_info(summary_msg, separate=True) |
70 | | - for failed_example in failed_examples: |
71 | | - sdc_utils.log_info(f'FAILED: {failed_example}') |
| 107 | + for test_name in TestResults.failed_examples: |
| 108 | + sdc_utils.log_info(f'FAILED: {test_name}') |
72 | 109 |
|
73 | | - if failed > 0: |
| 110 | + if TestResults.failed > 0: |
74 | 111 | sdc_utils.log_info('Intel SDC examples FAILED', separate=True) |
75 | 112 | exit(-1) |
76 | 113 | sdc_utils.log_info('Intel SDC examples PASSED', separate=True) |
|
0 commit comments