xref: /sqlite-3.40.0/ext/wasm/speedtest1.html (revision a002cc17)
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>
32    <div class='warning'>Achtung: running it with the dev tools open may
33      <em>drastically</em> slow it down. For faster results, keep the dev
34      tools closed when running it!
35    </div>
36    <div>Output is delayed/buffered because we cannot update the UI while the
37      speedtest is running. Output will appear below when ready...
38    <div id='test-output'></div>
39    <script src="common/whwasmutil.js"></script>
40    <script src="common/SqliteTestUtil.js"></script>
41    <script src="speedtest1.js"></script>
42    <script>(function(){
43        /**
44           If this environment contains OPFS, this function initializes it and
45           returns the name of the dir on which OPFS is mounted, else it returns
46           an empty string.
47        */
48        const opfsDir = function f(wasmUtil){
49          if(undefined !== f._) return f._;
50          const pdir = '/persistent';
51          if( !self.FileSystemHandle
52              || !self.FileSystemDirectoryHandle
53              || !self.FileSystemFileHandle){
54            return f._ = "";
55          }
56          try{
57            if(0===wasmUtil.xCallWrapped(
58              'sqlite3_wasm_init_opfs', 'i32', ['string'], pdir
59            )){
60              return f._ = pdir;
61            }else{
62              return f._ = "";
63            }
64          }catch(e){
65            // sqlite3_wasm_init_opfs() is not available
66            return f._ = "";
67          }
68        };
69        opfsDir._ = undefined;
70
71        const eOut = document.querySelector('#test-output');
72        const log2 = function(cssClass,...args){
73          const ln = document.createElement('div');
74          if(cssClass) ln.classList.add(cssClass);
75          ln.append(document.createTextNode(args.join(' ')));
76          eOut.append(ln);
77          //this.e.output.lastElementChild.scrollIntoViewIfNeeded();
78        };
79        const logList = [];
80        const dumpLogList = function(){
81          logList.forEach((v)=>log2('',v));
82          logList.length = 0;
83        };
84        /* can't update DOM while speedtest is running unless we run
85           speedtest in a worker thread. */;
86        const log = (...args)=>{
87          console.log(...args);
88          logList.push(args.join(' '));
89        };
90        const logErr = function(...args){
91          console.error(...args);
92          logList.push('ERROR: '+args.join(' '));
93        };
94
95        const runTests = function(EmscriptenModule){
96          console.log("Module inited.",EmscriptenModule);
97          const wasm = {
98            exports: EmscriptenModule.asm,
99            alloc: (n)=>EmscriptenModule._malloc(n),
100            dealloc: (m)=>EmscriptenModule._free(m),
101            memory: EmscriptenModule.asm.memory || EmscriptenModule.wasmMemory
102          };
103          //console.debug('wasm =',wasm);
104          self.WhWasmUtilInstaller(wasm);
105          const unlink = wasm.xWrap("sqlite3_wasm_vfs_unlink", "int", ["string"]);
106          const pDir = opfsDir(wasm);
107          if(pDir){
108            console.warn("Persistent storage:",pDir);
109          }
110          const scope = wasm.scopedAllocPush();
111          const dbFile = 0 ? "" : pDir+"/speedtest1.db";
112          const urlArgs = self.SqliteTestUtil.processUrlArgs();
113          const argv = ["speedtest1"];
114          if(urlArgs.flags){
115            // transform flags=a,b,c to ["--a", "--b", "--c"]
116            argv.push(...(urlArgs.flags.split(',').map((v)=>'--'+v)));
117          }else{
118            argv.push(
119              "--singlethread",
120              "--nomutex",
121              "--nosync",
122              "--nomemstat"
123            );
124            //"--memdb", // note that memdb trumps the filename arg
125            argv.push("--big-transactions"/*important for tests 410 and 510!*/,
126                      dbFile);
127          }
128          console.log("argv =",argv);
129          // These log messages are not emitted to the UI until after main() returns. Fixing that
130          // requires moving the main() call and related cleanup into a timeout handler.
131          if(pDir) unlink(dbFile);
132          log2('',"Starting native app:\n ",argv.join(' '));
133          log2('',"This will take a while and the browser might warn about the runaway JS.",
134               "Give it time...");
135          logList.length = 0;
136          setTimeout(function(){
137              wasm.xCall('__main_argc_argv', argv.length,
138                         wasm.scopedAllocMainArgv(argv));
139              wasm.scopedAllocPop(scope);
140              if(pDir) unlink(dbFile);
141              logList.unshift("Done running native main(). Output:");
142              dumpLogList();
143          }, 50);
144        }/*runTests()*/;
145
146        self.sqlite3TestModule.print = log;
147        self.sqlite3TestModule.printErr = logErr;
148        sqlite3Speedtest1InitModule(self.sqlite3TestModule).then(function(M){
149          setTimeout(()=>runTests(M), 100);
150        });
151      })();
152    </script>
153  </body>
154</html>
155