-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Totodore/ft-examples
Examples + Refactoring
- Loading branch information
Showing
39 changed files
with
2,168 additions
and
735 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
use std::{sync::Arc, time::Duration}; | ||
use std::str::FromStr; | ||
|
||
use bytes::{Buf, Bytes}; | ||
use criterion::{black_box, criterion_group, criterion_main, Criterion}; | ||
use engineioxide::{handler::EngineIoHandler, service::EngineIoService, socket::Socket}; | ||
|
||
use http::Request; | ||
use http_body::{Empty, Full}; | ||
use serde::{Deserialize, Serialize}; | ||
use tower::Service; | ||
use engineioxide::sid_generator::Sid; | ||
|
||
/// An OpenPacket is used to initiate a connection | ||
#[derive(Debug, Serialize, Deserialize, PartialEq, PartialOrd)] | ||
#[serde(rename_all = "camelCase")] | ||
struct OpenPacket { | ||
sid: String, | ||
upgrades: Vec<String>, | ||
ping_interval: u64, | ||
ping_timeout: u64, | ||
max_payload: u64, | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Client; | ||
impl EngineIoHandler for Client { | ||
type Data = (); | ||
|
||
fn on_connect(&self, _: &Socket<Self>) {} | ||
|
||
fn on_disconnect(&self, _: &Socket<Self>) {} | ||
|
||
fn on_message(&self, msg: String, socket: &Socket<Self>) { | ||
socket.emit(msg).unwrap(); | ||
} | ||
|
||
fn on_binary(&self, data: Vec<u8>, socket: &Socket<Self>) { | ||
socket.emit_binary(data).unwrap(); | ||
} | ||
} | ||
|
||
fn create_open_poll_req() -> Request<http_body::Empty<Bytes>> { | ||
http::Request::builder() | ||
.method(http::Method::GET) | ||
.uri("http://localhost:3000/engine.io/?EIO=4&transport=polling&t=NQ") | ||
.body(Empty::new()) | ||
.unwrap() | ||
} | ||
|
||
async fn prepare_ping_pong(mut svc: EngineIoService<Client>) -> Sid { | ||
let mut res = svc.call(create_open_poll_req()).await.unwrap(); | ||
let body = hyper::body::aggregate(res.body_mut()).await.unwrap(); | ||
let body: String = String::from_utf8(body.chunk().to_vec()) | ||
.unwrap() | ||
.chars() | ||
.skip(1) | ||
.collect(); | ||
let open_packet: OpenPacket = serde_json::from_str(&body).unwrap(); | ||
let sid = open_packet.sid; | ||
Sid::from_str(&sid).unwrap() | ||
} | ||
fn create_poll_req(sid: Sid) -> Request<http_body::Empty<Bytes>> { | ||
http::Request::builder() | ||
.method(http::Method::GET) | ||
.uri(format!( | ||
"http://localhost:3000/engine.io/?EIO=4&transport=polling&t=NQ&sid={sid}" | ||
)) | ||
.body(Empty::new()) | ||
.unwrap() | ||
} | ||
fn create_post_req(sid: Sid) -> Request<http_body::Full<Bytes>> { | ||
http::Request::builder() | ||
.method(http::Method::POST) | ||
.uri(format!( | ||
"http://localhost:3000/engine.io/?EIO=4&transport=polling&t=NQ&sid={sid}" | ||
)) | ||
.body(Full::new("4abcabc".to_owned().into())) | ||
.unwrap() | ||
} | ||
fn create_post_bin_req(sid: Sid) -> Request<http_body::Full<Bytes>> { | ||
http::Request::builder() | ||
.method(http::Method::POST) | ||
.uri(format!( | ||
"http://localhost:3000/engine.io/?EIO=4&transport=polling&t=NQ&sid={sid}" | ||
)) | ||
.body(Full::new("bYWJjYmFj".to_owned().into())) | ||
.unwrap() | ||
} | ||
pub fn criterion_benchmark(c: &mut Criterion) { | ||
c.bench_function("polling open request", |b| { | ||
let client = Arc::new(Client); | ||
let svc = EngineIoService::new(client); | ||
b.to_async(tokio::runtime::Runtime::new().unwrap()) | ||
.iter_batched( | ||
create_open_poll_req, | ||
|req| svc.clone().call(black_box(req)), | ||
criterion::BatchSize::SmallInput, | ||
); | ||
}); | ||
|
||
c.bench_function("ping/pong text", |b| { | ||
let client = Arc::new(Client); | ||
let svc = EngineIoService::new(client); | ||
b.to_async(tokio::runtime::Runtime::new().unwrap()) | ||
.iter_custom(|r| { | ||
let svc = svc.clone(); | ||
async move { | ||
futures::future::join_all((0..r).map(|_| async { | ||
let sid = prepare_ping_pong(svc.clone()).await; | ||
let poll_req = create_poll_req(sid); | ||
let post_req = create_post_req(sid); | ||
let start = std::time::Instant::now(); | ||
let (a, b) = futures::future::join( | ||
svc.clone().call(black_box(poll_req)), | ||
svc.clone().call(black_box(post_req)), | ||
) | ||
.await; | ||
a.unwrap(); | ||
b.unwrap(); | ||
start.elapsed() | ||
})) | ||
.await | ||
.iter() | ||
.sum::<Duration>() | ||
/ r as u32 | ||
} | ||
}); | ||
}); | ||
|
||
c.bench_function("ping/pong binary", |b| { | ||
let client = Arc::new(Client); | ||
let svc = EngineIoService::new(client); | ||
b.to_async(tokio::runtime::Runtime::new().unwrap()) | ||
.iter_custom(|r| { | ||
let svc = svc.clone(); | ||
async move { | ||
futures::future::join_all((0..r).map(|_| async { | ||
let sid = prepare_ping_pong(svc.clone()).await; | ||
let poll_req = create_poll_req(sid); | ||
let post_req = create_post_bin_req(sid); | ||
let start = std::time::Instant::now(); | ||
let (a, b) = futures::future::join( | ||
svc.clone().call(black_box(poll_req)), | ||
svc.clone().call(black_box(post_req)), | ||
) | ||
.await; | ||
a.unwrap(); | ||
b.unwrap(); | ||
start.elapsed() | ||
})) | ||
.await | ||
.iter() | ||
.sum::<Duration>() | ||
/ r as u32 | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.