Skip to content

Commit 3b5ac20

Browse files
themighty1heeckhau
andauthored
fix(benches): browser bench fixes (#821)
* fix(benches): make browser benches work again * Update crates/benches/binary/README.md Co-authored-by: Hendrik Eeckhaut <[email protected]> * Update crates/benches/browser/wasm/Cargo.toml Co-authored-by: Hendrik Eeckhaut <[email protected]> * add --release flag --------- Co-authored-by: Hendrik Eeckhaut <[email protected]>
1 parent a063f8c commit 3b5ac20

File tree

10 files changed

+72
-50
lines changed

10 files changed

+72
-50
lines changed

crates/benches/binary/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ With a Chrome browser installed on your system, make sure you're in the `crates/
3838
directory, build the wasm module, build the binaries, and then run the script:
3939
```sh
4040
cd browser/wasm
41-
rustup run nightly wasm-pack build --release --target web
41+
wasm-pack build --release --target web
4242
cd ../../binary
4343
cargo build --release --features browser-bench
4444
sudo ./bench.sh

crates/benches/binary/bench.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ upload-delay = 25
3838
download = 250
3939
download-delay = 25
4040
upload-size = 1024
41-
# Setting download-size higher than 45000 will cause a `Maximum call stack size exceeded`
42-
# error in the browser.
43-
download-size = [1024, 4096, 16384, 45000]
41+
# It was observed that setting download-size > 30K causes browser errors that need to
42+
# be investigated.
43+
download-size = [1024, 4096, 16384]
4444
defer-decryption = true
4545
memory-profile = true

crates/benches/binary/benches.Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ COPY . .
55
ARG BENCH_TYPE=native
66

77
RUN \
8+
rustup update; \
89
if [ "$BENCH_TYPE" = "browser" ]; then \
910
# ring's build script needs clang.
1011
apt update && apt install -y clang; \
1112
rustup install nightly; \
1213
rustup component add rust-src --toolchain nightly; \
1314
cargo install wasm-pack; \
1415
cd crates/benches/browser/wasm; \
15-
rustup run nightly wasm-pack build --release --target web; \
16+
wasm-pack build --release --target web; \
1617
cd ../../binary; \
1718
cargo build --release --features browser-bench; \
1819
else \

crates/benches/browser/native/src/lib.rs

+21-16
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@
44
//! components. The native component is responsible for starting the browser,
55
//! loading the wasm component and driving it.
66
7-
use std::{env, net::IpAddr};
8-
9-
use serio::{stream::IoStreamExt, SinkExt as _};
10-
use tlsn_benches_browser_core::{
11-
msg::{Config, Runtime},
12-
FramedIo,
13-
};
14-
use tlsn_benches_library::{AsyncIo, ProverKind, ProverTrait};
7+
use std::{env, net::IpAddr, time::Duration};
158

169
use anyhow::{anyhow, Context, Result};
1710
use async_trait::async_trait;
@@ -24,10 +17,17 @@ use chromiumoxide::{
2417
};
2518
use futures::{Future, FutureExt, StreamExt};
2619
use rust_embed::RustEmbed;
20+
use serio::{stream::IoStreamExt, SinkExt as _};
2721
use tokio::{io, io::AsyncWriteExt, net::TcpListener, task::JoinHandle};
2822
use tracing::{debug, error, info};
2923
use warp::Filter;
3024

25+
use tlsn_benches_browser_core::{
26+
msg::{Config, Runtime},
27+
FramedIo,
28+
};
29+
use tlsn_benches_library::{AsyncIo, ProverKind, ProverTrait};
30+
3131
/// The IP on which the wasm component is served.
3232
pub static DEFAULT_WASM_IP: &str = "127.0.0.1";
3333
/// The IP of the websocket relay.
@@ -100,6 +100,12 @@ impl ProverTrait for BrowserProver {
100100

101101
relays.push(spawn_websocket_relay(ws_ip, ws_port).await?);
102102

103+
// Create a framed connection to the wasm component.
104+
let (wasm_left, wasm_right) = tokio::io::duplex(1 << 16);
105+
106+
relays.push(spawn_port_relay(wasm_to_native_port, Box::new(wasm_right)).await?);
107+
let mut wasm_io = FramedIo::new(Box::new(wasm_left));
108+
103109
let http_server = spawn_http_server(wasm_ip, wasm_port)?;
104110

105111
// Relay data from the wasm component to the server.
@@ -108,12 +114,6 @@ impl ProverTrait for BrowserProver {
108114
// Relay data from the wasm component to the verifier.
109115
relays.push(spawn_port_relay(wasm_to_verifier_port, verifier_io).await?);
110116

111-
// Create a framed connection to the wasm component.
112-
let (wasm_left, wasm_right) = tokio::io::duplex(1 << 16);
113-
114-
relays.push(spawn_port_relay(wasm_to_native_port, Box::new(wasm_right)).await?);
115-
let mut wasm_io = FramedIo::new(Box::new(wasm_left));
116-
117117
info!("spawning browser");
118118

119119
// Note that the browser must be spawned only when the WebSocket relay is
@@ -129,6 +129,10 @@ impl ProverTrait for BrowserProver {
129129
)
130130
.await?;
131131

132+
// Without this sleep, it was observed that `wasm_io.send(Config)`
133+
// msg does not reach the browser component.
134+
tokio::time::sleep(Duration::from_secs(2)).await;
135+
132136
info!("sending config to the browser component");
133137

134138
wasm_io
@@ -267,14 +271,15 @@ async fn spawn_browser(
267271
tokio::spawn(register_listeners(&page).await?);
268272

269273
page.wait_for_navigation().await?;
274+
270275
// Note that `format!` needs double {{ }} in order to escape them.
271276
let _ = page
272277
.evaluate_function(&format!(
273278
r#"
274279
async function() {{
275-
await window.worker.init();
280+
await window.benchWorker.init();
276281
// Do not `await` run() or else it will block the browser.
277-
window.worker.run("{}", {}, {}, {}, {});
282+
window.benchWorker.run("{}", {}, {}, {}, {});
278283
}}
279284
"#,
280285
ws_ip, ws_port, wasm_to_server_port, wasm_to_verifier_port, wasm_to_native_port

crates/benches/browser/wasm/.cargo/config.toml

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
[build]
22
target = "wasm32-unknown-unknown"
33

4+
[unstable]
5+
build-std = ["panic_abort", "std"]
6+
47
[target.wasm32-unknown-unknown]
58
rustflags = [
69
"-C",
710
"target-feature=+atomics,+bulk-memory,+mutable-globals",
11+
"-C",
12+
# 4GB
13+
"link-arg=--max-memory=4294967296",
14+
"--cfg",
15+
'getrandom_backend="wasm_js"',
816
]
9-
10-
[unstable]
11-
build-std = ["panic_abort", "std"]

crates/benches/browser/wasm/Cargo.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ tlsn-wasm = { path = "../../../wasm" }
1818
serio = { workspace = true }
1919

2020
anyhow = { workspace = true }
21+
rayon = { workspace = true }
2122
tracing = { workspace = true }
22-
wasm-bindgen = { version = "0.2.87" }
23-
wasm-bindgen-futures = { version = "0.4.37" }
23+
wasm-bindgen = { version = "0.2" }
24+
wasm-bindgen-futures = { version = "0.4" }
25+
web-spawn = { workspace = true, features = ["no-bundler"] }
2426
web-time = { workspace = true }
2527
# Use the patched ws_stream_wasm to fix the issue https://github.com/najamelan/ws_stream_wasm/issues/12#issuecomment-1711902958
2628
ws_stream_wasm = { version = "0.7.4", git = "https://github.com/tlsnotary/ws_stream_wasm", rev = "2ed12aad9f0236e5321f577672f309920b2aef51", features = [
+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import * as Comlink from "./comlink.mjs";
22

3-
async function init() {
4-
const worker = Comlink.wrap(new Worker("worker.js", { type: "module" }));
5-
window.worker = worker;
6-
}
7-
init();
3+
const benchWorker = Comlink.wrap(new Worker("worker.js", { type: "module" }));
4+
5+
window.benchWorker = benchWorker;
+8-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import * as Comlink from "./comlink.mjs";
22

3-
import init, { wasm_main, initialize } from './tlsn_benches_browser_wasm.js';
3+
import init_wasm, * as wasm from './tlsn_benches_browser_wasm.js';
44

5-
class Worker {
5+
class BenchWorker {
66
async init() {
77
try {
8-
await init();
9-
// Tracing may interfere with the benchmark results. We should enable it only for debugging.
10-
// init_logging({
11-
// level: 'Debug',
12-
// crate_filters: undefined,
13-
// span_events: undefined,
14-
// });
15-
await initialize({ thread_count: navigator.hardwareConcurrency });
8+
await init_wasm();
9+
// Using Error level since excessive logging may interfere with the
10+
// benchmark results.
11+
await wasm.initialize_bench({ level: "Error" }, navigator.hardwareConcurrency);
1612
} catch (e) {
1713
console.error(e);
1814
throw e;
@@ -27,7 +23,7 @@ class Worker {
2723
wasm_to_native_port
2824
) {
2925
try {
30-
await wasm_main(
26+
await wasm.wasm_main(
3127
ws_ip,
3228
ws_port,
3329
wasm_to_server_port,
@@ -40,6 +36,6 @@ class Worker {
4036
}
4137
}
4238

43-
const worker = new Worker();
39+
const worker = new BenchWorker();
4440

4541
Comlink.expose(worker);
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
[toolchain]
2-
channel = "nightly"
2+
channel = "nightly"
3+
components = ["rust-src"]
4+
targets = ["wasm32-unknown-unknown"]

crates/benches/browser/wasm/src/lib.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55
//! Conceptually the browser prover consists of the native and the wasm
66
//! components.
77
8+
use anyhow::Result;
89
use serio::{stream::IoStreamExt, SinkExt as _};
10+
use tracing::info;
11+
use wasm_bindgen::prelude::*;
12+
use web_time::Instant;
13+
use ws_stream_wasm::WsMeta;
14+
915
use tlsn_benches_browser_core::{
1016
msg::{Config, Runtime},
1117
FramedIo,
1218
};
1319
use tlsn_benches_library::run_prover;
14-
15-
use anyhow::Result;
16-
use tracing::info;
17-
use wasm_bindgen::prelude::*;
18-
use web_time::Instant;
19-
use ws_stream_wasm::WsMeta;
20+
use tlsn_wasm::LoggingConfig;
2021

2122
#[wasm_bindgen]
2223
pub async fn wasm_main(
@@ -85,6 +86,9 @@ pub async fn main(
8586
let cfg: Config = native_io.expect_next().await?;
8687

8788
let start_time = Instant::now();
89+
90+
info!("running the prover");
91+
8892
run_prover(
8993
cfg.upload_size,
9094
cfg.download_size,
@@ -100,3 +104,12 @@ pub async fn main(
100104

101105
Ok(())
102106
}
107+
108+
/// Initializes the module.
109+
#[wasm_bindgen]
110+
pub async fn initialize_bench(
111+
logging_config: Option<LoggingConfig>,
112+
thread_count: usize,
113+
) -> Result<(), JsValue> {
114+
tlsn_wasm::initialize(logging_config, thread_count).await
115+
}

0 commit comments

Comments
 (0)