Skip to content

Commit d0535cf

Browse files
committed
dev: add wasm bench
1 parent fff8993 commit d0535cf

File tree

5 files changed

+44
-1
lines changed

5 files changed

+44
-1
lines changed

src/emulator/benchmark/bench.wasm

60 Bytes
Binary file not shown.

src/emulator/benchmark/bench.wat

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(module
2+
(func $benchmark (export "benchmark")
3+
(local $i i32)
4+
(loop $loop
5+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
6+
(i32.eq (local.get $i) (i32.const 10000))
7+
br_if $loop
8+
)
9+
)
10+
)

src/emulator/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"html-webpack-plugin": "^5.6.0"
1515
},
1616
"dependencies": {
17-
"brotli-dec-wasm": "^2.3.0"
17+
"brotli-dec-wasm": "^2.3.0",
18+
"copy-webpack-plugin": "^12.0.2"
1819
}
1920
}

src/emulator/src/main.js

+26
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,34 @@ puter.ui.on('connection', event => {
114114
conn.on('message', pty_on_first_message);
115115
});
116116

117+
const bench = async ({ modules }) => {
118+
const { benchmark } = modules.bench;
119+
const ts_start = performance.now();
120+
benchmark();
121+
const ts_end = performance.now();
122+
// console.log('benchmark took', ts_end - ts_start);
123+
return ts_end - ts_start;
124+
}
125+
126+
const bench_20ms = async (ctx) => {
127+
let ts = 0, count = 0;
128+
for (;;) {
129+
ts += await bench(ctx);
130+
count++;
131+
if ( ts > 20 ) {
132+
return count;
133+
}
134+
}
135+
}
136+
117137
window.onload = async function()
118138
{
139+
const modules = {};
140+
modules.bench = (await WebAssembly.instantiateStreaming(
141+
fetch('./static/bench.wasm'))).instance.exports;
142+
143+
const res = await bench_20ms({ modules });
144+
console.log('result', res);
119145
let emu_config; try {
120146
emu_config = await puter.fs.read('config.json');
121147
} catch (e) {}

src/emulator/webpack.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const HtmlWebpackPlugin = require('html-webpack-plugin');
22
const DefinePlugin = require('webpack').DefinePlugin;
3+
const CopyPlugin = require('copy-webpack-plugin');
34

45
module.exports = {
56
entry: [
@@ -12,5 +13,10 @@ module.exports = {
1213
new DefinePlugin({
1314
MODE: JSON.stringify(process.env.MODE ?? 'dev')
1415
}),
16+
new CopyPlugin({
17+
patterns: [
18+
{ from: 'benchmark', to: 'static' }
19+
]
20+
})
1521
]
1622
};

0 commit comments

Comments
 (0)