diff --git a/README.md b/README.md index 6e6ac6f..86bd708 100644 --- a/README.md +++ b/README.md @@ -9,5 +9,5 @@ ### Client -1. Make sure you have the `wasm32-unknown-unknown` target installed, `wasm-bindgen` executable installed, and `bash`, `python3` packages (`python3` is used for `http.server` module) -2. Run `bash build.sh` to build and start a webserver +1. Make sure you have the `wasm32-unknown-unknown` target installed, `wasm-bindgen` executable installed (and optionally `wasm-opt`), and `bash`, `python3` packages (`python3` is used for `http.server` module) +2. Run `bash build.sh` to build without wasm-opt and start a webserver, add any argument to call wasm-opt diff --git a/client/build.sh b/client/build.sh index e1b1ff4..8c7ca0e 100755 --- a/client/build.sh +++ b/client/build.sh @@ -6,5 +6,9 @@ rm -rf out/ || true mkdir out/ cargo build --target wasm32-unknown-unknown --release wasm-bindgen --weak-refs --no-typescript --target no-modules --out-dir out/ ../target/wasm32-unknown-unknown/release/wstcp_client.wasm +if [[ "$#" > 0 ]]; then + mv out/wstcp_client_bg.wasm out/wstcp_client_unoptimized.wasm + wasm-opt -O4 out/wstcp_client_unoptimized.wasm -o out/wstcp_client_bg.wasm +fi cp -r src/web/* out/ (cd out; python3 -m http.server) diff --git a/client/src/web/index.js b/client/src/web/index.js index 2380ca9..0468036 100644 --- a/client/src/web/index.js +++ b/client/src/web/index.js @@ -4,17 +4,43 @@ "color:red;font-size:2rem;font-weight:bold" ); await wasm_bindgen("./wstcp_client_bg.wasm"); + const tconn0 = performance.now(); // args: websocket url, user agent, redirect limit let wstcp = await new wasm_bindgen.WsTcp("wss://localhost:4000", navigator.userAgent, 10); const tconn1 = performance.now(); console.warn(`conn establish took ${tconn1 - tconn0} ms or ${(tconn1 - tconn0) / 1000} s`); - const t0 = performance.now(); - let resp = await wstcp.fetch("http://httpbin.org/redirect/11"); - const t1 = performance.now(); - console.warn(resp); - console.warn(Object.fromEntries(resp.headers)); - console.warn(await resp.text()); - console.warn(`mux 1 took ${t1 - t0} ms or ${(t1 - t0) / 1000} s`); + const test_mux = async (url) => { + const t0 = performance.now(); + await wstcp.fetch(url); + const t1 = performance.now(); + return t1 - t0; + }; + + const test_native = async (url) => { + const t0 = performance.now(); + await fetch(url); + const t1 = performance.now(); + return t1 - t0; + }; + + const num_tests = 10; + + let total_mux = 0; + for (const _ of Array(num_tests).keys()) { + 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()) { + 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`); + alert("you can open console now"); })();