From d0535cf10b6e4205579154754905ba33a0551597 Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Wed, 25 Sep 2024 21:45:03 -0400 Subject: [PATCH] dev: add wasm bench --- src/emulator/benchmark/bench.wasm | Bin 0 -> 60 bytes src/emulator/benchmark/bench.wat | 10 ++++++++++ src/emulator/package.json | 3 ++- src/emulator/src/main.js | 26 ++++++++++++++++++++++++++ src/emulator/webpack.config.js | 6 ++++++ 5 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/emulator/benchmark/bench.wasm create mode 100644 src/emulator/benchmark/bench.wat diff --git a/src/emulator/benchmark/bench.wasm b/src/emulator/benchmark/bench.wasm new file mode 100644 index 0000000000000000000000000000000000000000..f9fb504a04589ba8aba67db69edbb52cc3e868cf GIT binary patch literal 60 zcmZQbEY4+QU|?WmVN76PU}j=uVCQAzOiIm5&d5zH%4T5Tl4KNTWUOa)P+)Lm%u-|k OQWMTGxbZS@a{~Yo(+P|K literal 0 HcmV?d00001 diff --git a/src/emulator/benchmark/bench.wat b/src/emulator/benchmark/bench.wat new file mode 100644 index 00000000..9148976c --- /dev/null +++ b/src/emulator/benchmark/bench.wat @@ -0,0 +1,10 @@ +(module + (func $benchmark (export "benchmark") + (local $i i32) + (loop $loop + (local.set $i (i32.add (local.get $i) (i32.const 1))) + (i32.eq (local.get $i) (i32.const 10000)) + br_if $loop + ) + ) +) diff --git a/src/emulator/package.json b/src/emulator/package.json index 864649da..250c9763 100644 --- a/src/emulator/package.json +++ b/src/emulator/package.json @@ -14,6 +14,7 @@ "html-webpack-plugin": "^5.6.0" }, "dependencies": { - "brotli-dec-wasm": "^2.3.0" + "brotli-dec-wasm": "^2.3.0", + "copy-webpack-plugin": "^12.0.2" } } diff --git a/src/emulator/src/main.js b/src/emulator/src/main.js index 930eb00b..bbdbd86a 100644 --- a/src/emulator/src/main.js +++ b/src/emulator/src/main.js @@ -114,8 +114,34 @@ puter.ui.on('connection', event => { conn.on('message', pty_on_first_message); }); +const bench = async ({ modules }) => { + const { benchmark } = modules.bench; + const ts_start = performance.now(); + benchmark(); + const ts_end = performance.now(); + // console.log('benchmark took', ts_end - ts_start); + return ts_end - ts_start; +} + +const bench_20ms = async (ctx) => { + let ts = 0, count = 0; + for (;;) { + ts += await bench(ctx); + count++; + if ( ts > 20 ) { + return count; + } + } +} + window.onload = async function() { + const modules = {}; + modules.bench = (await WebAssembly.instantiateStreaming( + fetch('./static/bench.wasm'))).instance.exports; + + const res = await bench_20ms({ modules }); + console.log('result', res); let emu_config; try { emu_config = await puter.fs.read('config.json'); } catch (e) {} diff --git a/src/emulator/webpack.config.js b/src/emulator/webpack.config.js index 6dee8961..feeb4b63 100644 --- a/src/emulator/webpack.config.js +++ b/src/emulator/webpack.config.js @@ -1,5 +1,6 @@ const HtmlWebpackPlugin = require('html-webpack-plugin'); const DefinePlugin = require('webpack').DefinePlugin; +const CopyPlugin = require('copy-webpack-plugin'); module.exports = { entry: [ @@ -12,5 +13,10 @@ module.exports = { new DefinePlugin({ MODE: JSON.stringify(process.env.MODE ?? 'dev') }), + new CopyPlugin({ + patterns: [ + { from: 'benchmark', to: 'static' } + ] + }) ] };