1<!doctype html>
2<html lang="en-us">
3  <head>
4    <meta charset="utf-8">
5    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6    <link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
7    <link rel="stylesheet" href="common/emscripten.css"/>
8    <link rel="stylesheet" href="common/testing.css"/>
9    <title>speedtest1.wasm Worker</title>
10  </head>
11  <body>
12    <header id='titlebar'>speedtest1.wasm Worker</header>
13    <div>See also: <a href='speedtest1.html'>A main-thread variant of this page.</a></div>
14    <!-- emscripten bits -->
15    <figure id="module-spinner">
16      <div class="spinner"></div>
17      <div class='center'><strong>Initializing app...</strong></div>
18      <div class='center'>
19        On a slow internet connection this may take a moment.  If this
20        message displays for "a long time", intialization may have
21        failed and the JavaScript console may contain clues as to why.
22      </div>
23    </figure>
24    <div class="emscripten" id="module-status">Downloading...</div>
25    <div class="emscripten">
26      <progress value="0" max="100" id="module-progress" hidden='1'></progress>
27    </div><!-- /emscripten bits -->
28    <fieldset id='ui-controls' class='hidden'>
29      <legend>Options</legend>
30      <div id='toolbar'>
31        <div id='toolbar-select'>
32          <select id='select-flags' size='10' multiple></select>
33          <div>TODO? Options which require values are not represented here.</div>
34        </div>
35        <div class='toolbar-inner-vertical'>
36          <div id='toolbar-selected-flags'></div>
37          <span>&rarr; <a id='link-main-thread' href='#' target='main-thread'
38                          title='Start speedtest1.html with the selected flags'>speedtest1.html</a>
39          </span>
40        </div>
41        <div class='toolbar-inner-vertical' id='toolbar-runner-controls'>
42          <button id='btn-reset-flags'>Reset Flags</button>
43          <button id='btn-output-clear'>Clear output</button>
44          <button id='btn-run'>Run</button>
45        </div>
46      </div>
47    </fieldset>
48    <div>
49      <span class='input-wrapper'>
50        <input type='checkbox' class='disable-during-eval' id='cb-reverse-log-order' checked></input>
51        <label for='cb-reverse-log-order' id='lbl-reverse-log-order'>Reverse log order</label>
52      </span>
53    </div>
54    <div id='test-output'>
55    </div>
56    <div id='tips'>
57      <strong>Tips:</strong>
58      <ul>
59        <li>Control-click the flags to (de)select multiple flags.</li>
60        <li>The <tt>--big-transactions</tt> flag is important for two
61          of the bigger tests. Without it, those tests create a
62          combined total of 140k implicit transactions, reducing their
63          speed to an absolute crawl, especially when WASMFS is
64          activated.
65        </li>
66        <li>The easiest way to try different optimization levels is,
67          from this directory:
68          <pre>$ rm -f speedtest1.js; make -e emcc_opt='-O2' speedtest1.js</pre>
69          Then reload this page. -O2 seems to consistently produce the fastest results.
70        </li>
71        </ul>
72    </div>
73    <style>
74      #test-output {
75          white-space: break-spaces;
76          overflow: auto;
77      }
78      div#tips { margin-top: 1em; }
79      #toolbar {
80          display: flex;
81          flex-direction: row;
82          flex-wrap: wrap;
83      }
84      #toolbar > * {
85          margin: 0 0.5em;
86      }
87      .toolbar-inner-vertical {
88          display: flex;
89          flex-direction: column;
90          justify-content: space-between;
91      }
92      #toolbar-select {
93          display: flex;
94          flex-direction: column;
95      }
96      .toolbar-inner-vertical > *, #toolbar-select > * {
97          margin: 0.2em 0;
98      }
99      #select-flags > option {
100          white-space: pre;
101          font-family: monospace;
102      }
103      fieldset {
104          border-radius: 0.5em;
105      }
106      #toolbar-runner-controls { flex-grow: 1 }
107      #toolbar-runner-controls > * { flex: 1 0 auto }
108      #toolbar-selected-flags::before {
109        font-family: initial;
110        content:"Selected flags: ";
111      }
112      #toolbar-selected-flags {
113        display: flex;
114        flex-direction: column;
115        font-family: monospace;
116        justify-content: flex-start;
117      }
118    </style>
119    <script>(function(){
120      'use strict';
121      const E = (sel)=>document.querySelector(sel);
122      const eOut = E('#test-output');
123      const log2 = function(cssClass,...args){
124        let ln;
125        if(1 || cssClass){
126          ln = document.createElement('div');
127          if(cssClass) ln.classList.add(cssClass);
128          ln.append(document.createTextNode(args.join(' ')));
129        }else{
130          // This doesn't work with the "reverse order" option!
131          ln = document.createTextNode(args.join(' ')+'\n');
132        }
133        eOut.append(ln);
134      };
135      const log = (...args)=>{
136        //console.log(...args);
137        log2('', ...args);
138      };
139      const logErr = function(...args){
140        //console.error(...args);
141        log2('error', ...args);
142      };
143      const logWarn = function(...args){
144        //console.warn(...args);
145        log2('warning', ...args);
146      };
147
148      const spacePad = function(str,len=21){
149        if(str.length===len) return str;
150        else if(str.length>len) return str.substr(0,len);
151        const a = []; a.length = len - str.length;
152        return str+a.join(' ');
153      };
154      // OPTION elements seem to ignore white-space:pre, so do this the hard way...
155      const nbspPad = function(str,len=21){
156        if(str.length===len) return str;
157        else if(str.length>len) return str.substr(0,len);
158        const a = []; a.length = len - str.length;
159        return str+a.join('&nbsp;');
160      };
161
162      const W = new Worker("speedtest1-worker.js");
163      const mPost = function(msgType,payload){
164        W.postMessage({type: msgType, data: payload});
165      };
166
167      const eFlags = E('#select-flags');
168      const eSelectedFlags = E('#toolbar-selected-flags');
169      const eLinkMainThread = E('#link-main-thread');
170      const getSelectedFlags = ()=>Array.prototype.map.call(eFlags.selectedOptions, (v)=>v.value);
171      const updateSelectedFlags = function(){
172        eSelectedFlags.innerText = '';
173        const flags = getSelectedFlags();
174        flags.forEach(function(f){
175          const e = document.createElement('span');
176          e.innerText = f;
177          eSelectedFlags.appendChild(e);
178        });
179        const rxStripDash = /^(-+)?/;
180        const comma = flags.map((v)=>v.replace(rxStripDash,'')).join(',');
181        eLinkMainThread.setAttribute('target', 'main-thread-'+comma);
182        eLinkMainThread.href = 'speedtest1.html?flags='+comma;
183      };
184      eFlags.addEventListener('change', updateSelectedFlags );
185      {
186        const flags = Object.create(null);
187        /* TODO? Flags which require values need custom UI
188           controls and some of them make little sense here
189           (e.g. --script FILE). */
190        flags["autovacuum"] = "Enable AUTOVACUUM mode";
191        flags["big-transactions"] = "Important for tests 410 and 510!";
192        //flags["cachesize"] = "N       Set the cache size to N";
193        flags["checkpoint"] = "Run PRAGMA wal_checkpoint after each test case";
194        flags["exclusive"] = "Enable locking_mode=EXCLUSIVE";
195        flags["explain"] = "Like --sqlonly but with added EXPLAIN keywords";
196        //flags["heap"] = "SZ MIN       Memory allocator uses SZ bytes & min allocation MIN";
197        flags["incrvacuum"] = "Enable incremenatal vacuum mode";
198        //flags["journal"] = "M         Set the journal_mode to M";
199        //flags["key"] = "KEY           Set the encryption key to KEY";
200        //flags["lookaside"] = "N SZ    Configure lookaside for N slots of SZ bytes each";
201        flags["memdb"] = "Use an in-memory database";
202        //flags["mmap"] = "SZ           MMAP the first SZ bytes of the database file";
203        flags["multithread"] = "Set multithreaded mode";
204        flags["nomemstat"] = "Disable memory statistics";
205        flags["nomutex"] = "Open db with SQLITE_OPEN_NOMUTEX";
206        flags["nosync"] = "Set PRAGMA synchronous=OFF";
207        flags["notnull"] = "Add NOT NULL constraints to table columns";
208        //flags["output"] = "FILE       Store SQL output in FILE";
209        //flags["pagesize"] = "N        Set the page size to N";
210        //flags["pcache"] = "N SZ       Configure N pages of pagecache each of size SZ bytes";
211        //flags["primarykey"] = "Use PRIMARY KEY instead of UNIQUE where appropriate";
212        //flags["repeat"] = "N          Repeat each SELECT N times (default: 1)";
213        flags["reprepare"] = "Reprepare each statement upon every invocation";
214        //flags["reserve"] = "N         Reserve N bytes on each database page";
215        //flags["script"] = "FILE       Write an SQL script for the test into FILE";
216        flags["serialized"] = "Set serialized threading mode";
217        flags["singlethread"] = "Set single-threaded mode - disables all mutexing";
218        flags["sqlonly"] = "No-op.  Only show the SQL that would have been run.";
219        flags["shrink"] = "memory     Invoke sqlite3_db_release_memory() frequently.";
220        //flags["size"] = "N            Relative test size.  Default=100";
221        flags["strict"] = "Use STRICT table where appropriate";
222        flags["stats"] = "Show statistics at the end";
223        //flags["temp"] = "N            N from 0 to 9.  0: no temp table. 9: all temp tables";
224        //flags["testset"] = "T         Run test-set T (main, cte, rtree, orm, fp, debug)";
225        flags["trace"] = "Turn on SQL tracing";
226        //flags["threads"] = "N         Use up to N threads for sorting";
227        /*
228          The core API's WASM build does not support UTF16, but in
229          this app it's not an issue because the data are not crossing
230          JS/WASM boundaries.
231        */
232        flags["utf16be"] = "Set text encoding to UTF-16BE";
233        flags["utf16le"] = "Set text encoding to UTF-16LE";
234        flags["verify"] = "Run additional verification steps.";
235        flags["without"] = "rowid     Use WITHOUT ROWID where appropriate";
236        const preselectedFlags = [
237          'big-transactions',
238          'memdb',
239          'singlethread'
240        ];
241        Object.keys(flags).sort().forEach(function(f){
242          const opt = document.createElement('option');
243          eFlags.appendChild(opt);
244          const lbl = nbspPad('--'+f)+flags[f];
245          //opt.innerText = lbl;
246          opt.innerHTML = lbl;
247          opt.value = '--'+f;
248          if(preselectedFlags.indexOf(f) >= 0) opt.selected = true;
249        });
250
251        const cbReverseLog = E('#cb-reverse-log-order');
252        const lblReverseLog = E('#lbl-reverse-log-order');
253        if(cbReverseLog.checked){
254          lblReverseLog.classList.add('warning');
255          eOut.classList.add('reverse');
256        }
257        cbReverseLog.addEventListener('change', function(){
258          if(this.checked){
259            eOut.classList.add('reverse');
260            lblReverseLog.classList.add('warning');
261          }else{
262            eOut.classList.remove('reverse');
263            lblReverseLog.classList.remove('warning');
264          }
265        }, false);
266        updateSelectedFlags();
267      }
268      E('#btn-output-clear').addEventListener('click', ()=>{
269        eOut.innerText = '';
270      });
271      E('#btn-reset-flags').addEventListener('click',()=>{
272        eFlags.value = '';
273        updateSelectedFlags();
274      });
275      E('#btn-run').addEventListener('click',function(){
276        log("Running speedtest1. UI controls will be disabled until it completes.");
277        mPost('run', getSelectedFlags());
278      });
279
280      const eControls = E('#ui-controls');
281      /** Update Emscripten-related UI elements while loading the module. */
282      const updateLoadStatus = function f(text){
283        if(!f.last){
284          f.last = { text: '', step: 0 };
285          const E = (cssSelector)=>document.querySelector(cssSelector);
286          f.ui = {
287            status: E('#module-status'),
288            progress: E('#module-progress'),
289            spinner: E('#module-spinner')
290          };
291        }
292        if(text === f.last.text) return;
293        f.last.text = text;
294        if(f.ui.progress){
295          f.ui.progress.value = f.last.step;
296          f.ui.progress.max = f.last.step + 1;
297        }
298        ++f.last.step;
299        if(text) {
300          f.ui.status.classList.remove('hidden');
301          f.ui.status.innerText = text;
302        }else{
303          if(f.ui.progress){
304            f.ui.progress.remove();
305            f.ui.spinner.remove();
306            delete f.ui.progress;
307            delete f.ui.spinner;
308          }
309          f.ui.status.classList.add('hidden');
310        }
311      };
312
313      W.onmessage = function(msg){
314        msg = msg.data;
315        switch(msg.type){
316            case 'ready':
317              log("Worker is ready.");
318              eControls.classList.remove('hidden');
319              break;
320            case 'stdout': log(msg.data); break;
321            case 'stdout': logErr(msg.data); break;
322            case 'run-start':
323              eControls.disabled = true;
324              log("Running speedtest1 with argv =",msg.data.join(' '));
325              break;
326            case 'run-end':
327              log("speedtest1 finished.");
328              eControls.disabled = false;
329              // app output is in msg.data
330              break;
331            case 'error': logErr(msg.data); break;
332            case 'load-status': updateLoadStatus(msg.data); break;
333            default:
334              logErr("Unhandled worker message type:",msg);
335              break;
336        }
337      };
338    })();</script>
339  </body>
340</html>
341