xref: /sqlite-3.40.0/ext/wasm/speedtest1.html (revision 44a87f08)
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    <div>See also: <a href='speedtest1-worker.html'>A Worker-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    <div class='warning'>This page starts running the main exe when it loads, which will
29      block the UI until it finishes! Adding UI controls to manually configure and start it
30      are TODO.</div>
31    <div>Output is sent to the dev console because we cannot update the UI while the
32      speedtest is running unless/until we move the speedtest to a worker thread.</div>
33    <hr>
34    <div id='test-output'></div>
35    <script src="common/whwasmutil.js"></script>
36    <script src="common/SqliteTestUtil.js"></script>
37    <script src="speedtest1.js"></script>
38    <script>(function(){
39        /**
40           If this environment contains OPFS, this function initializes it and
41           returns the name of the dir on which OPFS is mounted, else it returns
42           an empty string.
43        */
44        const opfsDir = function f(wasmUtil){
45          if(undefined !== f._) return f._;
46          const pdir = '/persistent';
47          if( !self.FileSystemHandle
48              || !self.FileSystemDirectoryHandle
49              || !self.FileSystemFileHandle){
50            return f._ = "";
51          }
52          try{
53            if(0===wasmUtil.xCallWrapped(
54              'sqlite3_wasm_init_opfs', 'i32', ['string'], pdir
55            )){
56              return f._ = pdir;
57            }else{
58              return f._ = "";
59            }
60          }catch(e){
61            // sqlite3_wasm_init_opfs() is not available
62            return f._ = "";
63          }
64        };
65        opfsDir._ = undefined;
66
67        const eOut = document.querySelector('#test-output');
68        const log2 = async function(cssClass,...args){
69          const ln = document.createElement('div');
70          if(cssClass) ln.classList.add(cssClass);
71          ln.append(document.createTextNode(args.join(' ')));
72          eOut.append(ln);
73          //this.e.output.lastElementChild.scrollIntoViewIfNeeded();
74        };
75        const doHtmlOutput = false
76        /* can't update DOM while speedtest is running unless we run
77           speedtest in a worker thread. */;
78        const log = (...args)=>{
79          console.log(...args);
80          if(doHtmlOutput) log2('', ...args);
81        };
82        const logErr = function(...args){
83          console.error(...args);
84          if(doHtmlOutput) log2('error', ...args);
85        };
86
87        const runTests = function(EmscriptenModule){
88          console.log("Module inited.",EmscriptenModule);
89          const wasm = {
90            exports: EmscriptenModule.asm,
91            alloc: (n)=>EmscriptenModule._malloc(n),
92            dealloc: (m)=>EmscriptenModule._free(m),
93            memory: EmscriptenModule.asm.memory || EmscriptenModule.wasmMemory
94          };
95          //console.debug('wasm =',wasm);
96          self.WhWasmUtilInstaller(wasm);
97          const unlink = wasm.xWrap("sqlite3_wasm_vfs_unlink", "int", ["string"]);
98          const pDir = opfsDir(wasm);
99          if(pDir){
100            console.warn("Persistent storage:",pDir);
101          }
102          const scope = wasm.scopedAllocPush();
103          const dbFile = 0 ? "" : pDir+"/speedtest1.db";
104          try{
105            const argv = [
106              // TODO: accept flags via URL arguments and/or a
107              // UI control. A multi-SELECT element should do
108              // nicely.
109              "speedtest1",
110              "--singlethread",
111              //"--stats",
112              "--memdb", // note that memdb trumps the filename arg
113              dbFile
114            ];
115            console.log("argv =",argv);
116            wasm.xCall('__main_argc_argv', argv.length,
117                       wasm.scopedAllocMainArgv(argv));
118          }finally{
119            wasm.scopedAllocPop(scope);
120            if(pDir) unlink(dbFile);
121          }
122        };
123
124        self.sqlite3TestModule.print = log;
125        self.sqlite3TestModule.printErr = logErr;
126        sqlite3Speedtest1InitModule(self.sqlite3TestModule).then(function(M){
127          setTimeout(()=>runTests(M), 100);
128        });
129      })();
130    </script>
131  </body>
132</html>
133