128ef9bddSstephan<!doctype html>
228ef9bddSstephan<html lang="en-us">
328ef9bddSstephan  <head>
428ef9bddSstephan    <meta charset="utf-8">
528ef9bddSstephan    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
628ef9bddSstephan    <link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
728ef9bddSstephan    <link rel="stylesheet" href="common/emscripten.css"/>
828ef9bddSstephan    <link rel="stylesheet" href="common/testing.css"/>
928ef9bddSstephan    <title>speedtest1-wasmfs.wasm</title>
1028ef9bddSstephan  </head>
1128ef9bddSstephan  <body>
1228ef9bddSstephan    <header id='titlebar'><span>speedtest1-wasmfs.wasm</span></header>
1328ef9bddSstephan    <div>See also: <a href='speedtest1-worker.html'>A Worker-thread variant of this page.</a></div>
1428ef9bddSstephan    <!-- emscripten bits -->
1528ef9bddSstephan    <figure id="module-spinner">
1628ef9bddSstephan      <div class="spinner"></div>
1728ef9bddSstephan      <div class='center'><strong>Initializing app...</strong></div>
1828ef9bddSstephan      <div class='center'>
1928ef9bddSstephan        On a slow internet connection this may take a moment.  If this
2028ef9bddSstephan        message displays for "a long time", intialization may have
2128ef9bddSstephan        failed and the JavaScript console may contain clues as to why.
2228ef9bddSstephan      </div>
2328ef9bddSstephan    </figure>
2428ef9bddSstephan    <div class="emscripten" id="module-status">Downloading...</div>
2528ef9bddSstephan    <div class="emscripten">
2628ef9bddSstephan      <progress value="0" max="100" id="module-progress" hidden='1'></progress>
2728ef9bddSstephan    </div><!-- /emscripten bits -->
2828ef9bddSstephan    <div class='warning'>This page starts running the main exe when it loads, which will
2928ef9bddSstephan      block the UI until it finishes! Adding UI controls to manually configure and start it
3028ef9bddSstephan      are TODO.</div>
3128ef9bddSstephan    </div>
3228ef9bddSstephan    <div class='warning'>Achtung: running it with the dev tools open may
3328ef9bddSstephan      <em>drastically</em> slow it down. For faster results, keep the dev
3428ef9bddSstephan      tools closed when running it!
3528ef9bddSstephan    </div>
3628ef9bddSstephan    <div>Output is delayed/buffered because we cannot update the UI while the
3728ef9bddSstephan      speedtest is running. Output will appear below when ready...
3828ef9bddSstephan    <div id='test-output'></div>
3928ef9bddSstephan    <script src="common/SqliteTestUtil.js"></script>
4028ef9bddSstephan    <script src="speedtest1-wasmfs.js"></script>
4128ef9bddSstephan    <script>(function(){
4228ef9bddSstephan    /**
4328ef9bddSstephan       If this environment contains OPFS, this function initializes it and
4428ef9bddSstephan       returns the name of the dir on which OPFS is mounted, else it returns
4528ef9bddSstephan       an empty string.
4628ef9bddSstephan    */
473d645484Sstephan    const wasmfsDir = function f(wasmUtil,dirName="/opfs"){
4828ef9bddSstephan        if(undefined !== f._) return f._;
4928ef9bddSstephan        if( !self.FileSystemHandle
5028ef9bddSstephan            || !self.FileSystemDirectoryHandle
5128ef9bddSstephan            || !self.FileSystemFileHandle){
5228ef9bddSstephan            return f._ = "";
5328ef9bddSstephan        }
5428ef9bddSstephan        try{
5528ef9bddSstephan            if(0===wasmUtil.xCallWrapped(
563d645484Sstephan                'sqlite3_wasm_init_wasmfs', 'i32', ['string'], dirName
5728ef9bddSstephan            )){
583d645484Sstephan                return f._ = dirName;
5928ef9bddSstephan            }else{
6028ef9bddSstephan                return f._ = "";
6128ef9bddSstephan            }
6228ef9bddSstephan        }catch(e){
6328ef9bddSstephan            // sqlite3_wasm_init_wasmfs() is not available
6428ef9bddSstephan            return f._ = "";
6528ef9bddSstephan        }
6628ef9bddSstephan    };
6761418d5aSstephan    wasmfsDir._ = undefined;
6828ef9bddSstephan
6928ef9bddSstephan    const eOut = document.querySelector('#test-output');
7028ef9bddSstephan    const log2 = function(cssClass,...args){
7128ef9bddSstephan        const ln = document.createElement('div');
7228ef9bddSstephan        if(cssClass) ln.classList.add(cssClass);
7328ef9bddSstephan        ln.append(document.createTextNode(args.join(' ')));
7428ef9bddSstephan        eOut.append(ln);
7528ef9bddSstephan        //this.e.output.lastElementChild.scrollIntoViewIfNeeded();
7628ef9bddSstephan    };
7728ef9bddSstephan    const logList = [];
7828ef9bddSstephan    const dumpLogList = function(){
7928ef9bddSstephan        logList.forEach((v)=>log2('',v));
8028ef9bddSstephan        logList.length = 0;
8128ef9bddSstephan    };
8228ef9bddSstephan    /* can't update DOM while speedtest is running unless we run
8328ef9bddSstephan       speedtest in a worker thread. */;
8428ef9bddSstephan    const log = (...args)=>{
8528ef9bddSstephan        console.log(...args);
8628ef9bddSstephan        logList.push(args.join(' '));
8728ef9bddSstephan    };
8828ef9bddSstephan    const logErr = function(...args){
8928ef9bddSstephan        console.error(...args);
9028ef9bddSstephan        logList.push('ERROR: '+args.join(' '));
9128ef9bddSstephan    };
9228ef9bddSstephan
9361418d5aSstephan    const runTests = function(sqlite3){
9461418d5aSstephan        console.log("Module inited.");
9561418d5aSstephan        const wasm = sqlite3.capi.wasm;
9628ef9bddSstephan        const unlink = wasm.xWrap("sqlite3_wasm_vfs_unlink", "int", ["string"]);
9761418d5aSstephan        const pDir = wasmfsDir(wasm);
9828ef9bddSstephan        if(pDir) log2('',"Persistent storage:",pDir);
9928ef9bddSstephan        else{
10028ef9bddSstephan            log2('error',"Expecting persistent storage in this build.");
10128ef9bddSstephan            return;
10228ef9bddSstephan        }
10328ef9bddSstephan        const scope = wasm.scopedAllocPush();
10461418d5aSstephan        const dbFile = pDir+"/speedtest1.db";
1053d645484Sstephan        const urlParams = new URL(self.location.href).searchParams;
10628ef9bddSstephan        const argv = ["speedtest1"];
1073d645484Sstephan        if(urlParams.has('flags')){
1083d645484Sstephan            argv.push(...(urlParams.get('flags').split(',')));
10961418d5aSstephan            let i = argv.indexOf('--vfs');
11061418d5aSstephan            if(i>=0) argv.splice(i,2);
11128ef9bddSstephan        }else{
11228ef9bddSstephan            argv.push(
11328ef9bddSstephan                "--singlethread",
11428ef9bddSstephan                "--nomutex",
11528ef9bddSstephan                "--nosync",
11628ef9bddSstephan                "--nomemstat"
11728ef9bddSstephan            );
11828ef9bddSstephan            //"--memdb", // note that memdb trumps the filename arg
11928ef9bddSstephan        }
12061418d5aSstephan
12128ef9bddSstephan        if(argv.indexOf('--memdb')>=0){
12228ef9bddSstephan            log2('error',"WARNING: --memdb flag trumps db filename.");
12328ef9bddSstephan        }
12428ef9bddSstephan        argv.push("--big-transactions"/*important for tests 410 and 510!*/,
12528ef9bddSstephan                  dbFile);
12628ef9bddSstephan        console.log("argv =",argv);
12728ef9bddSstephan        // These log messages are not emitted to the UI until after main() returns. Fixing that
12828ef9bddSstephan        // requires moving the main() call and related cleanup into a timeout handler.
12928ef9bddSstephan        if(pDir) unlink(dbFile);
13028ef9bddSstephan        log2('',"Starting native app:\n ",argv.join(' '));
13128ef9bddSstephan        log2('',"This will take a while and the browser might warn about the runaway JS.",
13228ef9bddSstephan             "Give it time...");
13328ef9bddSstephan        logList.length = 0;
13428ef9bddSstephan        setTimeout(function(){
135b5ae85ecSstephan            wasm.xCall('wasm_main', argv.length,
13628ef9bddSstephan                       wasm.scopedAllocMainArgv(argv));
13728ef9bddSstephan            wasm.scopedAllocPop(scope);
13828ef9bddSstephan            if(pDir) unlink(dbFile);
13928ef9bddSstephan            logList.unshift("Done running native main(). Output:");
14028ef9bddSstephan            dumpLogList();
14161418d5aSstephan        }, 25);
14228ef9bddSstephan    }/*runTests()*/;
14328ef9bddSstephan
14428ef9bddSstephan    self.sqlite3TestModule.print = log;
14528ef9bddSstephan    self.sqlite3TestModule.printErr = logErr;
146*b94a9860Sstephan    sqlite3InitModule(self.sqlite3TestModule).then(runTests);
1473d645484Sstephan})();</script>
14828ef9bddSstephan  </body>
14928ef9bddSstephan</html>
150