Snapshot of upstream SQLite 3.43.2
[sqlcipher.git] / ext / wasm / speedtest1-worker.js
blobdedc320292780742207fa072b832b11a4339f4f2
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;
8 importScripts(speedtestJs);
9 /**
10 If this environment contains OPFS, this function initializes it and
11 returns the name of the dir on which OPFS is mounted, else it returns
12 an empty string.
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._ = "";
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._ = "";
30 }catch(e){
31 // sqlite3_wasm_init_wasmfs() is not available
32 return f._ = "";
35 wasmfsDir._ = undefined;
37 const mPost = function(msgType,payload){
38 postMessage({type: msgType, data: payload});
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);
48 const log = (...args)=>logMsg('stdout',args);
49 const logErr = (...args)=>logMsg('stderr',args);
51 const runSpeedtest = async function(cliFlagsArray){
52 const scope = App.wasm.scopedAllocPush();
53 const dbFile = App.pDir+"/speedtest1.sqlite3";
54 try{
55 const argv = [
56 "speedtest1.wasm", ...cliFlagsArray, dbFile
58 App.logBuffer.length = 0;
59 const ndxSahPool = argv.indexOf('opfs-sahpool');
60 const realSahName = 'opfs-sahpool-speedtest1';
61 if(ndxSahPool>0){
62 argv[ndxSahPool] = realSahName;
63 log("Updated argv for opfs-sahpool: --vfs",realSahName);
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",realSahName);
77 App.sqlite3.$SAHPoolUtil = PoolUtil;
78 //console.log("sqlite3.oo1.OpfsSAHPoolDb =", App.sqlite3.oo1.OpfsSAHPoolDb);
79 });
81 App.wasm.xCall('wasm_main', argv.length,
82 App.wasm.scopedAllocMainArgv(argv));
83 }catch(e){
84 mPost('error',e.message);
85 }finally{
86 App.wasm.scopedAllocPop(scope);
87 mPost('run-end', App.logBuffer.join('\n'));
88 App.logBuffer.length = 0;
92 self.onmessage = function(msg){
93 msg = msg.data;
94 switch(msg.type){
95 case 'run':
96 runSpeedtest(msg.data || [])
97 .catch(e=>mPost('error',e));
98 break;
99 default:
100 logErr("Unhandled worker message type:",msg.type);
101 break;
105 const sahpSanityChecks = function(sqlite3){
106 log("Attempting OpfsSAHPoolDb sanity checks...");
107 const db = new sqlite3.oo1.OpfsSAHPoolDb('opfs-sahpoool.db');
108 const fn = db.filename;
109 db.exec([
110 'create table t(a);',
111 'insert into t(a) values(1),(2),(3);'
113 db.close();
114 sqlite3.wasm.sqlite3_wasm_vfs_unlink(sqlite3_vfs_find("opfs-sahpool"), fn);
115 log("SAH sanity checks done.");
118 const EmscriptenModule = {
119 print: log,
120 printErr: logErr,
121 setStatus: (text)=>mPost('load-status',text)
123 log("Initializing speedtest1 module...");
124 self.sqlite3InitModule(EmscriptenModule).then(async (sqlite3)=>{
125 const S = globalThis.S = App.sqlite3 = sqlite3;
126 log("Loaded speedtest1 module. Setting up...");
127 App.vfsUnlink = function(pDb, fname){
128 const pVfs = S.wasm.sqlite3_wasm_db_vfs(pDb, 0);
129 if(pVfs) S.wasm.sqlite3_wasm_vfs_unlink(pVfs, fname||0);
131 App.pDir = wasmfsDir(S.wasm);
132 App.wasm = S.wasm;
133 //if(App.pDir) log("Persistent storage:",pDir);
134 //else log("Using transient storage.");
135 mPost('ready',true);
136 log("Registered VFSes:", ...S.capi.sqlite3_js_vfs_list());
137 }).catch(e=>{
138 logErr(e);
140 })();