|
|
@ -9,43 +9,51 @@ use tungstenite::{Message, WebSocket}; |
|
|
|
|
|
|
|
|
|
|
|
const MOCK_WRITE_LEN: usize = 8 * 1024 * 1024; |
|
|
|
const MOCK_WRITE_LEN: usize = 8 * 1024 * 1024; |
|
|
|
|
|
|
|
|
|
|
|
/// `Write` impl that simulates fast writes and slow flushes.
|
|
|
|
/// `Write` impl that simulates slowish writes and slow flushes.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// Buffers up to 8 MiB fast on `write`. Each `flush` takes ~100ns.
|
|
|
|
/// Each `write` can buffer up to 8 MiB before flushing but takes an additional **~80ns**
|
|
|
|
struct MockSlowFlushWrite(Vec<u8>); |
|
|
|
/// to simulate stuff going on in the underlying stream.
|
|
|
|
|
|
|
|
/// Each `flush` takes **~8µs** to simulate flush io.
|
|
|
|
|
|
|
|
struct MockWrite(Vec<u8>); |
|
|
|
|
|
|
|
|
|
|
|
impl Read for MockSlowFlushWrite { |
|
|
|
impl Read for MockWrite { |
|
|
|
fn read(&mut self, _: &mut [u8]) -> io::Result<usize> { |
|
|
|
fn read(&mut self, _: &mut [u8]) -> io::Result<usize> { |
|
|
|
Err(io::Error::new(io::ErrorKind::WouldBlock, "reads not supported")) |
|
|
|
Err(io::Error::new(io::ErrorKind::WouldBlock, "reads not supported")) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
impl Write for MockSlowFlushWrite { |
|
|
|
impl Write for MockWrite { |
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
|
|
|
if self.0.len() + buf.len() > MOCK_WRITE_LEN { |
|
|
|
if self.0.len() + buf.len() > MOCK_WRITE_LEN { |
|
|
|
self.flush()?; |
|
|
|
self.flush()?; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// simulate io
|
|
|
|
|
|
|
|
spin(Duration::from_nanos(80)); |
|
|
|
self.0.extend(buf); |
|
|
|
self.0.extend(buf); |
|
|
|
Ok(buf.len()) |
|
|
|
Ok(buf.len()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn flush(&mut self) -> io::Result<()> { |
|
|
|
fn flush(&mut self) -> io::Result<()> { |
|
|
|
if !self.0.is_empty() { |
|
|
|
if !self.0.is_empty() { |
|
|
|
// simulate 100ns io
|
|
|
|
// simulate io
|
|
|
|
let a = Instant::now(); |
|
|
|
spin(Duration::from_micros(8)); |
|
|
|
while a.elapsed() < Duration::from_nanos(100) { |
|
|
|
|
|
|
|
hint::spin_loop(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
self.0.clear(); |
|
|
|
self.0.clear(); |
|
|
|
} |
|
|
|
} |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn spin(duration: Duration) { |
|
|
|
|
|
|
|
let a = Instant::now(); |
|
|
|
|
|
|
|
while a.elapsed() < duration { |
|
|
|
|
|
|
|
hint::spin_loop(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn benchmark(c: &mut Criterion) { |
|
|
|
fn benchmark(c: &mut Criterion) { |
|
|
|
// Writes 100k small json text messages then flushes
|
|
|
|
// Writes 100k small json text messages then flushes
|
|
|
|
c.bench_function("write 100k small texts then flush", |b| { |
|
|
|
c.bench_function("write 100k small texts then flush", |b| { |
|
|
|
let mut ws = WebSocket::from_raw_socket( |
|
|
|
let mut ws = WebSocket::from_raw_socket( |
|
|
|
MockSlowFlushWrite(Vec::with_capacity(MOCK_WRITE_LEN)), |
|
|
|
MockWrite(Vec::with_capacity(MOCK_WRITE_LEN)), |
|
|
|
tungstenite::protocol::Role::Server, |
|
|
|
tungstenite::protocol::Role::Server, |
|
|
|
None, |
|
|
|
None, |
|
|
|
); |
|
|
|
); |
|
|
|