|
| 1 | +# Copyright 2025 The Emscripten Authors. All rights reserved. |
| 2 | +# Emscripten is available under two separate licenses, the MIT license and the |
| 3 | +# University of Illinois/NCSA Open Source License. Both these licenses can be |
| 4 | +# found in the LICENSE file. |
| 5 | + |
| 6 | +"""Stress test versions of some existing tests from test_core.py |
| 7 | +These don't run in test_core.py itself because that is already run in parallel and these |
| 8 | +stress tests each saturate the CPU cores. |
| 9 | +
|
| 10 | +TODO: Find a way to replace these tests with an `@also_with_stress_test` decorator. |
| 11 | +Hopfully we can replace the current parallelism with `taskset -u 0` to force the test |
| 12 | +only run on a single core (would limit the tests to linux-only). |
| 13 | +""" |
| 14 | + |
| 15 | +import multiprocessing |
| 16 | +import subprocess |
| 17 | +import threading |
| 18 | + |
| 19 | +from common import RunnerCore, node_pthreads, is_slow_test, also_with_modularize, NON_ZERO |
| 20 | + |
| 21 | + |
| 22 | +class stress(RunnerCore): |
| 23 | + def parallel_stress_test_js_file(self, js_file, assert_returncode=None, expected=None, not_expected=None): |
| 24 | + # If no expectations were passed, expect a successful run exit code |
| 25 | + if assert_returncode is None and expected is None and not_expected is None: |
| 26 | + assert_returncode = 0 |
| 27 | + |
| 28 | + # We will use Python multithreading, so prepare the command to run in advance, and keep the threading kernel |
| 29 | + # compact to avoid accessing unexpected data/functions across threads. |
| 30 | + cmd = self.get_engine_with_args() + [js_file] |
| 31 | + |
| 32 | + exception_thrown = threading.Event() |
| 33 | + error_lock = threading.Lock() |
| 34 | + error_exception = None |
| 35 | + |
| 36 | + def test_run(): |
| 37 | + nonlocal error_exception |
| 38 | + try: |
| 39 | + # Each thread repeatedly runs the test case in a tight loop, which is critical to coax out timing related issues |
| 40 | + for _ in range(16): |
| 41 | + # Early out from the test, if error was found |
| 42 | + if exception_thrown.is_set(): |
| 43 | + return |
| 44 | + result = subprocess.run(cmd, capture_output=True, text=True) |
| 45 | + |
| 46 | + output = f'\n----------------------------\n{result.stdout}{result.stderr}\n----------------------------' |
| 47 | + if not_expected is not None and not_expected in output: |
| 48 | + raise Exception(f'\n\nWhen running command "{cmd}",\nexpected string "{not_expected}" to NOT be present in output:{output}') |
| 49 | + if expected is not None and expected not in output: |
| 50 | + raise Exception(f'\n\nWhen running command "{cmd}",\nexpected string "{expected}" was not found in output:{output}') |
| 51 | + if assert_returncode is not None: |
| 52 | + if assert_returncode == NON_ZERO: |
| 53 | + if result.returncode != 0: |
| 54 | + raise Exception(f'\n\nCommand "{cmd}" was expected to fail, but did not (returncode=0). Output:{output}') |
| 55 | + elif assert_returncode != result.returncode: |
| 56 | + raise Exception(f'\n\nWhen running command "{cmd}",\nreturn code {result.returncode} does not match expected return code {assert_returncode}. Output:{output}') |
| 57 | + except Exception as e: |
| 58 | + if not exception_thrown.is_set(): |
| 59 | + exception_thrown.set() |
| 60 | + with error_lock: |
| 61 | + error_exception = e |
| 62 | + return |
| 63 | + |
| 64 | + threads = [] |
| 65 | + # Oversubscribe hardware threads to make sure scheduling becomes erratic |
| 66 | + while len(threads) < 2 * multiprocessing.cpu_count() and not exception_thrown.is_set(): |
| 67 | + threads += [threading.Thread(target=test_run)] |
| 68 | + threads[-1].start() |
| 69 | + for t in threads: |
| 70 | + t.join() |
| 71 | + if error_exception: |
| 72 | + raise error_exception |
| 73 | + |
| 74 | + # This is a stress test version that focuses on https://github.com/emscripten-core/emscripten/issues/20067 |
| 75 | + @node_pthreads |
| 76 | + @is_slow_test |
| 77 | + def test_stress_proxy_to_pthread_hello_world(self): |
| 78 | + self.skipTest('Occassionally hangs. https://github.com/emscripten-core/emscripten/issues/20067') |
| 79 | + self.set_setting('ABORT_ON_WASM_EXCEPTIONS') |
| 80 | + self.set_setting('PROXY_TO_PTHREAD') |
| 81 | + self.set_setting('EXIT_RUNTIME') |
| 82 | + js_file = self.build('core/test_hello_world.c') |
| 83 | + self.parallel_stress_test_js_file(js_file, assert_returncode=0, expected='hello, world!', not_expected='error') |
| 84 | + |
| 85 | + # This is a stress test to verify that the Node.js postMessage() vs uncaughtException |
| 86 | + # race does not affect Emscripten execution. |
| 87 | + @node_pthreads |
| 88 | + @is_slow_test |
| 89 | + def test_stress_pthread_abort(self): |
| 90 | + self.set_setting('PROXY_TO_PTHREAD') |
| 91 | + # Add the onAbort handler at runtime during preRun. This means that onAbort |
| 92 | + # handler will only be present in the main thread (much like it would if it |
| 93 | + # was passed in by pre-populating the module object on prior to loading). |
| 94 | + self.add_pre_run("Module.onAbort = () => console.log('My custom onAbort called');") |
| 95 | + self.cflags += ['-sINCOMING_MODULE_JS_API=preRun,onAbort'] |
| 96 | + js_file = self.build('pthread/test_pthread_abort.c') |
| 97 | + self.parallel_stress_test_js_file(js_file, expected='My custom onAbort called') |
| 98 | + # TODO: investigate why adding assert_returncode=NON_ZERO to above doesn't work. |
| 99 | + # Is the test test_pthread_abort still flaky? |
| 100 | + |
| 101 | + @node_pthreads |
| 102 | + @also_with_modularize |
| 103 | + @is_slow_test |
| 104 | + def test_stress_pthread_proxying(self): |
| 105 | + self.skipTest('https://github.com/emscripten-core/emscripten/issues/25026') |
| 106 | + if '-sMODULARIZE' in self.cflags: |
| 107 | + if self.get_setting('WASM') == 0: |
| 108 | + self.skipTest('MODULARIZE + WASM=0 + pthreads does not work (#16794)') |
| 109 | + self.set_setting('EXPORT_NAME=ModuleFactory') |
| 110 | + self.maybe_closure() |
| 111 | + self.set_setting('PROXY_TO_PTHREAD') |
| 112 | + if not self.has_changed_setting('INITIAL_MEMORY'): |
| 113 | + self.set_setting('INITIAL_MEMORY=32mb') |
| 114 | + |
| 115 | + js_file = self.build('pthread/test_pthread_proxying.c') |
| 116 | + self.parallel_stress_test_js_file(js_file, not_expected='running widget 17 on unknown', expected='running widget 17 on worker', assert_returncode=0) |
0 commit comments