snapview / tokio-tungstenite

Future-based Tungstenite for Tokio. Lightweight stream-based WebSocket implementation
MIT License
1.88k stars 236 forks source link

Vectorized send #325

Open AmineDiro opened 7 months ago

AmineDiro commented 7 months ago

Hello,

Thanks a lot for the work you've done in this crate!

I currently use tokio-tungstenite to write a high-performance client for benchmarking multiple Websocket libraries. The client-side code connects ~10_000 ws connections to the server and writes a batch of messages. When profiling the client code, I found the repeated call to __libc_sendto syscall which adds a lot of overhead given the test setup. For performance reasons, I wanted to write the batch of messages using a single writev syscall.

Looking at the code I found that the WebsocketStream implements Sink for an underlying Stream : AsyncRead + AsyncWrite

impl<T> Sink<Message> for WebSocketStream<T>
where
    T: AsyncRead + AsyncWrite + Unpin,

Is there an easy way to access the inner stream to call write_vectored ? Thanks for your help

daniel-abramov commented 7 months ago

Hi,

thanks for showing interest in benchmarking tokio-tungstenite Note that denoland/fastwebsockets have implemented an interesting benchmark and have already tested tokio-tungstenite.

As for your question - you are touching on a topic that is a known problem in tokio-tungstenite. Unfortunately, we can't add such support easily due to a major constrain of our underlying library tungstenite operating on Read + Write streams, in other words, the actual reading/writing happens inside tungstenite and we have a kludge in tokio-tungstenite to implement Read + Write for AsyncRead + AsyncWrite types that don't implement Read + Write (the requirement from tungstenite).

I've summarized most of the information related to performance here: https://github.com/snapview/tungstenite-rs/issues/352#issuecomment-1537488614. We have not addressed these issues yet as no one of the current maintainers actively uses the library, so it has stalled a bit.

AmineDiro commented 7 months ago

Hi, thanks for the very detailed response! I'll take a look at the links 🙂