xref: /sqlite-3.40.0/ext/wasm/speedtest1.html (revision 8948fbee)
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>
40cd0df83cSstephan    <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){
95*8948fbeeSstephan        const capi = sqlite3.capi, wasm = sqlite3.wasm;
96b5ae85ecSstephan        //console.debug('sqlite3 =',sqlite3);
97b5ae85ecSstephan        const pDir = wasmfsDir(wasm);
988fc8b5b3Sstephan        if(pDir){
998fc8b5b3Sstephan            console.warn("Persistent storage:",pDir);
1008fc8b5b3Sstephan        }
101100b496dSstephan        const scope = wasm.scopedAllocPush();
102b5ae85ecSstephan        let dbFile = pDir+"/speedtest1.db";
1033d645484Sstephan        const urlParams = new URL(self.location.href).searchParams;
104e66b2681Sstephan        const argv = ["speedtest1"];
1053d645484Sstephan        if(urlParams.has('flags')){
1063d645484Sstephan            argv.push(...(urlParams.get('flags').split(',')));
1073d645484Sstephan        }
1083d645484Sstephan
1093d645484Sstephan        let forceSize = 0;
1108a8244b5Sstephan        let vfs, pVfs = 0;
1113d645484Sstephan        if(urlParams.has('vfs')){
1128a8244b5Sstephan            vfs = urlParams.get('vfs');
1138a8244b5Sstephan            pVfs = capi.sqlite3_vfs_find(vfs);
1148a8244b5Sstephan            if(!pVfs){
1153d645484Sstephan                log2('error',"Unknown VFS:",vfs);
116b5ae85ecSstephan                return;
117b5ae85ecSstephan            }
1183d645484Sstephan            argv.push("--vfs", vfs);
1193d645484Sstephan            log2('',"Using VFS:",vfs);
1203d645484Sstephan            if('kvvfs' === vfs){
121674b312eSstephan                forceSize = 4 /* 5 uses approx. 4.96mb */;
122b5ae85ecSstephan                dbFile = 'session';
1233d645484Sstephan                log2('warning',"kvvfs VFS: forcing --size",forceSize,
124fa5aac74Sstephan                     "and filename '"+dbFile+"'.");
1258a8244b5Sstephan                capi.sqlite3_js_kvvfs_clear(dbFile);
126b5ae85ecSstephan            }
127b5ae85ecSstephan        }
1283d645484Sstephan        if(forceSize){
1293d645484Sstephan            argv.push('--size',forceSize);
1303d645484Sstephan        }else{
131b5ae85ecSstephan            [
132b5ae85ecSstephan                'size'
133b5ae85ecSstephan            ].forEach(function(k){
1343d645484Sstephan                const v = urlParams.get(k);
135b5ae85ecSstephan                if(v) argv.push('--'+k, urlParams[k]);
136b5ae85ecSstephan            });
1373d645484Sstephan        }
138e66b2681Sstephan        argv.push(
1398fc8b5b3Sstephan            "--singlethread",
140fa5aac74Sstephan            //"--nomutex",
141fa5aac74Sstephan            //"--nosync",
1423d645484Sstephan            //"--memdb", // note that memdb trumps the filename arg
143e66b2681Sstephan            "--nomemstat"
144e66b2681Sstephan        );
145e66b2681Sstephan        argv.push("--big-transactions"/*important for tests 410 and 510!*/,
146e66b2681Sstephan                  dbFile);
1478fc8b5b3Sstephan        console.log("argv =",argv);
148dd628ed5Sstephan        // These log messages are not emitted to the UI until after main() returns. Fixing that
149dd628ed5Sstephan        // requires moving the main() call and related cleanup into a timeout handler.
1508a8244b5Sstephan        if(pDir) wasm.sqlite3_wasm_vfs_unlink(pVfs,dbFile);
151e66b2681Sstephan        log2('',"Starting native app:\n ",argv.join(' '));
152e66b2681Sstephan        log2('',"This will take a while and the browser might warn about the runaway JS.",
153e66b2681Sstephan             "Give it time...");
154e66b2681Sstephan        logList.length = 0;
155dd628ed5Sstephan        setTimeout(function(){
156b5ae85ecSstephan            wasm.xCall('wasm_main', argv.length,
1578fc8b5b3Sstephan                       wasm.scopedAllocMainArgv(argv));
158100b496dSstephan            wasm.scopedAllocPop(scope);
159674b312eSstephan            if('kvvfs'===vfs){
160674b312eSstephan                logList.unshift("KVVFS "+dbFile+" size = "+
161674b312eSstephan                                capi.sqlite3_js_kvvfs_size(dbFile));
162674b312eSstephan            }
1638a8244b5Sstephan            if(pDir) wasm.sqlite3_wasm_vfs_unlink(pVfs,dbFile);
164e66b2681Sstephan            logList.unshift("Done running native main(). Output:");
165e66b2681Sstephan            dumpLogList();
166e66b2681Sstephan        }, 50);
167dd628ed5Sstephan    }/*runTests()*/;
168100b496dSstephan
1698fc8b5b3Sstephan    self.sqlite3TestModule.print = log;
170100b496dSstephan    self.sqlite3TestModule.printErr = logErr;
171b94a9860Sstephan    sqlite3InitModule(self.sqlite3TestModule).then(runTests);
1723d645484Sstephan})();</script>
173100b496dSstephan</body>
174100b496dSstephan</html>
175