xref: /sqlite-3.40.0/ext/wasm/speedtest1.html (revision dd628ed5)
1100b496dSstephan<!doctype html>
2100b496dSstephan<html lang="en-us">
3100b496dSstephan  <head>
4100b496dSstephan    <meta charset="utf-8">
5100b496dSstephan    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6100b496dSstephan    <link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
7100b496dSstephan    <link rel="stylesheet" href="common/emscripten.css"/>
8100b496dSstephan    <link rel="stylesheet" href="common/testing.css"/>
9100b496dSstephan    <title>speedtest1.wasm</title>
10100b496dSstephan  </head>
11100b496dSstephan  <body>
12100b496dSstephan    <header id='titlebar'><span>speedtest1.wasm</span></header>
1344a87f08Sstephan    <div>See also: <a href='speedtest1-worker.html'>A Worker-thread variant of this page.</a></div>
14100b496dSstephan    <!-- emscripten bits -->
15100b496dSstephan    <figure id="module-spinner">
16100b496dSstephan      <div class="spinner"></div>
17100b496dSstephan      <div class='center'><strong>Initializing app...</strong></div>
18100b496dSstephan      <div class='center'>
19100b496dSstephan        On a slow internet connection this may take a moment.  If this
20100b496dSstephan        message displays for "a long time", intialization may have
21100b496dSstephan        failed and the JavaScript console may contain clues as to why.
22100b496dSstephan      </div>
23100b496dSstephan    </figure>
24100b496dSstephan    <div class="emscripten" id="module-status">Downloading...</div>
25100b496dSstephan    <div class="emscripten">
26100b496dSstephan      <progress value="0" max="100" id="module-progress" hidden='1'></progress>
27100b496dSstephan    </div><!-- /emscripten bits -->
2844a87f08Sstephan    <div class='warning'>This page starts running the main exe when it loads, which will
2944a87f08Sstephan      block the UI until it finishes! Adding UI controls to manually configure and start it
3044a87f08Sstephan      are TODO.</div>
31100b496dSstephan    <div>Output is sent to the dev console because we cannot update the UI while the
32*dd628ed5Sstephan      speedtest is running unless/until we move the speedtest to a worker thread.
33*dd628ed5Sstephan    </div>
34*dd628ed5Sstephan      <div class='warning'>Achtung: running it with the dev tools open <em>drastically</em>
35*dd628ed5Sstephan        slows it down: by a factor of 2.5+. For faster results, keep the dev tools closed
36*dd628ed5Sstephan        when running it!
37*dd628ed5Sstephan    </div>
38100b496dSstephan    <hr>
39*dd628ed5Sstephan    <div id='test-output'>
40*dd628ed5Sstephan    </div>
41100b496dSstephan    <script src="common/whwasmutil.js"></script>
42100b496dSstephan    <script src="common/SqliteTestUtil.js"></script>
43100b496dSstephan    <script src="speedtest1.js"></script>
448fc8b5b3Sstephan    <script>(function(){
458fc8b5b3Sstephan        /**
468fc8b5b3Sstephan           If this environment contains OPFS, this function initializes it and
478fc8b5b3Sstephan           returns the name of the dir on which OPFS is mounted, else it returns
488fc8b5b3Sstephan           an empty string.
498fc8b5b3Sstephan        */
508fc8b5b3Sstephan        const opfsDir = function f(wasmUtil){
518fc8b5b3Sstephan          if(undefined !== f._) return f._;
528fc8b5b3Sstephan          const pdir = '/persistent';
538fc8b5b3Sstephan          if( !self.FileSystemHandle
548fc8b5b3Sstephan              || !self.FileSystemDirectoryHandle
558fc8b5b3Sstephan              || !self.FileSystemFileHandle){
568fc8b5b3Sstephan            return f._ = "";
578fc8b5b3Sstephan          }
588fc8b5b3Sstephan          try{
598fc8b5b3Sstephan            if(0===wasmUtil.xCallWrapped(
608fc8b5b3Sstephan              'sqlite3_wasm_init_opfs', 'i32', ['string'], pdir
618fc8b5b3Sstephan            )){
628fc8b5b3Sstephan              return f._ = pdir;
638fc8b5b3Sstephan            }else{
648fc8b5b3Sstephan              return f._ = "";
658fc8b5b3Sstephan            }
668fc8b5b3Sstephan          }catch(e){
678fc8b5b3Sstephan            // sqlite3_wasm_init_opfs() is not available
688fc8b5b3Sstephan            return f._ = "";
698fc8b5b3Sstephan          }
708fc8b5b3Sstephan        };
718fc8b5b3Sstephan        opfsDir._ = undefined;
728fc8b5b3Sstephan
73100b496dSstephan        const eOut = document.querySelector('#test-output');
74*dd628ed5Sstephan        const log2 = function(cssClass,...args){
75100b496dSstephan          const ln = document.createElement('div');
76100b496dSstephan          if(cssClass) ln.classList.add(cssClass);
77100b496dSstephan          ln.append(document.createTextNode(args.join(' ')));
78100b496dSstephan          eOut.append(ln);
79100b496dSstephan          //this.e.output.lastElementChild.scrollIntoViewIfNeeded();
80100b496dSstephan        };
81100b496dSstephan        const doHtmlOutput = false
82100b496dSstephan        /* can't update DOM while speedtest is running unless we run
83100b496dSstephan           speedtest in a worker thread. */;
848fc8b5b3Sstephan        const log = (...args)=>{
85100b496dSstephan          console.log(...args);
868fc8b5b3Sstephan          if(doHtmlOutput) log2('', ...args);
87100b496dSstephan        };
88100b496dSstephan        const logErr = function(...args){
89100b496dSstephan          console.error(...args);
908fc8b5b3Sstephan          if(doHtmlOutput) log2('error', ...args);
91100b496dSstephan        };
92100b496dSstephan
93100b496dSstephan        const runTests = function(EmscriptenModule){
948fc8b5b3Sstephan          console.log("Module inited.",EmscriptenModule);
95100b496dSstephan          const wasm = {
96100b496dSstephan            exports: EmscriptenModule.asm,
97100b496dSstephan            alloc: (n)=>EmscriptenModule._malloc(n),
988fc8b5b3Sstephan            dealloc: (m)=>EmscriptenModule._free(m),
998fc8b5b3Sstephan            memory: EmscriptenModule.asm.memory || EmscriptenModule.wasmMemory
100100b496dSstephan          };
101100b496dSstephan          //console.debug('wasm =',wasm);
102100b496dSstephan          self.WhWasmUtilInstaller(wasm);
1038fc8b5b3Sstephan          const unlink = wasm.xWrap("sqlite3_wasm_vfs_unlink", "int", ["string"]);
1048fc8b5b3Sstephan          const pDir = opfsDir(wasm);
1058fc8b5b3Sstephan          if(pDir){
1068fc8b5b3Sstephan            console.warn("Persistent storage:",pDir);
1078fc8b5b3Sstephan          }
108100b496dSstephan          const scope = wasm.scopedAllocPush();
1098fc8b5b3Sstephan          const dbFile = 0 ? "" : pDir+"/speedtest1.db";
1108fc8b5b3Sstephan          const argv = [
111100b496dSstephan            // TODO: accept flags via URL arguments and/or a
112100b496dSstephan            // UI control. A multi-SELECT element should do
113100b496dSstephan            // nicely.
114100b496dSstephan            "speedtest1",
1158fc8b5b3Sstephan            "--singlethread",
1161174c23eSstephan            "--nomutex",
117*dd628ed5Sstephan            "--nosync",
118*dd628ed5Sstephan            "--nomemstat",
119*dd628ed5Sstephan            "--big-transactions", // important for tests 410 and 510!
120*dd628ed5Sstephan            //"--memdb", // note that memdb trumps the filename arg
1218fc8b5b3Sstephan            dbFile
122100b496dSstephan          ];
1238fc8b5b3Sstephan          console.log("argv =",argv);
124*dd628ed5Sstephan          // These log messages are not emitted to the UI until after main() returns. Fixing that
125*dd628ed5Sstephan          // requires moving the main() call and related cleanup into a timeout handler.
126*dd628ed5Sstephan          log2('',"Starting native main() with flags:",argv.join(' '));
127*dd628ed5Sstephan          log2('',"This will take a while and the browser might warn about the runaway JS. Give it time.");
128*dd628ed5Sstephan          setTimeout(function(){
1298fc8b5b3Sstephan              wasm.xCall('__main_argc_argv', argv.length,
1308fc8b5b3Sstephan                         wasm.scopedAllocMainArgv(argv));
131100b496dSstephan              wasm.scopedAllocPop(scope);
1328fc8b5b3Sstephan              if(pDir) unlink(dbFile);
133*dd628ed5Sstephan              log2('',"Done running native main(). Check dev console for output.");
134*dd628ed5Sstephan          }, 100);
135*dd628ed5Sstephan        }/*runTests()*/;
136100b496dSstephan
1378fc8b5b3Sstephan        self.sqlite3TestModule.print = log;
138100b496dSstephan        self.sqlite3TestModule.printErr = logErr;
139100b496dSstephan        sqlite3Speedtest1InitModule(self.sqlite3TestModule).then(function(M){
140100b496dSstephan          setTimeout(()=>runTests(M), 100);
141100b496dSstephan        });
142100b496dSstephan      })();
143100b496dSstephan    </script>
144100b496dSstephan  </body>
145100b496dSstephan</html>
146