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-wasmfs.wasm</title>
10  </head>
11  <body>
12    <header id='titlebar'><span>speedtest1-wasmfs.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-wasmfs.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_wasmfs', 'i32', ['string'], pdir
59            )){
60              return f._ = pdir;
61            }else{
62              return f._ = "";
63            }
64          }catch(e){
65            // sqlite3_wasm_init_wasmfs() 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) log2('',"Persistent storage:",pDir);
108          else{
109            log2('error',"Expecting persistent storage in this build.");
110            return;
111          }
112          const scope = wasm.scopedAllocPush();
113          const dbFile = 0 ? "" : pDir+"/speedtest1.db";
114          const urlArgs = self.SqliteTestUtil.processUrlArgs();
115          const argv = ["speedtest1"];
116          if(urlArgs.flags){
117            argv.push(...(urlArgs.flags.split(',')));
118          }else{
119            argv.push(
120              "--singlethread",
121              "--nomutex",
122              "--nosync",
123              "--nomemstat"
124            );
125            //"--memdb", // note that memdb trumps the filename arg
126          }
127          if(argv.indexOf('--memdb')>=0){
128              log2('error',"WARNING: --memdb flag trumps db filename.");
129          }
130          argv.push("--big-transactions"/*important for tests 410 and 510!*/,
131                    dbFile);
132          console.log("argv =",argv);
133          // These log messages are not emitted to the UI until after main() returns. Fixing that
134          // requires moving the main() call and related cleanup into a timeout handler.
135          if(pDir) unlink(dbFile);
136          log2('',"Starting native app:\n ",argv.join(' '));
137          log2('',"This will take a while and the browser might warn about the runaway JS.",
138               "Give it time...");
139          logList.length = 0;
140          setTimeout(function(){
141              wasm.xCall('wasm_main', argv.length,
142                         wasm.scopedAllocMainArgv(argv));
143              wasm.scopedAllocPop(scope);
144              if(pDir) unlink(dbFile);
145              logList.unshift("Done running native main(). Output:");
146              dumpLogList();
147          }, 50);
148        }/*runTests()*/;
149
150        self.sqlite3TestModule.print = log;
151        self.sqlite3TestModule.printErr = logErr;
152        sqlite3Speedtest1InitModule(self.sqlite3TestModule).then(function(M){
153          setTimeout(()=>runTests(M), 100);
154        });
155      })();
156    </script>
157  </body>
158</html>
159