144a87f08Sstephan<!doctype html>
244a87f08Sstephan<html lang="en-us">
344a87f08Sstephan  <head>
444a87f08Sstephan    <meta charset="utf-8">
544a87f08Sstephan    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
644a87f08Sstephan    <link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
744a87f08Sstephan    <link rel="stylesheet" href="common/emscripten.css"/>
844a87f08Sstephan    <link rel="stylesheet" href="common/testing.css"/>
944a87f08Sstephan    <title>speedtest1.wasm Worker</title>
1044a87f08Sstephan  </head>
1144a87f08Sstephan  <body>
1244a87f08Sstephan    <header id='titlebar'>speedtest1.wasm Worker</header>
1344a87f08Sstephan    <div>See also: <a href='speedtest1.html'>A main-thread variant of this page.</a></div>
1444a87f08Sstephan    <!-- emscripten bits -->
1544a87f08Sstephan    <figure id="module-spinner">
1644a87f08Sstephan      <div class="spinner"></div>
1744a87f08Sstephan      <div class='center'><strong>Initializing app...</strong></div>
1844a87f08Sstephan      <div class='center'>
1944a87f08Sstephan        On a slow internet connection this may take a moment.  If this
2044a87f08Sstephan        message displays for "a long time", intialization may have
2144a87f08Sstephan        failed and the JavaScript console may contain clues as to why.
2244a87f08Sstephan      </div>
2344a87f08Sstephan    </figure>
2444a87f08Sstephan    <div class="emscripten" id="module-status">Downloading...</div>
2544a87f08Sstephan    <div class="emscripten">
2644a87f08Sstephan      <progress value="0" max="100" id="module-progress" hidden='1'></progress>
2744a87f08Sstephan    </div><!-- /emscripten bits -->
2844a87f08Sstephan    <fieldset id='ui-controls' class='hidden'>
2944a87f08Sstephan      <legend>Options</legend>
3044a87f08Sstephan      <div id='toolbar'>
3144a87f08Sstephan        <div id='toolbar-select'>
3244a87f08Sstephan          <select id='select-flags' size='10' multiple></select>
33b5ae85ecSstephan          <div>The following flags can be passed as URL parameters:
348948fbeeSstephan            vfs=NAME, size=N, journal=MODE, cachesize=SIZE
35b5ae85ecSstephan          </div>
3644a87f08Sstephan        </div>
37e66b2681Sstephan        <div class='toolbar-inner-vertical'>
38e66b2681Sstephan          <div id='toolbar-selected-flags'></div>
3928ef9bddSstephan          <div class='toolbar-inner-vertical'>
4028ef9bddSstephan            <span>&rarr; <a id='link-main-thread' href='#' target='speedtest-main'
41f6b6188bSstephan                            title='Start speedtest1.html with the selected flags'>speedtest1</a>
42e66b2681Sstephan            </span>
4328ef9bddSstephan            <span>&rarr; <a id='link-wasmfs' href='#' target='speedtest-wasmfs'
44f6b6188bSstephan                            title='Start speedtest1-wasmfs.html with the selected flags'>speedtest1-wasmfs</a>
4528ef9bddSstephan            </span>
460144a848Sstephan            <span>&rarr; <a id='link-kvvfs' href='#' target='speedtest-kvvfs'
47f6b6188bSstephan                            title='Start kvvfs speedtest1 with the selected flags'>speedtest1-kvvfs</a>
480144a848Sstephan            </span>
4928ef9bddSstephan          </div>
5044a87f08Sstephan        </div>
5144a87f08Sstephan        <div class='toolbar-inner-vertical' id='toolbar-runner-controls'>
5244a87f08Sstephan          <button id='btn-reset-flags'>Reset Flags</button>
5344a87f08Sstephan          <button id='btn-output-clear'>Clear output</button>
5444a87f08Sstephan          <button id='btn-run'>Run</button>
5544a87f08Sstephan        </div>
5644a87f08Sstephan      </div>
5744a87f08Sstephan    </fieldset>
5844a87f08Sstephan    <div>
5944a87f08Sstephan      <span class='input-wrapper'>
6044a87f08Sstephan        <input type='checkbox' class='disable-during-eval' id='cb-reverse-log-order' checked></input>
6144a87f08Sstephan        <label for='cb-reverse-log-order' id='lbl-reverse-log-order'>Reverse log order</label>
6244a87f08Sstephan      </span>
6344a87f08Sstephan    </div>
641174c23eSstephan    <div id='test-output'>
651174c23eSstephan    </div>
661174c23eSstephan    <div id='tips'>
671174c23eSstephan      <strong>Tips:</strong>
681174c23eSstephan      <ul>
691174c23eSstephan        <li>Control-click the flags to (de)select multiple flags.</li>
70f5bf66c8Sstephan        <li>The <tt>--big-transactions</tt> flag is important for two
71f5bf66c8Sstephan          of the bigger tests. Without it, those tests create a
72f5bf66c8Sstephan          combined total of 140k implicit transactions, reducing their
73f5bf66c8Sstephan          speed to an absolute crawl, especially when WASMFS is
74f5bf66c8Sstephan          activated.
75f5bf66c8Sstephan        </li>
761174c23eSstephan        <li>The easiest way to try different optimization levels is,
771174c23eSstephan          from this directory:
781174c23eSstephan          <pre>$ rm -f speedtest1.js; make -e emcc_opt='-O2' speedtest1.js</pre>
791174c23eSstephan          Then reload this page. -O2 seems to consistently produce the fastest results.
801174c23eSstephan        </li>
811174c23eSstephan        </ul>
821174c23eSstephan    </div>
8344a87f08Sstephan    <style>
8444a87f08Sstephan      #test-output {
8544a87f08Sstephan          white-space: break-spaces;
8644a87f08Sstephan          overflow: auto;
8744a87f08Sstephan      }
881174c23eSstephan      div#tips { margin-top: 1em; }
8944a87f08Sstephan      #toolbar {
9044a87f08Sstephan          display: flex;
9144a87f08Sstephan          flex-direction: row;
9244a87f08Sstephan          flex-wrap: wrap;
9344a87f08Sstephan      }
9444a87f08Sstephan      #toolbar > * {
9544a87f08Sstephan          margin: 0 0.5em;
9644a87f08Sstephan      }
9744a87f08Sstephan      .toolbar-inner-vertical {
9844a87f08Sstephan          display: flex;
9944a87f08Sstephan          flex-direction: column;
10044a87f08Sstephan          justify-content: space-between;
10144a87f08Sstephan      }
10244a87f08Sstephan      #toolbar-select {
10344a87f08Sstephan          display: flex;
10444a87f08Sstephan          flex-direction: column;
10544a87f08Sstephan      }
10644a87f08Sstephan      .toolbar-inner-vertical > *, #toolbar-select > * {
10744a87f08Sstephan          margin: 0.2em 0;
10844a87f08Sstephan      }
10944a87f08Sstephan      #select-flags > option {
11044a87f08Sstephan          white-space: pre;
11144a87f08Sstephan          font-family: monospace;
11244a87f08Sstephan      }
11344a87f08Sstephan      fieldset {
11444a87f08Sstephan          border-radius: 0.5em;
11544a87f08Sstephan      }
11644a87f08Sstephan      #toolbar-runner-controls { flex-grow: 1 }
11744a87f08Sstephan      #toolbar-runner-controls > * { flex: 1 0 auto }
11844a87f08Sstephan      #toolbar-selected-flags::before {
11944a87f08Sstephan        font-family: initial;
12044a87f08Sstephan        content:"Selected flags: ";
12144a87f08Sstephan      }
12244a87f08Sstephan      #toolbar-selected-flags {
123e66b2681Sstephan        display: flex;
124e66b2681Sstephan        flex-direction: column;
12544a87f08Sstephan        font-family: monospace;
12644a87f08Sstephan        justify-content: flex-start;
12744a87f08Sstephan      }
12844a87f08Sstephan    </style>
12944a87f08Sstephan    <script>(function(){
13044a87f08Sstephan    'use strict';
13144a87f08Sstephan    const E = (sel)=>document.querySelector(sel);
13244a87f08Sstephan    const eOut = E('#test-output');
13344a87f08Sstephan    const log2 = function(cssClass,...args){
13444a87f08Sstephan        let ln;
13544a87f08Sstephan        if(1 || cssClass){
13644a87f08Sstephan            ln = document.createElement('div');
13744a87f08Sstephan            if(cssClass) ln.classList.add(cssClass);
13844a87f08Sstephan            ln.append(document.createTextNode(args.join(' ')));
13944a87f08Sstephan        }else{
14044a87f08Sstephan            // This doesn't work with the "reverse order" option!
14144a87f08Sstephan            ln = document.createTextNode(args.join(' ')+'\n');
14244a87f08Sstephan        }
14344a87f08Sstephan        eOut.append(ln);
14444a87f08Sstephan    };
14544a87f08Sstephan    const log = (...args)=>{
14644a87f08Sstephan        //console.log(...args);
14744a87f08Sstephan        log2('', ...args);
14844a87f08Sstephan    };
14944a87f08Sstephan    const logErr = function(...args){
15061418d5aSstephan        console.error(...args);
15144a87f08Sstephan        log2('error', ...args);
15244a87f08Sstephan    };
15344a87f08Sstephan    const logWarn = function(...args){
15461418d5aSstephan        console.warn(...args);
15544a87f08Sstephan        log2('warning', ...args);
15644a87f08Sstephan    };
15744a87f08Sstephan
158e66b2681Sstephan    const spacePad = function(str,len=21){
15944a87f08Sstephan        if(str.length===len) return str;
16044a87f08Sstephan        else if(str.length>len) return str.substr(0,len);
16144a87f08Sstephan        const a = []; a.length = len - str.length;
16244a87f08Sstephan        return str+a.join(' ');
16344a87f08Sstephan    };
16444a87f08Sstephan    // OPTION elements seem to ignore white-space:pre, so do this the hard way...
165e66b2681Sstephan    const nbspPad = function(str,len=21){
16644a87f08Sstephan        if(str.length===len) return str;
16744a87f08Sstephan        else if(str.length>len) return str.substr(0,len);
16844a87f08Sstephan        const a = []; a.length = len - str.length;
16944a87f08Sstephan        return str+a.join('&nbsp;');
17044a87f08Sstephan    };
17144a87f08Sstephan
172*da264159Sstephan    const urlParams = new URL(self.location.href).searchParams;
173*da264159Sstephan    const W = new Worker(
174*da264159Sstephan        "speedtest1-worker.js?sqlite3.dir=jswasm"+
175*da264159Sstephan            (urlParams.has('opfs-verbose') ? '&opfs-verbose' : '')
176*da264159Sstephan    );
17744a87f08Sstephan    const mPost = function(msgType,payload){
17844a87f08Sstephan        W.postMessage({type: msgType, data: payload});
17944a87f08Sstephan    };
18044a87f08Sstephan
18144a87f08Sstephan    const eFlags = E('#select-flags');
18244a87f08Sstephan    const eSelectedFlags = E('#toolbar-selected-flags');
183e66b2681Sstephan    const eLinkMainThread = E('#link-main-thread');
18428ef9bddSstephan    const eLinkWasmfs = E('#link-wasmfs');
1850144a848Sstephan    const eLinkKvvfs = E('#link-kvvfs');
186b5ae85ecSstephan    const getSelectedFlags = ()=>{
187b5ae85ecSstephan        const f = Array.prototype.map.call(eFlags.selectedOptions, (v)=>v.value);
188b5ae85ecSstephan        [
189d234902bSstephan            'size', 'vfs', 'journal', 'cachesize'
190b5ae85ecSstephan        ].forEach(function(k){
191b5ae85ecSstephan            if(urlParams.has(k)) f.push('--'+k, urlParams.get(k));
192b5ae85ecSstephan        });
193b5ae85ecSstephan        return f;
194b5ae85ecSstephan    };
19544a87f08Sstephan    const updateSelectedFlags = function(){
19644a87f08Sstephan        eSelectedFlags.innerText = '';
197e66b2681Sstephan        const flags = getSelectedFlags();
198e66b2681Sstephan        flags.forEach(function(f){
19944a87f08Sstephan            const e = document.createElement('span');
20044a87f08Sstephan            e.innerText = f;
20144a87f08Sstephan            eSelectedFlags.appendChild(e);
20244a87f08Sstephan        });
203e66b2681Sstephan        const rxStripDash = /^(-+)?/;
2040e0687ccSstephan        const comma = flags.join(',');
20528ef9bddSstephan        eLinkMainThread.setAttribute('target', 'speedtest1-main-'+comma);
206e66b2681Sstephan        eLinkMainThread.href = 'speedtest1.html?flags='+comma;
20728ef9bddSstephan        eLinkWasmfs.setAttribute('target', 'speedtest1-wasmfs-'+comma);
20828ef9bddSstephan        eLinkWasmfs.href = 'speedtest1-wasmfs.html?flags='+comma;
2090144a848Sstephan        eLinkKvvfs.setAttribute('target', 'speedtest1-kvvfs-'+comma);
210f6b6188bSstephan        eLinkKvvfs.href = 'speedtest1.html?vfs=kvvfs&flags='+comma;
21144a87f08Sstephan    };
21244a87f08Sstephan    eFlags.addEventListener('change', updateSelectedFlags );
21344a87f08Sstephan    {
21444a87f08Sstephan        const flags = Object.create(null);
21544a87f08Sstephan        /* TODO? Flags which require values need custom UI
21644a87f08Sstephan           controls and some of them make little sense here
21744a87f08Sstephan           (e.g. --script FILE). */
2185e8bb0aaSstephan        flags["--autovacuum"] = "Enable AUTOVACUUM mode";
2195e8bb0aaSstephan        flags["--big-transactions"] = "Important for tests 410 and 510!";
220d234902bSstephan        //flags["--cachesize"] = "N       Set the cache size to N pages";
2215e8bb0aaSstephan        flags["--checkpoint"] = "Run PRAGMA wal_checkpoint after each test case";
2225e8bb0aaSstephan        flags["--exclusive"] = "Enable locking_mode=EXCLUSIVE";
2235e8bb0aaSstephan        flags["--explain"] = "Like --sqlonly but with added EXPLAIN keywords";
2245e8bb0aaSstephan        //flags["--heap"] = "SZ MIN       Memory allocator uses SZ bytes & min allocation MIN";
2255e8bb0aaSstephan        flags["--incrvacuum"] = "Enable incremenatal vacuum mode";
2265e8bb0aaSstephan        //flags["--journal"] = "M         Set the journal_mode to M";
2275e8bb0aaSstephan        //flags["--key"] = "KEY           Set the encryption key to KEY";
2285e8bb0aaSstephan        //flags["--lookaside"] = "N SZ    Configure lookaside for N slots of SZ bytes each";
2295e8bb0aaSstephan        flags["--memdb"] = "Use an in-memory database";
2305e8bb0aaSstephan        //flags["--mmap"] = "SZ           MMAP the first SZ bytes of the database file";
2315e8bb0aaSstephan        flags["--multithread"] = "Set multithreaded mode";
2325e8bb0aaSstephan        flags["--nomemstat"] = "Disable memory statistics";
2335e8bb0aaSstephan        flags["--nomutex"] = "Open db with SQLITE_OPEN_NOMUTEX";
2345e8bb0aaSstephan        flags["--nosync"] = "Set PRAGMA synchronous=OFF";
2355e8bb0aaSstephan        flags["--notnull"] = "Add NOT NULL constraints to table columns";
2365e8bb0aaSstephan        //flags["--output"] = "FILE       Store SQL output in FILE";
2375e8bb0aaSstephan        //flags["--pagesize"] = "N        Set the page size to N";
2385e8bb0aaSstephan        //flags["--pcache"] = "N SZ       Configure N pages of pagecache each of size SZ bytes";
2395e8bb0aaSstephan        //flags["--primarykey"] = "Use PRIMARY KEY instead of UNIQUE where appropriate";
2405e8bb0aaSstephan        //flags["--repeat"] = "N          Repeat each SELECT N times (default: 1)";
2415e8bb0aaSstephan        flags["--reprepare"] = "Reprepare each statement upon every invocation";
2425e8bb0aaSstephan        //flags["--reserve"] = "N         Reserve N bytes on each database page";
2435e8bb0aaSstephan        //flags["--script"] = "FILE       Write an SQL script for the test into FILE";
2445e8bb0aaSstephan        flags["--serialized"] = "Set serialized threading mode";
2455e8bb0aaSstephan        flags["--singlethread"] = "Set single-threaded mode - disables all mutexing";
2465e8bb0aaSstephan        flags["--sqlonly"] = "No-op.  Only show the SQL that would have been run.";
247e8afca3fSstephan        flags["--shrink-memory"] = "Invoke sqlite3_db_release_memory() frequently.";
2485e8bb0aaSstephan        //flags["--size"] = "N            Relative test size.  Default=100";
2495e8bb0aaSstephan        flags["--strict"] = "Use STRICT table where appropriate";
2505e8bb0aaSstephan        flags["--stats"] = "Show statistics at the end";
2515e8bb0aaSstephan        //flags["--temp"] = "N            N from 0 to 9.  0: no temp table. 9: all temp tables";
2525e8bb0aaSstephan        //flags["--testset"] = "T         Run test-set T (main, cte, rtree, orm, fp, debug)";
2535e8bb0aaSstephan        flags["--trace"] = "Turn on SQL tracing";
2545e8bb0aaSstephan        //flags["--threads"] = "N         Use up to N threads for sorting";
25544a87f08Sstephan        /*
25644a87f08Sstephan          The core API's WASM build does not support UTF16, but in
25744a87f08Sstephan          this app it's not an issue because the data are not crossing
25844a87f08Sstephan          JS/WASM boundaries.
25944a87f08Sstephan        */
2605e8bb0aaSstephan        flags["--utf16be"] = "Set text encoding to UTF-16BE";
2615e8bb0aaSstephan        flags["--utf16le"] = "Set text encoding to UTF-16LE";
2625e8bb0aaSstephan        flags["--verify"] = "Run additional verification steps.";
2635e8bb0aaSstephan        flags["--without"] = "rowid     Use WITHOUT ROWID where appropriate";
26444a87f08Sstephan        const preselectedFlags = [
2655e8bb0aaSstephan            '--big-transactions',
2665e8bb0aaSstephan            '--singlethread'
26744a87f08Sstephan        ];
2685e8bb0aaSstephan        if(urlParams.has('flags')){
2695e8bb0aaSstephan            preselectedFlags.push(...urlParams.get('flags').split(','));
270f815011aSstephan        }
2715e8bb0aaSstephan        if('opfs'!==urlParams.get('vfs')){
2725e8bb0aaSstephan            preselectedFlags.push('--memdb');
2735e8bb0aaSstephan        }
27444a87f08Sstephan        Object.keys(flags).sort().forEach(function(f){
27544a87f08Sstephan            const opt = document.createElement('option');
27644a87f08Sstephan            eFlags.appendChild(opt);
2775e8bb0aaSstephan            const lbl = nbspPad(f)+flags[f];
27844a87f08Sstephan            //opt.innerText = lbl;
27944a87f08Sstephan            opt.innerHTML = lbl;
2805e8bb0aaSstephan            opt.value = f;
28144a87f08Sstephan            if(preselectedFlags.indexOf(f) >= 0) opt.selected = true;
28244a87f08Sstephan        });
28344a87f08Sstephan        const cbReverseLog = E('#cb-reverse-log-order');
28444a87f08Sstephan        const lblReverseLog = E('#lbl-reverse-log-order');
28544a87f08Sstephan        if(cbReverseLog.checked){
28644a87f08Sstephan            lblReverseLog.classList.add('warning');
28744a87f08Sstephan            eOut.classList.add('reverse');
28844a87f08Sstephan        }
28944a87f08Sstephan        cbReverseLog.addEventListener('change', function(){
29044a87f08Sstephan            if(this.checked){
29144a87f08Sstephan                eOut.classList.add('reverse');
29244a87f08Sstephan                lblReverseLog.classList.add('warning');
29344a87f08Sstephan            }else{
29444a87f08Sstephan                eOut.classList.remove('reverse');
29544a87f08Sstephan                lblReverseLog.classList.remove('warning');
29644a87f08Sstephan            }
29744a87f08Sstephan        }, false);
29844a87f08Sstephan        updateSelectedFlags();
29944a87f08Sstephan    }
30044a87f08Sstephan    E('#btn-output-clear').addEventListener('click', ()=>{
30144a87f08Sstephan        eOut.innerText = '';
30244a87f08Sstephan    });
30344a87f08Sstephan    E('#btn-reset-flags').addEventListener('click',()=>{
30444a87f08Sstephan        eFlags.value = '';
30544a87f08Sstephan        updateSelectedFlags();
30644a87f08Sstephan    });
30744a87f08Sstephan    E('#btn-run').addEventListener('click',function(){
30844a87f08Sstephan        log("Running speedtest1. UI controls will be disabled until it completes.");
30944a87f08Sstephan        mPost('run', getSelectedFlags());
31044a87f08Sstephan    });
31144a87f08Sstephan
31244a87f08Sstephan    const eControls = E('#ui-controls');
31344a87f08Sstephan    /** Update Emscripten-related UI elements while loading the module. */
31444a87f08Sstephan    const updateLoadStatus = function f(text){
31544a87f08Sstephan        if(!f.last){
31644a87f08Sstephan            f.last = { text: '', step: 0 };
31744a87f08Sstephan            const E = (cssSelector)=>document.querySelector(cssSelector);
31844a87f08Sstephan            f.ui = {
31944a87f08Sstephan                status: E('#module-status'),
32044a87f08Sstephan                progress: E('#module-progress'),
32144a87f08Sstephan                spinner: E('#module-spinner')
32244a87f08Sstephan            };
32344a87f08Sstephan        }
32444a87f08Sstephan        if(text === f.last.text) return;
32544a87f08Sstephan        f.last.text = text;
32644a87f08Sstephan        if(f.ui.progress){
32744a87f08Sstephan            f.ui.progress.value = f.last.step;
32844a87f08Sstephan            f.ui.progress.max = f.last.step + 1;
32944a87f08Sstephan        }
33044a87f08Sstephan        ++f.last.step;
33144a87f08Sstephan        if(text) {
33244a87f08Sstephan            f.ui.status.classList.remove('hidden');
33344a87f08Sstephan            f.ui.status.innerText = text;
33444a87f08Sstephan        }else{
33544a87f08Sstephan            if(f.ui.progress){
33644a87f08Sstephan                f.ui.progress.remove();
33744a87f08Sstephan                f.ui.spinner.remove();
33844a87f08Sstephan                delete f.ui.progress;
33944a87f08Sstephan                delete f.ui.spinner;
34044a87f08Sstephan            }
34144a87f08Sstephan            f.ui.status.classList.add('hidden');
34244a87f08Sstephan        }
34344a87f08Sstephan    };
34444a87f08Sstephan
34544a87f08Sstephan    W.onmessage = function(msg){
34644a87f08Sstephan        msg = msg.data;
34744a87f08Sstephan        switch(msg.type){
3481174c23eSstephan            case 'ready':
3491174c23eSstephan                log("Worker is ready.");
3501174c23eSstephan                eControls.classList.remove('hidden');
3511174c23eSstephan                break;
35244a87f08Sstephan            case 'stdout': log(msg.data); break;
35344a87f08Sstephan            case 'stdout': logErr(msg.data); break;
35444a87f08Sstephan            case 'run-start':
35544a87f08Sstephan                eControls.disabled = true;
35644a87f08Sstephan                log("Running speedtest1 with argv =",msg.data.join(' '));
35744a87f08Sstephan                break;
3581174c23eSstephan            case 'run-end':
3591174c23eSstephan                log("speedtest1 finished.");
36044a87f08Sstephan                eControls.disabled = false;
36144a87f08Sstephan                // app output is in msg.data
36244a87f08Sstephan                break;
36344a87f08Sstephan            case 'error': logErr(msg.data); break;
36444a87f08Sstephan            case 'load-status': updateLoadStatus(msg.data); break;
36544a87f08Sstephan            default:
366dd628ed5Sstephan                logErr("Unhandled worker message type:",msg);
36744a87f08Sstephan                break;
36844a87f08Sstephan        }
36944a87f08Sstephan    };
37044a87f08Sstephan})();</script>
37144a87f08Sstephan  </body>
37244a87f08Sstephan</html>
373