xref: /sqlite-3.40.0/ext/wasm/speedtest1.html (revision fa5aac74)
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/SqliteTestUtil.js"></script>
40100b496dSstephan    <script src="speedtest1.js"></script>
418fc8b5b3Sstephan    <script>(function(){
428fc8b5b3Sstephan        /**
438fc8b5b3Sstephan           If this environment contains OPFS, this function initializes it and
448fc8b5b3Sstephan           returns the name of the dir on which OPFS is mounted, else it returns
458fc8b5b3Sstephan           an empty string.
468fc8b5b3Sstephan        */
47b5ae85ecSstephan        const wasmfsDir = function f(wasmUtil){
488fc8b5b3Sstephan          if(undefined !== f._) return f._;
498fc8b5b3Sstephan          const pdir = '/persistent';
508fc8b5b3Sstephan          if( !self.FileSystemHandle
518fc8b5b3Sstephan              || !self.FileSystemDirectoryHandle
528fc8b5b3Sstephan              || !self.FileSystemFileHandle){
538fc8b5b3Sstephan            return f._ = "";
548fc8b5b3Sstephan          }
558fc8b5b3Sstephan          try{
568fc8b5b3Sstephan            if(0===wasmUtil.xCallWrapped(
5728ef9bddSstephan              'sqlite3_wasm_init_wasmfs', 'i32', ['string'], pdir
588fc8b5b3Sstephan            )){
598fc8b5b3Sstephan              return f._ = pdir;
608fc8b5b3Sstephan            }else{
618fc8b5b3Sstephan              return f._ = "";
628fc8b5b3Sstephan            }
638fc8b5b3Sstephan          }catch(e){
6428ef9bddSstephan            // sqlite3_wasm_init_wasmfs() is not available
658fc8b5b3Sstephan            return f._ = "";
668fc8b5b3Sstephan          }
678fc8b5b3Sstephan        };
68b5ae85ecSstephan        wasmfsDir._ = undefined;
698fc8b5b3Sstephan
70100b496dSstephan        const eOut = document.querySelector('#test-output');
71dd628ed5Sstephan        const log2 = function(cssClass,...args){
72100b496dSstephan          const ln = document.createElement('div');
73100b496dSstephan          if(cssClass) ln.classList.add(cssClass);
74100b496dSstephan          ln.append(document.createTextNode(args.join(' ')));
75100b496dSstephan          eOut.append(ln);
76100b496dSstephan          //this.e.output.lastElementChild.scrollIntoViewIfNeeded();
77100b496dSstephan        };
78e66b2681Sstephan        const logList = [];
79e66b2681Sstephan        const dumpLogList = function(){
80e66b2681Sstephan          logList.forEach((v)=>log2('',v));
81e66b2681Sstephan          logList.length = 0;
82e66b2681Sstephan        };
83100b496dSstephan        /* can't update DOM while speedtest is running unless we run
84100b496dSstephan           speedtest in a worker thread. */;
858fc8b5b3Sstephan        const log = (...args)=>{
86100b496dSstephan          console.log(...args);
87e66b2681Sstephan          logList.push(args.join(' '));
88100b496dSstephan        };
89100b496dSstephan        const logErr = function(...args){
90100b496dSstephan          console.error(...args);
91e66b2681Sstephan          logList.push('ERROR: '+args.join(' '));
92100b496dSstephan        };
93100b496dSstephan
94b5ae85ecSstephan        const runTests = function(sqlite3){
95b5ae85ecSstephan          const capi = sqlite3.capi, wasm = capi.wasm;
96b5ae85ecSstephan          //console.debug('sqlite3 =',sqlite3);
978fc8b5b3Sstephan          const unlink = wasm.xWrap("sqlite3_wasm_vfs_unlink", "int", ["string"]);
98b5ae85ecSstephan          const pDir = wasmfsDir(wasm);
998fc8b5b3Sstephan          if(pDir){
1008fc8b5b3Sstephan            console.warn("Persistent storage:",pDir);
1018fc8b5b3Sstephan          }
102100b496dSstephan          const scope = wasm.scopedAllocPush();
103b5ae85ecSstephan          let dbFile = pDir+"/speedtest1.db";
104b5ae85ecSstephan          const urlParams = self.SqliteTestUtil.processUrlArgs();
105e66b2681Sstephan          const argv = ["speedtest1"];
106b5ae85ecSstephan          if('string'===typeof urlParams.vfs){
107b5ae85ecSstephan              if(!capi.sqlite3_vfs_find(urlParams.vfs)){
108b5ae85ecSstephan                  log2('error',"Unknown VFS:",urlParams.vfs);
109b5ae85ecSstephan                  return;
110b5ae85ecSstephan              }
111b5ae85ecSstephan              argv.push("--vfs", urlParams.vfs);
112b5ae85ecSstephan              log2('',"Using VFS:",urlParams.vfs);
113b5ae85ecSstephan              if('kvvfs' === urlParams.vfs){
114fa5aac74Sstephan                  urlParams.size = 2;
115b5ae85ecSstephan                  dbFile = 'session';
116fa5aac74Sstephan                  log2('warning',"kvvfs VFS: forcing --size",urlParams.size,
117fa5aac74Sstephan                       "and filename '"+dbFile+"'.");
118b5ae85ecSstephan                  capi.sqlite3_web_kvvfs_clear('session');
119b5ae85ecSstephan              }
120b5ae85ecSstephan          }
121b5ae85ecSstephan          [
122b5ae85ecSstephan              'size'
123b5ae85ecSstephan          ].forEach(function(k){
124b5ae85ecSstephan              const v = urlParams[k];
125b5ae85ecSstephan              if(v) argv.push('--'+k, urlParams[k]);
126b5ae85ecSstephan          });
127b5ae85ecSstephan          if(urlParams.flags){
128*0e0687ccSstephan            argv.push(...(urlParams.flags.split(',')));
129e66b2681Sstephan          }else{
130e66b2681Sstephan            argv.push(
1318fc8b5b3Sstephan              "--singlethread",
132fa5aac74Sstephan              //"--nomutex",
133fa5aac74Sstephan              //"--nosync",
134e66b2681Sstephan              "--nomemstat"
135e66b2681Sstephan            );
136dd628ed5Sstephan            //"--memdb", // note that memdb trumps the filename arg
13728ef9bddSstephan          }
138e66b2681Sstephan          argv.push("--big-transactions"/*important for tests 410 and 510!*/,
139e66b2681Sstephan                    dbFile);
1408fc8b5b3Sstephan          console.log("argv =",argv);
141dd628ed5Sstephan          // These log messages are not emitted to the UI until after main() returns. Fixing that
142dd628ed5Sstephan          // requires moving the main() call and related cleanup into a timeout handler.
143e66b2681Sstephan          if(pDir) unlink(dbFile);
144e66b2681Sstephan          log2('',"Starting native app:\n ",argv.join(' '));
145e66b2681Sstephan          log2('',"This will take a while and the browser might warn about the runaway JS.",
146e66b2681Sstephan               "Give it time...");
147e66b2681Sstephan          logList.length = 0;
148dd628ed5Sstephan          setTimeout(function(){
149b5ae85ecSstephan              wasm.xCall('wasm_main', argv.length,
1508fc8b5b3Sstephan                         wasm.scopedAllocMainArgv(argv));
151100b496dSstephan              wasm.scopedAllocPop(scope);
1528fc8b5b3Sstephan              if(pDir) unlink(dbFile);
153e66b2681Sstephan              logList.unshift("Done running native main(). Output:");
154e66b2681Sstephan              dumpLogList();
155e66b2681Sstephan          }, 50);
156dd628ed5Sstephan        }/*runTests()*/;
157100b496dSstephan
1588fc8b5b3Sstephan        self.sqlite3TestModule.print = log;
159100b496dSstephan        self.sqlite3TestModule.printErr = logErr;
160b5ae85ecSstephan        sqlite3Speedtest1InitModule(self.sqlite3TestModule)
161b5ae85ecSstephan        .then((EmscriptenModule)=>{
162b5ae85ecSstephan            return EmscriptenModule.sqlite3.installOpfsVfs()
163b5ae85ecSstephan                .catch((e)=>{console.warn(e.message)})
164b5ae85ecSstephan                .then(()=>runTests(EmscriptenModule.sqlite3));
165100b496dSstephan        });
166100b496dSstephan      })();
167100b496dSstephan    </script>
168100b496dSstephan  </body>
169100b496dSstephan</html>
170