interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / ext / wasm / speedtest1-worker.js

129 lines · 4072 bytes Raw Blame History
1'use strict';
2(function(){
3 let speedtestJs = 'speedtest1.js';
4 const urlParams = new URL(globalThis.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( !globalThis.FileSystemHandle
18 || !globalThis.FileSystemDirectoryHandle
19 || !globalThis.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
37 const mPost = function(msgType,payload){
38 postMessage({type: msgType, data: payload});
39 };
40
41 const App = Object.create(null);
42 App.logBuffer = [];
43 const logMsg = (type,msgArgs)=>{
44 const msg = msgArgs.join(' ');
45 App.logBuffer.push(msg);
46 mPost(type,msg);
47 };
48 const log = (...args)=>logMsg('stdout',args);
49 const logErr = (...args)=>logMsg('stderr',args);
50 const realSahName = 'opfs-sahpool-speedtest1';
51
52 const runSpeedtest = async function(cliFlagsArray){
53 const scope = App.wasm.scopedAllocPush();
54 const dbFile = App.pDir+"/speedtest1.sqlite3";
55 try{
56 const argv = [
57 "speedtest1.wasm", ...cliFlagsArray, dbFile
58 ];
59 App.logBuffer.length = 0;
60 const ndxSahPool = argv.indexOf('opfs-sahpool');
61 if(ndxSahPool>0){
62 argv[ndxSahPool] = realSahName;
63 log("Updated argv for opfs-sahpool: --vfs",realSahName);
64 }
65 mPost('run-start', [...argv]);
66 if(App.sqlite3.installOpfsSAHPoolVfs
67 && !App.sqlite3.$SAHPoolUtil
68 && ndxSahPool>0){
69 log("Installing opfs-sahpool as",realSahName,"...");
70 await App.sqlite3.installOpfsSAHPoolVfs({
71 name: realSahName,
72 initialCapacity: 3,
73 clearOnInit: true,
74 verbosity: 2
75 }).then(PoolUtil=>{
76 log("opfs-sahpool successfully installed as",PoolUtil.vfsName);
77 App.sqlite3.$SAHPoolUtil = PoolUtil;
78 //console.log("sqlite3.oo1.OpfsSAHPoolDb =", App.sqlite3.oo1.OpfsSAHPoolDb);
79 });
80 }
81 App.wasm.xCall('wasm_main', argv.length,
82 App.wasm.scopedAllocMainArgv(argv));
83 log("WASM heap size:",App.wasm.heap8().byteLength,"bytes");
84 log("WASM pointer size:",App.wasm.ptr.size);
85
86 }catch(e){
87 mPost('error',e.message);
88 }finally{
89 App.wasm.scopedAllocPop(scope);
90 mPost('run-end', App.logBuffer.join('\n'));
91 App.logBuffer.length = 0;
92 }
93 };
94
95 globalThis.onmessage = function(msg){
96 msg = msg.data;
97 switch(msg.type){
98 case 'run':
99 runSpeedtest(msg.data || [])
100 .catch(e=>mPost('error',e));
101 break;
102 default:
103 logErr("Unhandled worker message type:",msg.type);
104 break;
105 }
106 };
107
108 const EmscriptenModule = {
109 print: log,
110 printErr: logErr,
111 setStatus: (text)=>mPost('load-status',text)
112 };
113 log("Initializing speedtest1 module...");
114 globalThis.sqlite3InitModule.__isUnderTest = true;
115 globalThis.sqlite3InitModule(EmscriptenModule).then(async (sqlite3)=>{
116 const S = globalThis.S = App.sqlite3 = sqlite3;
117 log("Loaded speedtest1 module. Setting up...");
118 App.pDir = wasmfsDir(S.wasm);
119 App.wasm = S.wasm;
120 log("WASM heap size:",sqlite3.wasm.heap8().byteLength,"bytes");
121 log("WASM pointer size:",sqlite3.wasm.ptr.size);
122 //if(App.pDir) log("Persistent storage:",pDir);
123 //else log("Using transient storage.");
124 mPost('ready',true);
125 log("Registered VFSes:", ...S.capi.sqlite3_js_vfs_list());
126 }).catch(e=>{
127 logErr(e);
128 });
129})();