make wasm smaller and update build/test system

This commit is contained in:
Toshit Chawda 2024-01-31 06:47:00 -08:00
parent c5cf95fcb1
commit 619a2a61c7
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
6 changed files with 49 additions and 22 deletions

9
.gitignore vendored
View file

@ -3,7 +3,10 @@ server/src/*.pem
client/pkg
client/out
.direnv
client/index.js
client/module.js
client/module.d.ts
client/epoxy-bundled.js
client/epoxy-module-bundled.js
client/epoxy-module-bundled.d.ts
client/epoxy.js
client/epoxy.d.ts
client/epoxy.wasm
pnpm-lock.yaml

View file

@ -4,3 +4,7 @@ members = ["server", "client", "wisp"]
[patch.crates-io]
rustls-pki-types = { git = "https://github.com/r58Playz/rustls-pki-types" }
[profile.release]
lto = true
opt-level = 'z'

View file

@ -11,21 +11,25 @@ wasm-bindgen --weak-refs --target no-modules --no-modules-global epoxy --out-dir
echo "[ws] bindgen finished"
mv out/epoxy_client_bg.wasm out/epoxy_client_unoptimized.wasm
time wasm-opt -O4 out/epoxy_client_unoptimized.wasm -o out/epoxy_client_bg.wasm
time wasm-opt -Oz out/epoxy_client_unoptimized.wasm -o out/epoxy_client_bg.wasm
echo "[ws] optimized"
AUTOGENERATED_SOURCE=$(<"out/epoxy_client.js")
WASM_BASE64=$(base64 -w0 out/epoxy_client_bg.wasm)
AUTOGENERATED_SOURCE=${AUTOGENERATED_SOURCE//__wbg_init(input) \{/__wbg_init() \{let input=\'data:application/wasm;base64,$WASM_BASE64\'}
AUTOGENERATED_SOURCE=${AUTOGENERATED_SOURCE//return __wbg_finalize_init\(instance\, module\);/__wbg_finalize_init\(instance\, module\); return epoxy}
echo "$AUTOGENERATED_SOURCE" > index.js
cp index.js module.js
echo "module.exports = epoxy" >> module.js
echo "$AUTOGENERATED_SOURCE" > epoxy-bundled.js
cp epoxy-bundled.js epoxy-module-bundled.js
echo "module.exports = epoxy" >> epoxy-module-bundled.js
AUTOGENERATED_TYPEDEFS=$(<"out/epoxy_client.d.ts")
AUTOGENERATED_TYPEDEFS=${AUTOGENERATED_TYPEDEFS%%export class IntoUnderlyingByteSource*}
echo "$AUTOGENERATED_TYPEDEFS" >"module.d.ts"
echo "} export default function epoxy(): Promise<typeof wasm_bindgen>;" >> "module.d.ts"
echo "$AUTOGENERATED_TYPEDEFS" >"epoxy-module-bundled.d.ts"
echo "} export default function epoxy(): Promise<typeof wasm_bindgen>;" >> "epoxy-module-bundled.d.ts"
cp out/epoxy_client.js epoxy.js
cp out/epoxy_client.d.ts epoxy.d.ts
cp out/epoxy_client_bg.wasm epoxy.wasm
rm -rf out/
echo "[ws] done!"

View file

@ -8,13 +8,20 @@
const should_perf_test = (new URL(window.location.href)).searchParams.has("perf_test");
const should_ws_test = (new URL(window.location.href)).searchParams.has("ws_test");
const log = (str) => {
let el = document.createElement("div");
el.innerText = str;
document.getElementById("logs").appendChild(el);
console.warn(str);
}
let { EpoxyClient } = await epoxy();
const tconn0 = performance.now();
// args: websocket url, user agent, redirect limit
let epoxy_client = await new EpoxyClient("wss://localhost:4000", navigator.userAgent, 10);
const tconn1 = performance.now();
console.warn(`conn establish took ${tconn1 - tconn0} ms or ${(tconn1 - tconn0) / 1000} s`);
log(`conn establish took ${tconn1 - tconn0} ms or ${(tconn1 - tconn0) / 1000} s`);
if (should_feature_test) {
@ -47,20 +54,22 @@
const num_tests = 10;
let total_mux = 0;
for (const _ of Array(num_tests).keys()) {
for (const i of Array(num_tests).keys()) {
log(`running mux test ${i}`);
total_mux += await test_mux("https://httpbin.org/get");
}
total_mux = total_mux / num_tests;
let total_native = 0;
for (const _ of Array(num_tests).keys()) {
for (const i of Array(num_tests).keys()) {
log(`running native test ${i}`);
total_native += await test_native("https://httpbin.org/get");
}
total_native = total_native / num_tests;
console.warn(`avg mux (10) took ${total_mux} ms or ${total_mux / 1000} s`);
console.warn(`avg native (10) took ${total_native} ms or ${total_native / 1000} s`);
console.warn(`mux - native: ${total_mux - total_native} ms or ${(total_mux - total_native) / 1000} s`);
log(`avg mux (10) took ${total_mux} ms or ${total_mux / 1000} s`);
log(`avg native (10) took ${total_native} ms or ${total_native / 1000} s`);
log(`mux - native: ${total_mux - total_native} ms or ${(total_mux - total_native) / 1000} s`);
} else if (should_ws_test) {
let ws = await epoxy_client.connect_ws(
() => console.log("opened"),
@ -80,5 +89,5 @@
console.warn(resp, Object.fromEntries(resp.headers));
console.warn(await resp.text());
}
if (!should_ws_test) alert("you can open console now");
log("done");
})();

View file

@ -2,12 +2,19 @@
<head>
<title>epoxy</title>
<script src="index.js"></script>
<script src="epoxy-bundled.js"></script>
<script src="demo.js"></script>
<style>
body { font-family: sans-serif }
#logs > * { font-family: monospace }
</style>
</head>
<body>
running... (wait for the browser alert if not running ws test)
<div>
running... (wait for the browser alert if not running ws test)
</div>
<div id="logs"></div>
</body>
</html>

View file

@ -16,8 +16,8 @@
"author": "MercuryWorkshop",
"repository": "https://github.com/MercuryWorkshop/epoxy-tls",
"license": "MIT",
"browser": "./client/module.js",
"module": "./client/module.js",
"main": "./client/module.js",
"types": "./client/module.d.ts"
"browser": "./client/epoxy-module-bundled.js",
"module": "./client/epoxy-module-bundled.js",
"main": "./client/epoxy-module-bundled.js",
"types": "./client/epoxy-module-bundled.d.ts"
}