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