xref: /sqlite-3.40.0/ext/wasm/speedtest1.html (revision 8fc8b5b3)
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</title>
10  </head>
11  <body>
12    <header id='titlebar'><span>speedtest1.wasm</span></header>
13    <!-- emscripten bits -->
14    <figure id="module-spinner">
15      <div class="spinner"></div>
16      <div class='center'><strong>Initializing app...</strong></div>
17      <div class='center'>
18        On a slow internet connection this may take a moment.  If this
19        message displays for "a long time", intialization may have
20        failed and the JavaScript console may contain clues as to why.
21      </div>
22    </figure>
23    <div class="emscripten" id="module-status">Downloading...</div>
24    <div class="emscripten">
25      <progress value="0" max="100" id="module-progress" hidden='1'></progress>
26    </div><!-- /emscripten bits -->
27    <div>Output is sent to the dev console because we cannot update the UI while the
28      speedtest is running unless/until we move the speedtest to a worker thread.</div>
29    <hr>
30    <div id='test-output'></div>
31    <script src="common/whwasmutil.js"></script>
32    <script src="common/SqliteTestUtil.js"></script>
33    <script src="speedtest1.js"></script>
34    <script>(function(){
35        /**
36           If this environment contains OPFS, this function initializes it and
37           returns the name of the dir on which OPFS is mounted, else it returns
38           an empty string.
39        */
40        const opfsDir = function f(wasmUtil){
41          if(undefined !== f._) return f._;
42          const pdir = '/persistent';
43          if( !self.FileSystemHandle
44              || !self.FileSystemDirectoryHandle
45              || !self.FileSystemFileHandle){
46            return f._ = "";
47          }
48          try{
49            if(0===wasmUtil.xCallWrapped(
50              'sqlite3_wasm_init_opfs', 'i32', ['string'], pdir
51            )){
52              return f._ = pdir;
53            }else{
54              return f._ = "";
55            }
56          }catch(e){
57            // sqlite3_wasm_init_opfs() is not available
58            return f._ = "";
59          }
60        };
61        opfsDir._ = undefined;
62
63        const eOut = document.querySelector('#test-output');
64        const log2 = async function(cssClass,...args){
65          const ln = document.createElement('div');
66          if(cssClass) ln.classList.add(cssClass);
67          ln.append(document.createTextNode(args.join(' ')));
68          eOut.append(ln);
69          //this.e.output.lastElementChild.scrollIntoViewIfNeeded();
70        };
71        const doHtmlOutput = false
72        /* can't update DOM while speedtest is running unless we run
73           speedtest in a worker thread. */;
74        const log = (...args)=>{
75          console.log(...args);
76          if(doHtmlOutput) log2('', ...args);
77        };
78        const logErr = function(...args){
79          console.error(...args);
80          if(doHtmlOutput) log2('error', ...args);
81        };
82
83        const runTests = function(EmscriptenModule){
84          console.log("Module inited.",EmscriptenModule);
85          const wasm = {
86            exports: EmscriptenModule.asm,
87            alloc: (n)=>EmscriptenModule._malloc(n),
88            dealloc: (m)=>EmscriptenModule._free(m),
89            memory: EmscriptenModule.asm.memory || EmscriptenModule.wasmMemory
90          };
91          //console.debug('wasm =',wasm);
92          self.WhWasmUtilInstaller(wasm);
93          const unlink = wasm.xWrap("sqlite3_wasm_vfs_unlink", "int", ["string"]);
94          const pDir = opfsDir(wasm);
95          if(pDir){
96            console.warn("Persistent storage:",pDir);
97          }
98          const scope = wasm.scopedAllocPush();
99          const dbFile = 0 ? "" : pDir+"/speedtest1.db";
100          try{
101            const argv = [
102              // TODO: accept flags via URL arguments and/or a
103              // UI control. A multi-SELECT element should do
104              // nicely.
105              "speedtest1",
106              "--singlethread",
107              //"--stats",
108              "--memdb", // note that memdb trumps the filename arg
109              dbFile
110            ];
111            console.log("argv =",argv);
112            wasm.xCall('__main_argc_argv', argv.length,
113                       wasm.scopedAllocMainArgv(argv));
114          }finally{
115            wasm.scopedAllocPop(scope);
116            if(pDir) unlink(dbFile);
117          }
118        };
119
120        self.sqlite3TestModule.print = log;
121        self.sqlite3TestModule.printErr = logErr;
122        sqlite3Speedtest1InitModule(self.sqlite3TestModule).then(function(M){
123          setTimeout(()=>runTests(M), 100);
124        });
125      })();
126    </script>
127  </body>
128</html>
129