emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.6k stars 3.28k forks source link

[Question] How to write unit tests for multithreaded WASM? #22242

Closed ravisumit33 closed 2 weeks ago

ravisumit33 commented 1 month ago

Currently we have a single threaded WASM app for which we have written tests in jasmine framework. We want to enable multithreading on the core software engine that we use. That engine creates a static pool of threads when instantiated. When running tests, since tests run at a fast speed there are many instances of web-workers getting created and destroyed. This makes browser go out of memory while running tests which, may be, is occuring because browsers GC isn't able to keep up. What is the ideal way of testing such apps?

Note: We load our wasm itself in a web worker not on main UI thread.

sbc100 commented 1 month ago

You could write your tests in C/C++ using something like gtest (https://github.com/google/googletest) then there would only be one single instance of program and one single worker pool, so no workers should ever need to be destroyed until all tests have been run.

If you want to continue to use jasmine you could still keep one instance of your program running (i.e. just create the module once and run all the tests on the same modue). Again, if there is only one module then there is only one single thread pool and workers will get recycled.

ravisumit33 commented 1 month ago

@sbc100 Porting tests is currently infeasible for us. I was looking more into re-using module instance. Currently, we have a wrapper js (e.g. proxy.js) which has its own logic and internally does importScripts('wasm.js') which instantiates the wasm module. We create new web worker from this wrapper js in each test to create a new instance of the wasm module. To re-use the module, we have to re-use this worker, right? That means we have to clear all the states set in the logic of wrapper js and wasm js used in a run. Doing this in javascript seems difficult, isn't it?

sbc100 commented 1 month ago

How much state cleanup you need to do between tests depends on how your program is structured. If its structured as a library is should be safe to run all the tests against same instance without cleaning up the state. If your program is more of one-shot application with main function than I guess it might not do its own cleanup?