speedtest1-worker.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. 'use strict';
  2. (function(){
  3. let speedtestJs = 'speedtest1.js';
  4. const urlParams = new URL(self.location.href).searchParams;
  5. if(urlParams.has('sqlite3.dir')){
  6. speedtestJs = urlParams.get('sqlite3.dir') + '/' + speedtestJs;
  7. }
  8. importScripts(speedtestJs);
  9. /**
  10. If this build includes WASMFS, this function initializes it and
  11. returns the name of the dir on which OPFS is mounted, else it
  12. returns an empty string.
  13. */
  14. const wasmfsDir = function f(wasmUtil){
  15. if(undefined !== f._) return f._;
  16. const pdir = '/opfs';
  17. if( !self.FileSystemHandle
  18. || !self.FileSystemDirectoryHandle
  19. || !self.FileSystemFileHandle){
  20. return f._ = "";
  21. }
  22. try{
  23. if(0===wasmUtil.xCallWrapped(
  24. 'sqlite3_wasm_init_wasmfs', 'i32', ['string'], pdir
  25. )){
  26. return f._ = pdir;
  27. }else{
  28. return f._ = "";
  29. }
  30. }catch(e){
  31. // sqlite3_wasm_init_wasmfs() is not available
  32. return f._ = "";
  33. }
  34. };
  35. wasmfsDir._ = undefined;
  36. const mPost = function(msgType,payload){
  37. postMessage({type: msgType, data: payload});
  38. };
  39. const App = Object.create(null);
  40. App.logBuffer = [];
  41. const logMsg = (type,msgArgs)=>{
  42. const msg = msgArgs.join(' ');
  43. App.logBuffer.push(msg);
  44. mPost(type,msg);
  45. };
  46. const log = (...args)=>logMsg('stdout',args);
  47. const logErr = (...args)=>logMsg('stderr',args);
  48. const realSahName = 'opfs-sahpool-speedtest1';
  49. const runSpeedtest = async function(cliFlagsArray){
  50. const scope = App.wasm.scopedAllocPush();
  51. const dbFile = App.pDir+"/speedtest1.sqlite3";
  52. try{
  53. const argv = [
  54. "speedtest1.wasm", ...cliFlagsArray, dbFile
  55. ];
  56. App.logBuffer.length = 0;
  57. const ndxSahPool = argv.indexOf('opfs-sahpool');
  58. if(ndxSahPool>0){
  59. argv[ndxSahPool] = realSahName;
  60. log("Updated argv for opfs-sahpool: --vfs",realSahName);
  61. }
  62. mPost('run-start', [...argv]);
  63. if(App.sqlite3.installOpfsSAHPoolVfs
  64. && !App.sqlite3.$SAHPoolUtil
  65. && ndxSahPool>0){
  66. log("Installing opfs-sahpool as",realSahName,"...");
  67. await App.sqlite3.installOpfsSAHPoolVfs({
  68. name: realSahName,
  69. initialCapacity: 3,
  70. clearOnInit: true,
  71. verbosity: 2
  72. }).then(PoolUtil=>{
  73. log("opfs-sahpool successfully installed as",PoolUtil.vfsName);
  74. App.sqlite3.$SAHPoolUtil = PoolUtil;
  75. //console.log("sqlite3.oo1.OpfsSAHPoolDb =", App.sqlite3.oo1.OpfsSAHPoolDb);
  76. });
  77. }
  78. App.wasm.xCall('wasm_main', argv.length,
  79. App.wasm.scopedAllocMainArgv(argv));
  80. }catch(e){
  81. mPost('error',e.message);
  82. }finally{
  83. App.wasm.scopedAllocPop(scope);
  84. mPost('run-end', App.logBuffer.join('\n'));
  85. App.logBuffer.length = 0;
  86. }
  87. };
  88. self.onmessage = function(msg){
  89. msg = msg.data;
  90. switch(msg.type){
  91. case 'run':
  92. runSpeedtest(msg.data || [])
  93. .catch(e=>mPost('error',e));
  94. break;
  95. default:
  96. logErr("Unhandled worker message type:",msg.type);
  97. break;
  98. }
  99. };
  100. const EmscriptenModule = {
  101. print: log,
  102. printErr: logErr,
  103. setStatus: (text)=>mPost('load-status',text)
  104. };
  105. log("Initializing speedtest1 module...");
  106. self.sqlite3InitModule(EmscriptenModule).then(async (sqlite3)=>{
  107. const S = globalThis.S = App.sqlite3 = sqlite3;
  108. log("Loaded speedtest1 module. Setting up...");
  109. App.pDir = wasmfsDir(S.wasm);
  110. App.wasm = S.wasm;
  111. //if(App.pDir) log("Persistent storage:",pDir);
  112. //else log("Using transient storage.");
  113. mPost('ready',true);
  114. log("Registered VFSes:", ...S.capi.sqlite3_js_vfs_list());
  115. }).catch(e=>{
  116. logErr(e);
  117. });
  118. })();