xref: /sqlite-3.40.0/ext/wasm/speedtest1.html (revision 28ef9bdd)
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>
31dd628ed5Sstephan    </div>
32e66b2681Sstephan    <div class='warning'>Achtung: running it with the dev tools open may
33e66b2681Sstephan      <em>drastically</em> slow it down. For faster results, keep the dev
34e66b2681Sstephan      tools closed when running it!
35dd628ed5Sstephan    </div>
36e66b2681Sstephan    <div>Output is delayed/buffered because we cannot update the UI while the
37e66b2681Sstephan      speedtest is running. Output will appear below when ready...
38e66b2681Sstephan    <div id='test-output'></div>
39100b496dSstephan    <script src="common/whwasmutil.js"></script>
40100b496dSstephan    <script src="common/SqliteTestUtil.js"></script>
41100b496dSstephan    <script src="speedtest1.js"></script>
428fc8b5b3Sstephan    <script>(function(){
438fc8b5b3Sstephan        /**
448fc8b5b3Sstephan           If this environment contains OPFS, this function initializes it and
458fc8b5b3Sstephan           returns the name of the dir on which OPFS is mounted, else it returns
468fc8b5b3Sstephan           an empty string.
478fc8b5b3Sstephan        */
488fc8b5b3Sstephan        const opfsDir = function f(wasmUtil){
498fc8b5b3Sstephan          if(undefined !== f._) return f._;
508fc8b5b3Sstephan          const pdir = '/persistent';
518fc8b5b3Sstephan          if( !self.FileSystemHandle
528fc8b5b3Sstephan              || !self.FileSystemDirectoryHandle
538fc8b5b3Sstephan              || !self.FileSystemFileHandle){
548fc8b5b3Sstephan            return f._ = "";
558fc8b5b3Sstephan          }
568fc8b5b3Sstephan          try{
578fc8b5b3Sstephan            if(0===wasmUtil.xCallWrapped(
58*28ef9bddSstephan              'sqlite3_wasm_init_wasmfs', 'i32', ['string'], pdir
598fc8b5b3Sstephan            )){
608fc8b5b3Sstephan              return f._ = pdir;
618fc8b5b3Sstephan            }else{
628fc8b5b3Sstephan              return f._ = "";
638fc8b5b3Sstephan            }
648fc8b5b3Sstephan          }catch(e){
65*28ef9bddSstephan            // sqlite3_wasm_init_wasmfs() is not available
668fc8b5b3Sstephan            return f._ = "";
678fc8b5b3Sstephan          }
688fc8b5b3Sstephan        };
698fc8b5b3Sstephan        opfsDir._ = undefined;
708fc8b5b3Sstephan
71100b496dSstephan        const eOut = document.querySelector('#test-output');
72dd628ed5Sstephan        const log2 = function(cssClass,...args){
73100b496dSstephan          const ln = document.createElement('div');
74100b496dSstephan          if(cssClass) ln.classList.add(cssClass);
75100b496dSstephan          ln.append(document.createTextNode(args.join(' ')));
76100b496dSstephan          eOut.append(ln);
77100b496dSstephan          //this.e.output.lastElementChild.scrollIntoViewIfNeeded();
78100b496dSstephan        };
79e66b2681Sstephan        const logList = [];
80e66b2681Sstephan        const dumpLogList = function(){
81e66b2681Sstephan          logList.forEach((v)=>log2('',v));
82e66b2681Sstephan          logList.length = 0;
83e66b2681Sstephan        };
84100b496dSstephan        /* can't update DOM while speedtest is running unless we run
85100b496dSstephan           speedtest in a worker thread. */;
868fc8b5b3Sstephan        const log = (...args)=>{
87100b496dSstephan          console.log(...args);
88e66b2681Sstephan          logList.push(args.join(' '));
89100b496dSstephan        };
90100b496dSstephan        const logErr = function(...args){
91100b496dSstephan          console.error(...args);
92e66b2681Sstephan          logList.push('ERROR: '+args.join(' '));
93100b496dSstephan        };
94100b496dSstephan
95100b496dSstephan        const runTests = function(EmscriptenModule){
968fc8b5b3Sstephan          console.log("Module inited.",EmscriptenModule);
97100b496dSstephan          const wasm = {
98100b496dSstephan            exports: EmscriptenModule.asm,
99100b496dSstephan            alloc: (n)=>EmscriptenModule._malloc(n),
1008fc8b5b3Sstephan            dealloc: (m)=>EmscriptenModule._free(m),
1018fc8b5b3Sstephan            memory: EmscriptenModule.asm.memory || EmscriptenModule.wasmMemory
102100b496dSstephan          };
103100b496dSstephan          //console.debug('wasm =',wasm);
104100b496dSstephan          self.WhWasmUtilInstaller(wasm);
1058fc8b5b3Sstephan          const unlink = wasm.xWrap("sqlite3_wasm_vfs_unlink", "int", ["string"]);
1068fc8b5b3Sstephan          const pDir = opfsDir(wasm);
1078fc8b5b3Sstephan          if(pDir){
1088fc8b5b3Sstephan            console.warn("Persistent storage:",pDir);
1098fc8b5b3Sstephan          }
110100b496dSstephan          const scope = wasm.scopedAllocPush();
1118fc8b5b3Sstephan          const dbFile = 0 ? "" : pDir+"/speedtest1.db";
112e66b2681Sstephan          const urlArgs = self.SqliteTestUtil.processUrlArgs();
113e66b2681Sstephan          const argv = ["speedtest1"];
114e66b2681Sstephan          if(urlArgs.flags){
115e66b2681Sstephan            // transform flags=a,b,c to ["--a", "--b", "--c"]
116e66b2681Sstephan            argv.push(...(urlArgs.flags.split(',').map((v)=>'--'+v)));
117e66b2681Sstephan          }else{
118e66b2681Sstephan            argv.push(
1198fc8b5b3Sstephan              "--singlethread",
1201174c23eSstephan              "--nomutex",
121dd628ed5Sstephan              "--nosync",
122e66b2681Sstephan              "--nomemstat"
123e66b2681Sstephan            );
124dd628ed5Sstephan            //"--memdb", // note that memdb trumps the filename arg
125*28ef9bddSstephan          }
126e66b2681Sstephan          argv.push("--big-transactions"/*important for tests 410 and 510!*/,
127e66b2681Sstephan                    dbFile);
1288fc8b5b3Sstephan          console.log("argv =",argv);
129dd628ed5Sstephan          // These log messages are not emitted to the UI until after main() returns. Fixing that
130dd628ed5Sstephan          // requires moving the main() call and related cleanup into a timeout handler.
131e66b2681Sstephan          if(pDir) unlink(dbFile);
132e66b2681Sstephan          log2('',"Starting native app:\n ",argv.join(' '));
133e66b2681Sstephan          log2('',"This will take a while and the browser might warn about the runaway JS.",
134e66b2681Sstephan               "Give it time...");
135e66b2681Sstephan          logList.length = 0;
136dd628ed5Sstephan          setTimeout(function(){
1378fc8b5b3Sstephan              wasm.xCall('__main_argc_argv', argv.length,
1388fc8b5b3Sstephan                         wasm.scopedAllocMainArgv(argv));
139100b496dSstephan              wasm.scopedAllocPop(scope);
1408fc8b5b3Sstephan              if(pDir) unlink(dbFile);
141e66b2681Sstephan              logList.unshift("Done running native main(). Output:");
142e66b2681Sstephan              dumpLogList();
143e66b2681Sstephan          }, 50);
144dd628ed5Sstephan        }/*runTests()*/;
145100b496dSstephan
1468fc8b5b3Sstephan        self.sqlite3TestModule.print = log;
147100b496dSstephan        self.sqlite3TestModule.printErr = logErr;
148100b496dSstephan        sqlite3Speedtest1InitModule(self.sqlite3TestModule).then(function(M){
149100b496dSstephan          setTimeout(()=>runTests(M), 100);
150100b496dSstephan        });
151100b496dSstephan      })();
152100b496dSstephan    </script>
153100b496dSstephan  </body>
154100b496dSstephan</html>
155