xref: /sqlite-3.40.0/ext/wasm/speedtest1.html (revision b5ae85ec)
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 wasmfsDir = 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        wasmfsDir._ = 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(sqlite3){
96          const capi = sqlite3.capi, wasm = capi.wasm;
97          //console.debug('sqlite3 =',sqlite3);
98          const unlink = wasm.xWrap("sqlite3_wasm_vfs_unlink", "int", ["string"]);
99          const pDir = wasmfsDir(wasm);
100          if(pDir){
101            console.warn("Persistent storage:",pDir);
102          }
103          const scope = wasm.scopedAllocPush();
104          let dbFile = pDir+"/speedtest1.db";
105          const urlParams = self.SqliteTestUtil.processUrlArgs();
106          const argv = ["speedtest1"];
107          if('string'===typeof urlParams.vfs){
108              if(!capi.sqlite3_vfs_find(urlParams.vfs)){
109                  log2('error',"Unknown VFS:",urlParams.vfs);
110                  return;
111              }
112              argv.push("--vfs", urlParams.vfs);
113              log2('',"Using VFS:",urlParams.vfs);
114              if('kvvfs' === urlParams.vfs){
115                  urlParams.size = 1;
116                  dbFile = 'session';
117                  log2('',"kvvfs VFS: forcing --size 1 and filename 'session'");
118                  capi.sqlite3_web_kvvfs_clear('session');
119              }
120          }
121          [
122              'size'
123          ].forEach(function(k){
124              const v = urlParams[k];
125              if(v) argv.push('--'+k, urlParams[k]);
126          });
127          if(urlParams.flags){
128            // transform flags=a,b,c to ["--a", "--b", "--c"]
129            argv.push(...(urlParams.flags.split(',').map((v)=>'--'+v)));
130          }else{
131            argv.push(
132              "--singlethread",
133              "--nomutex",
134              "--nosync",
135              "--nomemstat"
136            );
137            //"--memdb", // note that memdb trumps the filename arg
138          }
139          argv.push("--big-transactions"/*important for tests 410 and 510!*/,
140                    dbFile);
141          console.log("argv =",argv);
142          // These log messages are not emitted to the UI until after main() returns. Fixing that
143          // requires moving the main() call and related cleanup into a timeout handler.
144          if(pDir) unlink(dbFile);
145          log2('',"Starting native app:\n ",argv.join(' '));
146          log2('',"This will take a while and the browser might warn about the runaway JS.",
147               "Give it time...");
148          logList.length = 0;
149          setTimeout(function(){
150              wasm.xCall('wasm_main', argv.length,
151                         wasm.scopedAllocMainArgv(argv));
152              wasm.scopedAllocPop(scope);
153              if(pDir) unlink(dbFile);
154              logList.unshift("Done running native main(). Output:");
155              dumpLogList();
156          }, 50);
157        }/*runTests()*/;
158
159        self.sqlite3TestModule.print = log;
160        self.sqlite3TestModule.printErr = logErr;
161        sqlite3Speedtest1InitModule(self.sqlite3TestModule)
162        .then((EmscriptenModule)=>{
163            return EmscriptenModule.sqlite3.installOpfsVfs()
164                .catch((e)=>{console.warn(e.message)})
165                .then(()=>runTests(EmscriptenModule.sqlite3));
166        });
167      })();
168    </script>
169  </body>
170</html>
171