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-wasmfs.mjs

90 lines · 2739 bytes Raw Blame History
1import sqlite3InitModule from './jswasm/sqlite3-wasmfs.mjs';
2const wMsg = (type,...args)=>{
3 postMessage({type, args});
4};
5wMsg('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*/
11const 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};
31wasmfsDir._ = undefined;
32
33const log = (...args)=>wMsg('log',...args);
34const logErr = (...args)=>wMsg('logErr',...args);
35
36const runTests = function(sqlite3){
37 console.log("Module inited.",sqlite3);
38 const wasm = sqlite3.wasm;
39 const __unlink = wasm.xWrap("sqlite3__wasm_vfs_unlink", "int", ["*","string"]);
40 const unlink = (fn)=>__unlink(0,fn);
41 const pDir = wasmfsDir(wasm);
42 if(pDir) log("Persistent storage:",pDir);
43 else{
44 logErr("Expecting persistent storage in this build.");
45 return;
46 }
47 const scope = wasm.scopedAllocPush();
48 const dbFile = pDir+"/speedtest1.db";
49 const urlParams = new URL(globalThis.location.href).searchParams;
50 const argv = ["speedtest1"];
51 if(urlParams.has('flags')){
52 argv.push(...(urlParams.get('flags').split(',')));
53 let i = argv.indexOf('--vfs');
54 if(i>=0) argv.splice(i,2);
55 }else{
56 argv.push(
57 "--singlethread",
58 "--nomutex",
59 //"--nosync",
60 "--nomemstat",
61 "--size", "10"
62 );
63 }
64
65 if(argv.indexOf('--memdb')>=0){
66 logErr("WARNING: --memdb flag trumps db filename.");
67 }
68 argv.push("--big-transactions"/*important for tests 410 and 510!*/,
69 dbFile);
70 //log("argv =",argv);
71 // These log messages are not emitted to the UI until after main() returns. Fixing that
72 // requires moving the main() call and related cleanup into a timeout handler.
73 if(pDir) unlink(dbFile);
74 log("Starting native app:\n ",argv.join(' '));
75 log("This will take a while and the browser might warn about the runaway JS.",
76 "Give it time...");
77 setTimeout(function(){
78 if(pDir) unlink(dbFile);
79 wasm.xCall('wasm_main', argv.length,
80 wasm.scopedAllocMainArgv(argv));
81 wasm.scopedAllocPop(scope);
82 if(pDir) unlink(dbFile);
83 log("Done running native main()");
84 }, 25);
85}/*runTests()*/;
86
87sqlite3InitModule({
88 print: log,
89 printErr: logErr
90}).then(runTests);