speedtest1-wasmfs.mjs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import sqlite3InitModule from './jswasm/speedtest1-wasmfs.mjs';
  2. const wMsg = (type,...args)=>{
  3. postMessage({type, args});
  4. };
  5. wMsg('log',"speedtest1-wasmfs starting...");
  6. /**
  7. If this environment contains OPFS, this function initializes it and
  8. returns the name of the dir on which OPFS is mounted, else it returns
  9. an empty string.
  10. */
  11. const wasmfsDir = function f(wasmUtil,dirName="/opfs"){
  12. if(undefined !== f._) return f._;
  13. if( !globalThis.FileSystemHandle
  14. || !globalThis.FileSystemDirectoryHandle
  15. || !globalThis.FileSystemFileHandle){
  16. return f._ = "";
  17. }
  18. try{
  19. if(0===wasmUtil.xCallWrapped(
  20. 'sqlite3__wasm_init_wasmfs', 'i32', ['string'], dirName
  21. )){
  22. return f._ = dirName;
  23. }else{
  24. return f._ = "";
  25. }
  26. }catch(e){
  27. // sqlite3_wasm_init_wasmfs() is not available
  28. return f._ = "";
  29. }
  30. };
  31. wasmfsDir._ = undefined;
  32. const log = (...args)=>wMsg('log',...args);
  33. const logErr = (...args)=>wMsg('logErr',...args);
  34. const runTests = function(sqlite3){
  35. console.log("Module inited.",sqlite3);
  36. const wasm = sqlite3.wasm;
  37. const __unlink = wasm.xWrap("sqlite3__wasm_vfs_unlink", "int", ["*","string"]);
  38. const unlink = (fn)=>__unlink(0,fn);
  39. const pDir = wasmfsDir(wasm);
  40. if(pDir) log("Persistent storage:",pDir);
  41. else{
  42. logErr("Expecting persistent storage in this build.");
  43. return;
  44. }
  45. const scope = wasm.scopedAllocPush();
  46. const dbFile = pDir+"/speedtest1.db";
  47. const urlParams = new URL(globalThis.location.href).searchParams;
  48. const argv = ["speedtest1"];
  49. if(urlParams.has('flags')){
  50. argv.push(...(urlParams.get('flags').split(',')));
  51. let i = argv.indexOf('--vfs');
  52. if(i>=0) argv.splice(i,2);
  53. }else{
  54. argv.push(
  55. "--singlethread",
  56. "--nomutex",
  57. //"--nosync",
  58. "--nomemstat",
  59. "--size", "10"
  60. );
  61. }
  62. if(argv.indexOf('--memdb')>=0){
  63. logErr("WARNING: --memdb flag trumps db filename.");
  64. }
  65. argv.push("--big-transactions"/*important for tests 410 and 510!*/,
  66. dbFile);
  67. //log("argv =",argv);
  68. // These log messages are not emitted to the UI until after main() returns. Fixing that
  69. // requires moving the main() call and related cleanup into a timeout handler.
  70. if(pDir) unlink(dbFile);
  71. log("Starting native app:\n ",argv.join(' '));
  72. log("This will take a while and the browser might warn about the runaway JS.",
  73. "Give it time...");
  74. setTimeout(function(){
  75. if(pDir) unlink(dbFile);
  76. wasm.xCall('wasm_main', argv.length,
  77. wasm.scopedAllocMainArgv(argv));
  78. wasm.scopedAllocPop(scope);
  79. if(pDir) unlink(dbFile);
  80. log("Done running native main()");
  81. }, 25);
  82. }/*runTests()*/;
  83. sqlite3InitModule({
  84. print: log,
  85. printErr: logErr
  86. }).then(runTests);