From e00d33479a4f5f3d6157c3751a354b12a042a2d3 Mon Sep 17 00:00:00 2001 From: Balearica Date: Sun, 27 Aug 2023 20:56:13 -0700 Subject: [PATCH] Edited browser benchmark to report memory use per #813 --- benchmarks/browser/speed-benchmark.html | 19 ++++++++++++++++++- scripts/server.js | 10 +++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/benchmarks/browser/speed-benchmark.html b/benchmarks/browser/speed-benchmark.html index b34c74458..93b62fe73 100644 --- a/benchmarks/browser/speed-benchmark.html +++ b/benchmarks/browser/speed-benchmark.html @@ -16,7 +16,18 @@ const worker = await createWorker(); await worker.loadLanguage('eng'); await worker.initialize('eng'); - + + // The performance.measureUserAgentSpecificMemory function only runs under specific circumstances for security reasons. + // See: https://developer.mozilla.org/en-US/docs/Web/API/Performance/measureUserAgentSpecificMemory#security_requirements + // Launching a server using `npm start` and accessing via localhost on the same system should meet these conditions. + const debugMemory = true; + if (debugMemory && crossOriginIsolated) { + console.log("Memory utilization after initialization:"); + console.log(await performance.measureUserAgentSpecificMemory()); + } else { + console.log("Unable to run `performance.measureUserAgentSpecificMemory`: not crossOriginIsolated.") + } + const fileArr = ["../data/meditations.jpg", "../data/tyger.jpg", "../data/testocr.png"]; let timeTotal = 0; for (let file of fileArr) { @@ -29,6 +40,12 @@ timeTotal += timeDif; document.getElementById('message').innerHTML += "\n" + file + " [x10] runtime: " + timeDif + "s"; } + + if (debugMemory && crossOriginIsolated) { + console.log("Memory utilization after recognition:"); + console.log(await performance.measureUserAgentSpecificMemory()); + } + document.getElementById('message').innerHTML += "\nTotal runtime: " + timeTotal + "s"; })(); diff --git a/scripts/server.js b/scripts/server.js index 95e2d5494..8e857f5f8 100644 --- a/scripts/server.js +++ b/scripts/server.js @@ -9,9 +9,17 @@ const compiler = webpack(webpackConfig); const app = express(); app.use(cors()); -app.use('/', express.static(path.resolve(__dirname, '..'))); app.use(middleware(compiler, { publicPath: '/dist', writeToDisk: true })); +// These headers are required to measure memory within the benchmark code. +// If they are problematic within other contexts they can be removed. +app.use(express.static(path.resolve(__dirname, '..'), { + setHeaders: (res) => { + res.set('Cross-Origin-Opener-Policy', 'same-origin'); + res.set('Cross-Origin-Embedder-Policy', 'require-corp'); + } +})); + module.exports = app.listen(3000, () => { console.log('Server is running on the port no. 3000'); });