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["cachesize"] = "N       Set the cache size to N";
180        flags["checkpoint"] = "Run PRAGMA wal_checkpoint after each test case";
181        flags["exclusive"] = "Enable locking_mode=EXCLUSIVE";
182        flags["explain"] = "Like --sqlonly but with added EXPLAIN keywords";
183        //flags["heap"] = "SZ MIN       Memory allocator uses SZ bytes & min allocation MIN";
184        flags["incrvacuum"] = "Enable incremenatal vacuum mode";
185        //flags["journal"] = "M         Set the journal_mode to M";
186        //flags["key"] = "KEY           Set the encryption key to KEY";
187        //flags["lookaside"] = "N SZ    Configure lookaside for N slots of SZ bytes each";
188        flags["memdb"] = "Use an in-memory database";
189        //flags["mmap"] = "SZ           MMAP the first SZ bytes of the database file";
190        flags["multithread"] = "Set multithreaded mode";
191        flags["nomemstat"] = "Disable memory statistics";
192        flags["nomutex"] = "Open db with SQLITE_OPEN_NOMUTEX";
193        flags["nosync"] = "Set PRAGMA synchronous=OFF";
194        flags["notnull"] = "Add NOT NULL constraints to table columns";
195        //flags["output"] = "FILE       Store SQL output in FILE";
196        //flags["pagesize"] = "N        Set the page size to N";
197        //flags["pcache"] = "N SZ       Configure N pages of pagecache each of size SZ bytes";
198        //flags["primarykey"] = "Use PRIMARY KEY instead of UNIQUE where appropriate";
199        //flags["repeat"] = "N          Repeat each SELECT N times (default: 1)";
200        flags["reprepare"] = "Reprepare each statement upon every invocation";
201        //flags["reserve"] = "N         Reserve N bytes on each database page";
202        //flags["script"] = "FILE       Write an SQL script for the test into FILE";
203        flags["serialized"] = "Set serialized threading mode";
204        flags["singlethread"] = "Set single-threaded mode - disables all mutexing";
205        flags["sqlonly"] = "No-op.  Only show the SQL that would have been run.";
206        flags["shrink"] = "memory     Invoke sqlite3_db_release_memory() frequently.";
207        //flags["size"] = "N            Relative test size.  Default=100";
208        flags["strict"] = "Use STRICT table where appropriate";
209        flags["stats"] = "Show statistics at the end";
210        //flags["temp"] = "N            N from 0 to 9.  0: no temp table. 9: all temp tables";
211        //flags["testset"] = "T         Run test-set T (main, cte, rtree, orm, fp, debug)";
212        flags["trace"] = "Turn on SQL tracing";
213        //flags["threads"] = "N         Use up to N threads for sorting";
214        /*
215          The core API's WASM build does not support UTF16, but in
216          this app it's not an issue because the data are not crossing
217          JS/WASM boundaries.
218        */
219        flags["utf16be"] = "Set text encoding to UTF-16BE";
220        flags["utf16le"] = "Set text encoding to UTF-16LE";
221        flags["verify"] = "Run additional verification steps.";
222        flags["without"] = "rowid     Use WITHOUT ROWID where appropriate";
223        const preselectedFlags = [
224          'singlethread',
225          'memdb'
226        ];
227        Object.keys(flags).sort().forEach(function(f){
228          const opt = document.createElement('option');
229          eFlags.appendChild(opt);
230          const lbl = nbspPad('--'+f)+flags[f];
231          //opt.innerText = lbl;
232          opt.innerHTML = lbl;
233          opt.value = '--'+f;
234          if(preselectedFlags.indexOf(f) >= 0) opt.selected = true;
235        });
236
237        const cbReverseLog = E('#cb-reverse-log-order');
238        const lblReverseLog = E('#lbl-reverse-log-order');
239        if(cbReverseLog.checked){
240          lblReverseLog.classList.add('warning');
241          eOut.classList.add('reverse');
242        }
243        cbReverseLog.addEventListener('change', function(){
244          if(this.checked){
245            eOut.classList.add('reverse');
246            lblReverseLog.classList.add('warning');
247          }else{
248            eOut.classList.remove('reverse');
249            lblReverseLog.classList.remove('warning');
250          }
251        }, false);
252        updateSelectedFlags();
253      }
254      E('#btn-output-clear').addEventListener('click', ()=>{
255        eOut.innerText = '';
256      });
257      E('#btn-reset-flags').addEventListener('click',()=>{
258        eFlags.value = '';
259        updateSelectedFlags();
260      });
261      E('#btn-run').addEventListener('click',function(){
262        log("Running speedtest1. UI controls will be disabled until it completes.");
263        mPost('run', getSelectedFlags());
264      });
265
266      const eControls = E('#ui-controls');
267      /** Update Emscripten-related UI elements while loading the module. */
268      const updateLoadStatus = function f(text){
269        if(!f.last){
270          f.last = { text: '', step: 0 };
271          const E = (cssSelector)=>document.querySelector(cssSelector);
272          f.ui = {
273            status: E('#module-status'),
274            progress: E('#module-progress'),
275            spinner: E('#module-spinner')
276          };
277        }
278        if(text === f.last.text) return;
279        f.last.text = text;
280        if(f.ui.progress){
281          f.ui.progress.value = f.last.step;
282          f.ui.progress.max = f.last.step + 1;
283        }
284        ++f.last.step;
285        if(text) {
286          f.ui.status.classList.remove('hidden');
287          f.ui.status.innerText = text;
288        }else{
289          if(f.ui.progress){
290            f.ui.progress.remove();
291            f.ui.spinner.remove();
292            delete f.ui.progress;
293            delete f.ui.spinner;
294          }
295          f.ui.status.classList.add('hidden');
296        }
297      };
298
299      W.onmessage = function(msg){
300        msg = msg.data;
301        switch(msg.type){
302            case 'ready':
303              log("Worker is ready.");
304              eControls.classList.remove('hidden');
305              break;
306            case 'stdout': log(msg.data); break;
307            case 'stdout': logErr(msg.data); break;
308            case 'run-start':
309              eControls.disabled = true;
310              log("Running speedtest1 with argv =",msg.data.join(' '));
311              break;
312            case 'run-end':
313              log("speedtest1 finished.");
314              eControls.disabled = false;
315              // app output is in msg.data
316              break;
317            case 'error': logErr(msg.data); break;
318            case 'load-status': updateLoadStatus(msg.data); break;
319            default:
320              logErr("Unhandled worker message type:",arguments[0]);
321              break;
322        }
323      };
324    })();</script>
325  </body>
326</html>
327