xref: /sqlite-3.40.0/ext/wasm/speedtest1.html (revision cd0df83c)
1100b496dSstephan<!doctype html>
2100b496dSstephan<html lang="en-us">
3100b496dSstephan  <head>
4100b496dSstephan    <meta charset="utf-8">
5100b496dSstephan    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6100b496dSstephan    <link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
7100b496dSstephan    <link rel="stylesheet" href="common/emscripten.css"/>
8100b496dSstephan    <link rel="stylesheet" href="common/testing.css"/>
9100b496dSstephan    <title>speedtest1.wasm</title>
10100b496dSstephan  </head>
11100b496dSstephan  <body>
12100b496dSstephan    <header id='titlebar'><span>speedtest1.wasm</span></header>
1344a87f08Sstephan    <div>See also: <a href='speedtest1-worker.html'>A Worker-thread variant of this page.</a></div>
14100b496dSstephan    <!-- emscripten bits -->
15100b496dSstephan    <figure id="module-spinner">
16100b496dSstephan      <div class="spinner"></div>
17100b496dSstephan      <div class='center'><strong>Initializing app...</strong></div>
18100b496dSstephan      <div class='center'>
19100b496dSstephan        On a slow internet connection this may take a moment.  If this
20100b496dSstephan        message displays for "a long time", intialization may have
21100b496dSstephan        failed and the JavaScript console may contain clues as to why.
22100b496dSstephan      </div>
23100b496dSstephan    </figure>
24100b496dSstephan    <div class="emscripten" id="module-status">Downloading...</div>
25100b496dSstephan    <div class="emscripten">
26100b496dSstephan      <progress value="0" max="100" id="module-progress" hidden='1'></progress>
27100b496dSstephan    </div><!-- /emscripten bits -->
2844a87f08Sstephan    <div class='warning'>This page starts running the main exe when it loads, which will
2944a87f08Sstephan      block the UI until it finishes! Adding UI controls to manually configure and start it
3044a87f08Sstephan      are TODO.</div>
31dd628ed5Sstephan    </div>
32e66b2681Sstephan    <div class='warning'>Achtung: running it with the dev tools open may
33e66b2681Sstephan      <em>drastically</em> slow it down. For faster results, keep the dev
34e66b2681Sstephan      tools closed when running it!
35dd628ed5Sstephan    </div>
36e66b2681Sstephan    <div>Output is delayed/buffered because we cannot update the UI while the
37e66b2681Sstephan      speedtest is running. Output will appear below when ready...
38e66b2681Sstephan    <div id='test-output'></div>
39100b496dSstephan    <script src="common/SqliteTestUtil.js"></script>
40*cd0df83cSstephan    <script src="jswasm/speedtest1.js"></script>
418fc8b5b3Sstephan    <script>(function(){
428fc8b5b3Sstephan    /**
438fc8b5b3Sstephan       If this environment contains OPFS, this function initializes it and
448fc8b5b3Sstephan       returns the name of the dir on which OPFS is mounted, else it returns
458fc8b5b3Sstephan       an empty string.
468fc8b5b3Sstephan    */
47b5ae85ecSstephan    const wasmfsDir = function f(wasmUtil){
488fc8b5b3Sstephan        if(undefined !== f._) return f._;
498fc8b5b3Sstephan        const pdir = '/persistent';
508fc8b5b3Sstephan        if( !self.FileSystemHandle
518fc8b5b3Sstephan            || !self.FileSystemDirectoryHandle
528fc8b5b3Sstephan            || !self.FileSystemFileHandle){
538fc8b5b3Sstephan            return f._ = "";
548fc8b5b3Sstephan        }
558fc8b5b3Sstephan        try{
568fc8b5b3Sstephan            if(0===wasmUtil.xCallWrapped(
5728ef9bddSstephan                'sqlite3_wasm_init_wasmfs', 'i32', ['string'], pdir
588fc8b5b3Sstephan            )){
598fc8b5b3Sstephan                return f._ = pdir;
608fc8b5b3Sstephan            }else{
618fc8b5b3Sstephan                return f._ = "";
628fc8b5b3Sstephan            }
638fc8b5b3Sstephan        }catch(e){
6428ef9bddSstephan            // sqlite3_wasm_init_wasmfs() is not available
658fc8b5b3Sstephan            return f._ = "";
668fc8b5b3Sstephan        }
678fc8b5b3Sstephan    };
68b5ae85ecSstephan    wasmfsDir._ = undefined;
698fc8b5b3Sstephan
70100b496dSstephan    const eOut = document.querySelector('#test-output');
71dd628ed5Sstephan    const log2 = function(cssClass,...args){
72100b496dSstephan        const ln = document.createElement('div');
73100b496dSstephan        if(cssClass) ln.classList.add(cssClass);
74100b496dSstephan        ln.append(document.createTextNode(args.join(' ')));
75100b496dSstephan        eOut.append(ln);
76100b496dSstephan        //this.e.output.lastElementChild.scrollIntoViewIfNeeded();
77100b496dSstephan    };
78e66b2681Sstephan    const logList = [];
79e66b2681Sstephan    const dumpLogList = function(){
80e66b2681Sstephan        logList.forEach((v)=>log2('',v));
81e66b2681Sstephan        logList.length = 0;
82e66b2681Sstephan    };
83100b496dSstephan    /* can't update DOM while speedtest is running unless we run
84100b496dSstephan       speedtest in a worker thread. */;
858fc8b5b3Sstephan    const log = (...args)=>{
86100b496dSstephan        console.log(...args);
87e66b2681Sstephan        logList.push(args.join(' '));
88100b496dSstephan    };
89100b496dSstephan    const logErr = function(...args){
90100b496dSstephan        console.error(...args);
91e66b2681Sstephan        logList.push('ERROR: '+args.join(' '));
92100b496dSstephan    };
93100b496dSstephan
94b5ae85ecSstephan    const runTests = function(sqlite3){
95b5ae85ecSstephan        const capi = sqlite3.capi, wasm = capi.wasm;
96b5ae85ecSstephan        //console.debug('sqlite3 =',sqlite3);
978fc8b5b3Sstephan        const unlink = wasm.xWrap("sqlite3_wasm_vfs_unlink", "int", ["string"]);
98b5ae85ecSstephan        const pDir = wasmfsDir(wasm);
998fc8b5b3Sstephan        if(pDir){
1008fc8b5b3Sstephan            console.warn("Persistent storage:",pDir);
1018fc8b5b3Sstephan        }
102100b496dSstephan        const scope = wasm.scopedAllocPush();
103b5ae85ecSstephan        let dbFile = pDir+"/speedtest1.db";
1043d645484Sstephan        const urlParams = new URL(self.location.href).searchParams;
105e66b2681Sstephan        const argv = ["speedtest1"];
1063d645484Sstephan        if(urlParams.has('flags')){
1073d645484Sstephan            argv.push(...(urlParams.get('flags').split(',')));
1083d645484Sstephan        }
1093d645484Sstephan
1103d645484Sstephan        let forceSize = 0;
1113d645484Sstephan        if(urlParams.has('vfs')){
1123d645484Sstephan            const vfs = urlParams.get('vfs');
1133d645484Sstephan            if(!capi.sqlite3_vfs_find(vfs)){
1143d645484Sstephan                log2('error',"Unknown VFS:",vfs);
115b5ae85ecSstephan                return;
116b5ae85ecSstephan            }
1173d645484Sstephan            argv.push("--vfs", vfs);
1183d645484Sstephan            log2('',"Using VFS:",vfs);
1193d645484Sstephan            if('kvvfs' === vfs){
1203d645484Sstephan                forceSize = 2;
121b5ae85ecSstephan                dbFile = 'session';
1223d645484Sstephan                log2('warning',"kvvfs VFS: forcing --size",forceSize,
123fa5aac74Sstephan                     "and filename '"+dbFile+"'.");
1243d645484Sstephan                capi.sqlite3_web_kvvfs_clear(dbFile);
125b5ae85ecSstephan            }
126b5ae85ecSstephan        }
1273d645484Sstephan        if(forceSize){
1283d645484Sstephan            argv.push('--size',forceSize);
1293d645484Sstephan        }else{
130b5ae85ecSstephan            [
131b5ae85ecSstephan                'size'
132b5ae85ecSstephan            ].forEach(function(k){
1333d645484Sstephan                const v = urlParams.get(k);
134b5ae85ecSstephan                if(v) argv.push('--'+k, urlParams[k]);
135b5ae85ecSstephan            });
1363d645484Sstephan        }
137e66b2681Sstephan        argv.push(
1388fc8b5b3Sstephan            "--singlethread",
139fa5aac74Sstephan            //"--nomutex",
140fa5aac74Sstephan            //"--nosync",
1413d645484Sstephan            //"--memdb", // note that memdb trumps the filename arg
142e66b2681Sstephan            "--nomemstat"
143e66b2681Sstephan        );
144e66b2681Sstephan        argv.push("--big-transactions"/*important for tests 410 and 510!*/,
145e66b2681Sstephan                  dbFile);
1468fc8b5b3Sstephan        console.log("argv =",argv);
147dd628ed5Sstephan        // These log messages are not emitted to the UI until after main() returns. Fixing that
148dd628ed5Sstephan        // requires moving the main() call and related cleanup into a timeout handler.
149e66b2681Sstephan        if(pDir) unlink(dbFile);
150e66b2681Sstephan        log2('',"Starting native app:\n ",argv.join(' '));
151e66b2681Sstephan        log2('',"This will take a while and the browser might warn about the runaway JS.",
152e66b2681Sstephan             "Give it time...");
153e66b2681Sstephan        logList.length = 0;
154dd628ed5Sstephan        setTimeout(function(){
155b5ae85ecSstephan            wasm.xCall('wasm_main', argv.length,
1568fc8b5b3Sstephan                       wasm.scopedAllocMainArgv(argv));
157100b496dSstephan            wasm.scopedAllocPop(scope);
1588fc8b5b3Sstephan            if(pDir) unlink(dbFile);
159e66b2681Sstephan            logList.unshift("Done running native main(). Output:");
160e66b2681Sstephan            dumpLogList();
161e66b2681Sstephan        }, 50);
162dd628ed5Sstephan    }/*runTests()*/;
163100b496dSstephan
1648fc8b5b3Sstephan    self.sqlite3TestModule.print = log;
165100b496dSstephan    self.sqlite3TestModule.printErr = logErr;
166b94a9860Sstephan    sqlite3InitModule(self.sqlite3TestModule).then(runTests);
1673d645484Sstephan})();</script>
168100b496dSstephan</body>
169100b496dSstephan</html>
170