phip1611 / ttfb

Library + CLI-Tool to measure the TTFB (time to first byte) of HTTP requests. Additionally, this crate measures the times of DNS lookup, TCP connect and TLS handshake.
MIT License
42 stars 11 forks source link

webassembly #10

Open skaboy007 opened 1 year ago

skaboy007 commented 1 year ago

Can this work in the browser via web assembly?

phip1611 commented 1 year ago

Hi, thanks for the interest in my library.

I don't know much about wasm, tbh.

I tried the following:

$ rustup target add wasm32-wasi
$ cargo build --target wasm32-wasi

and

$ rustup target add wasm32-unknown-emscripten
$ cargo build --target wasm32-unknown-emscripten

In both cases, the build fails because of a dependency. As I know very little about wasm, I can't tell you more. Feel free to deep-dive and share the results

yannh commented 1 year ago

I'm interested in this as well. I suspect it might work quite differently in webassembly and would need to use the performance API underneath. Also not super familiar with this myself but maybe this is an opportunity to learn :sweat_smile:

yannh commented 1 year ago

Sooo - this is how getrandom() does it -> https://github.com/rust-random/getrandom/blob/abe7ef3107a4d178618e86e403c2ea200c42bf96/src/lib.rs#L217

Essentially - conditional include of the right module, depending on some macro. We could introduce hopefully the same library interface ttfb() but using Performance() API exposed by the web-sys crate https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.Performance.html , and try to reuse code as much as possible.

This... might take a bit of effort :D Is this code you would be willing to review? I might try to give this a shot but it is likely to be a fairly substantial PR (and I am fairly new at rust). Let me know if you're interested, otherwise I'll play in my fork :bow:

EDIT: Of course this would be different if the webassembly runs in the browser (wasm‑unknown , where web-sys would work) or WASI, for which I have no idea :/

EDIT: So for WASI i suspect this is what we could use in the future https://github.com/WebAssembly/wasi-sockets but I suspect this is not implemented much yet. My personal use-case is for wasm‑unknown so I might start with that...

phip1611 commented 1 year ago

I'm open for adding a wasm module that is used conditionally (as compile-time option). I'm very interested - I never used web-assembly but want to learn something new!

yannh commented 1 year ago

If you promise to not scream :sweat_smile: you can check the branch in my fork, it compiles to wasm with wasm-pack build --release --target web

Then start a server (eg. python3 -m http.server ) and then in theory the webpage calls the ttfb_js() function in webassembly, which is a wrapper for ttfb() that then uses the performance API to.. do stuff... and return some stuff back to javascript, mostly wrong at this state, but the javascript still captures the output.

Your code was quite nice before, now it's a bit... well it's going to take me a bit of time to get it back to your standards, but I want to get it working first :) If you're ever in Berlin ping me! Your library has been quite useful to me I owe you a :beer:

phip1611 commented 1 year ago

If you promise to not scream

Don't worry :)

If you're ever in Berlin ping me! Your library has been quite useful to me I owe you a beer

I'm in Berlin every few weeks, maybe I'll come back to your offer 😉 🍺

Could you open a PR? Then we can discuss further how to merge that functionality and how we structure the unit/integration test! - no hard deadline. It's okay if this takes a few weeks or months.