2020-07-22 05:32:13 +02:00
|
|
|
use actix_service::boxed::BoxFuture;
|
|
|
|
use actix_service::IntoService;
|
|
|
|
use actix_service::Service;
|
2020-02-26 08:45:23 +01:00
|
|
|
/// Benchmark various implementations of and_then
|
|
|
|
use criterion::{criterion_main, Criterion};
|
|
|
|
use futures_util::future::join_all;
|
2020-07-22 05:32:13 +02:00
|
|
|
use futures_util::future::TryFutureExt;
|
2020-02-26 08:45:23 +01:00
|
|
|
use std::future::Future;
|
|
|
|
use std::pin::Pin;
|
2020-07-22 05:32:13 +02:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::task::{Context, Poll};
|
2020-12-27 05:28:00 +01:00
|
|
|
use std::{
|
|
|
|
cell::{RefCell, UnsafeCell},
|
|
|
|
marker::PhantomData,
|
|
|
|
};
|
2020-02-26 08:45:23 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Test services A,B for AndThen service implementations
|
|
|
|
*/
|
|
|
|
|
|
|
|
async fn svc1(_: ()) -> Result<usize, ()> {
|
2020-07-22 05:32:13 +02:00
|
|
|
Ok(1)
|
2020-02-26 08:45:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn svc2(req: usize) -> Result<usize, ()> {
|
2020-07-22 05:32:13 +02:00
|
|
|
Ok(req + 1)
|
2020-02-26 08:45:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AndThenUC - original AndThen service based on UnsafeCell
|
|
|
|
* Cut down version of actix_service::AndThenService based on actix-service::Cell
|
|
|
|
*/
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
struct AndThenUC<A, Req, B>(Rc<UnsafeCell<(A, B)>>, PhantomData<Req>);
|
2020-02-26 08:45:23 +01:00
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, Req, B> AndThenUC<A, Req, B> {
|
2020-07-22 05:32:13 +02:00
|
|
|
fn new(a: A, b: B) -> Self
|
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: Service<Req>,
|
|
|
|
B: Service<A::Response, Error = A::Error>,
|
2020-07-22 05:32:13 +02:00
|
|
|
{
|
2020-12-27 05:28:00 +01:00
|
|
|
Self(Rc::new(UnsafeCell::new((a, b))), PhantomData)
|
2020-07-22 05:32:13 +02:00
|
|
|
}
|
2020-02-26 08:45:23 +01:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, Req, B> Clone for AndThenUC<A, Req, B> {
|
2020-07-22 05:32:13 +02:00
|
|
|
fn clone(&self) -> Self {
|
2020-12-27 05:28:00 +01:00
|
|
|
Self(self.0.clone(), PhantomData)
|
2020-07-22 05:32:13 +02:00
|
|
|
}
|
2020-02-26 08:45:23 +01:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, Req, B> Service<Req> for AndThenUC<A, Req, B>
|
2020-02-26 08:45:23 +01:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: Service<Req>,
|
|
|
|
B: Service<A::Response, Error = A::Error>,
|
2020-02-26 08:45:23 +01:00
|
|
|
{
|
2020-07-22 05:32:13 +02:00
|
|
|
type Response = B::Response;
|
|
|
|
type Error = A::Error;
|
2020-12-27 05:28:00 +01:00
|
|
|
type Future = AndThenServiceResponse<A, Req, B>;
|
2020-07-22 05:32:13 +02:00
|
|
|
|
|
|
|
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
fn call(&mut self, req: Req) -> Self::Future {
|
2020-07-22 05:32:13 +02:00
|
|
|
let fut = unsafe { &mut *(*self.0).get() }.0.call(req);
|
|
|
|
AndThenServiceResponse {
|
|
|
|
state: State::A(fut, Some(self.0.clone())),
|
2020-12-27 05:28:00 +01:00
|
|
|
_phantom: PhantomData,
|
2020-07-22 05:32:13 +02:00
|
|
|
}
|
|
|
|
}
|
2020-02-26 08:45:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[pin_project::pin_project]
|
2020-12-27 05:28:00 +01:00
|
|
|
pub(crate) struct AndThenServiceResponse<A, Req, B>
|
2020-02-26 08:45:23 +01:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: Service<Req>,
|
|
|
|
B: Service<A::Response, Error = A::Error>,
|
2020-02-26 08:45:23 +01:00
|
|
|
{
|
|
|
|
#[pin]
|
2020-12-27 05:28:00 +01:00
|
|
|
state: State<A, Req, B>,
|
|
|
|
_phantom: PhantomData<Req>,
|
2020-02-26 08:45:23 +01:00
|
|
|
}
|
|
|
|
|
2020-06-05 23:42:45 +02:00
|
|
|
#[pin_project::pin_project(project = StateProj)]
|
2020-12-27 05:28:00 +01:00
|
|
|
enum State<A, Req, B>
|
2020-02-26 08:45:23 +01:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: Service<Req>,
|
|
|
|
B: Service<A::Response, Error = A::Error>,
|
2020-02-26 08:45:23 +01:00
|
|
|
{
|
|
|
|
A(#[pin] A::Future, Option<Rc<UnsafeCell<(A, B)>>>),
|
|
|
|
B(#[pin] B::Future),
|
2020-12-27 05:28:00 +01:00
|
|
|
Empty(PhantomData<Req>),
|
2020-02-26 08:45:23 +01:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, Req, B> Future for AndThenServiceResponse<A, Req, B>
|
2020-02-26 08:45:23 +01:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: Service<Req>,
|
|
|
|
B: Service<A::Response, Error = A::Error>,
|
2020-02-26 08:45:23 +01:00
|
|
|
{
|
|
|
|
type Output = Result<B::Response, A::Error>;
|
|
|
|
|
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
let mut this = self.as_mut().project();
|
|
|
|
|
|
|
|
match this.state.as_mut().project() {
|
2020-06-05 23:42:45 +02:00
|
|
|
StateProj::A(fut, b) => match fut.poll(cx)? {
|
2020-02-26 08:45:23 +01:00
|
|
|
Poll::Ready(res) => {
|
|
|
|
let b = b.take().unwrap();
|
2020-12-27 05:28:00 +01:00
|
|
|
this.state.set(State::Empty(PhantomData)); // drop fut A
|
2020-02-26 08:45:23 +01:00
|
|
|
let fut = unsafe { &mut (*b.get()).1 }.call(res);
|
|
|
|
this.state.set(State::B(fut));
|
|
|
|
self.poll(cx)
|
|
|
|
}
|
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
},
|
2020-06-05 23:42:45 +02:00
|
|
|
StateProj::B(fut) => fut.poll(cx).map(|r| {
|
2020-12-27 05:28:00 +01:00
|
|
|
this.state.set(State::Empty(PhantomData));
|
2020-02-26 08:45:23 +01:00
|
|
|
r
|
|
|
|
}),
|
2020-12-27 05:28:00 +01:00
|
|
|
StateProj::Empty(_) => {
|
2020-07-22 05:32:13 +02:00
|
|
|
panic!("future must not be polled after it returned `Poll::Ready`")
|
|
|
|
}
|
2020-02-26 08:45:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AndThenRC - AndThen service based on RefCell
|
|
|
|
*/
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
struct AndThenRC<A, Req, B>(Rc<RefCell<(A, B)>>, PhantomData<Req>);
|
2020-07-22 05:32:13 +02:00
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, Req, B> AndThenRC<A, Req, B> {
|
2020-07-22 05:32:13 +02:00
|
|
|
fn new(a: A, b: B) -> Self
|
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: Service<Req>,
|
|
|
|
B: Service<A::Response, Error = A::Error>,
|
2020-07-22 05:32:13 +02:00
|
|
|
{
|
2020-12-27 05:28:00 +01:00
|
|
|
Self(Rc::new(RefCell::new((a, b))), PhantomData)
|
2020-07-22 05:32:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, Req, B> Clone for AndThenRC<A, Req, B> {
|
2020-07-22 05:32:13 +02:00
|
|
|
fn clone(&self) -> Self {
|
2020-12-27 05:28:00 +01:00
|
|
|
Self(self.0.clone(), PhantomData)
|
2020-07-22 05:32:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, Req, B> Service<Req> for AndThenRC<A, Req, B>
|
2020-07-22 05:32:13 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: Service<Req>,
|
|
|
|
B: Service<A::Response, Error = A::Error>,
|
2020-07-22 05:32:13 +02:00
|
|
|
{
|
|
|
|
type Response = B::Response;
|
|
|
|
type Error = A::Error;
|
2020-12-27 05:28:00 +01:00
|
|
|
type Future = AndThenServiceResponseRC<A, Req, B>;
|
2020-07-22 05:32:13 +02:00
|
|
|
|
|
|
|
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
fn call(&mut self, req: Req) -> Self::Future {
|
2020-07-22 05:32:13 +02:00
|
|
|
let fut = self.0.borrow_mut().0.call(req);
|
|
|
|
AndThenServiceResponseRC {
|
|
|
|
state: StateRC::A(fut, Some(self.0.clone())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pin_project::pin_project]
|
2020-12-27 05:28:00 +01:00
|
|
|
pub(crate) struct AndThenServiceResponseRC<A, Req, B>
|
2020-07-22 05:32:13 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: Service<Req>,
|
|
|
|
B: Service<A::Response, Error = A::Error>,
|
2020-07-22 05:32:13 +02:00
|
|
|
{
|
|
|
|
#[pin]
|
2020-12-27 05:28:00 +01:00
|
|
|
state: StateRC<A, Req, B>,
|
2020-07-22 05:32:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[pin_project::pin_project(project = StateRCProj)]
|
2020-12-27 05:28:00 +01:00
|
|
|
enum StateRC<A, Req, B>
|
2020-07-22 05:32:13 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: Service<Req>,
|
|
|
|
B: Service<A::Response, Error = A::Error>,
|
2020-07-22 05:32:13 +02:00
|
|
|
{
|
|
|
|
A(#[pin] A::Future, Option<Rc<RefCell<(A, B)>>>),
|
|
|
|
B(#[pin] B::Future),
|
2020-12-27 05:28:00 +01:00
|
|
|
Empty(PhantomData<Req>),
|
2020-07-22 05:32:13 +02:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, Req, B> Future for AndThenServiceResponseRC<A, Req, B>
|
2020-07-22 05:32:13 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: Service<Req>,
|
|
|
|
B: Service<A::Response, Error = A::Error>,
|
2020-07-22 05:32:13 +02:00
|
|
|
{
|
|
|
|
type Output = Result<B::Response, A::Error>;
|
|
|
|
|
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
let mut this = self.as_mut().project();
|
|
|
|
|
|
|
|
match this.state.as_mut().project() {
|
|
|
|
StateRCProj::A(fut, b) => match fut.poll(cx)? {
|
|
|
|
Poll::Ready(res) => {
|
|
|
|
let b = b.take().unwrap();
|
2020-12-27 05:28:00 +01:00
|
|
|
this.state.set(StateRC::Empty(PhantomData)); // drop fut A
|
2020-07-22 05:32:13 +02:00
|
|
|
let fut = b.borrow_mut().1.call(res);
|
|
|
|
this.state.set(StateRC::B(fut));
|
|
|
|
self.poll(cx)
|
|
|
|
}
|
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
},
|
|
|
|
StateRCProj::B(fut) => fut.poll(cx).map(|r| {
|
2020-12-27 05:28:00 +01:00
|
|
|
this.state.set(StateRC::Empty(PhantomData));
|
2020-07-22 05:32:13 +02:00
|
|
|
r
|
|
|
|
}),
|
2020-12-27 05:28:00 +01:00
|
|
|
StateRCProj::Empty(_) => {
|
2020-07-22 05:32:13 +02:00
|
|
|
panic!("future must not be polled after it returned `Poll::Ready`")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AndThenRCFuture - AndThen service based on RefCell
|
2020-02-26 08:45:23 +01:00
|
|
|
* and standard futures::future::and_then combinator in a Box
|
|
|
|
*/
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
struct AndThenRCFuture<A, Req, B>(Rc<RefCell<(A, B)>>, PhantomData<Req>);
|
2020-07-22 05:32:13 +02:00
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, Req, B> AndThenRCFuture<A, Req, B> {
|
2020-07-22 05:32:13 +02:00
|
|
|
fn new(a: A, b: B) -> Self
|
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: Service<Req>,
|
|
|
|
B: Service<A::Response, Error = A::Error>,
|
2020-07-22 05:32:13 +02:00
|
|
|
{
|
2020-12-27 05:28:00 +01:00
|
|
|
Self(Rc::new(RefCell::new((a, b))), PhantomData)
|
2020-07-22 05:32:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, Req, B> Clone for AndThenRCFuture<A, Req, B> {
|
2020-07-22 05:32:13 +02:00
|
|
|
fn clone(&self) -> Self {
|
2020-12-27 05:28:00 +01:00
|
|
|
Self(self.0.clone(), PhantomData)
|
2020-07-22 05:32:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, Req, B> Service<Req> for AndThenRCFuture<A, Req, B>
|
2020-07-22 05:32:13 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: Service<Req> + 'static,
|
2020-07-22 05:32:13 +02:00
|
|
|
A::Future: 'static,
|
2020-12-27 05:28:00 +01:00
|
|
|
B: Service<A::Response, Error = A::Error> + 'static,
|
2020-07-22 05:32:13 +02:00
|
|
|
B::Future: 'static,
|
|
|
|
{
|
|
|
|
type Response = B::Response;
|
|
|
|
type Error = A::Error;
|
|
|
|
type Future = BoxFuture<Self::Response, Self::Error>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
fn call(&mut self, req: Req) -> Self::Future {
|
2020-07-22 05:32:13 +02:00
|
|
|
let fut = self.0.borrow_mut().0.call(req);
|
|
|
|
let core = self.0.clone();
|
|
|
|
let fut2 = move |res| (*core).borrow_mut().1.call(res);
|
|
|
|
Box::pin(fut.and_then(fut2))
|
|
|
|
}
|
|
|
|
}
|
2020-02-26 08:45:23 +01:00
|
|
|
|
|
|
|
/// Criterion Benchmark for async Service
|
|
|
|
/// Should be used from within criterion group:
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// let mut criterion: ::criterion::Criterion<_> =
|
|
|
|
/// ::criterion::Criterion::default().configure_from_args();
|
|
|
|
/// bench_async_service(&mut criterion, ok_service(), "async_service_direct");
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Usable for benching Service wrappers:
|
|
|
|
/// Using minimum service code implementation we first measure
|
|
|
|
/// time to run minimum service, then measure time with wrapper.
|
|
|
|
///
|
|
|
|
/// Sample output
|
|
|
|
/// async_service_direct time: [1.0908 us 1.1656 us 1.2613 us]
|
|
|
|
pub fn bench_async_service<S>(c: &mut Criterion, srv: S, name: &str)
|
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
S: Service<(), Response = usize, Error = ()> + Clone + 'static,
|
2020-02-26 08:45:23 +01:00
|
|
|
{
|
|
|
|
let mut rt = actix_rt::System::new("test");
|
|
|
|
|
|
|
|
// start benchmark loops
|
|
|
|
c.bench_function(name, move |b| {
|
|
|
|
b.iter_custom(|iters| {
|
|
|
|
let mut srvs: Vec<_> = (1..iters).map(|_| srv.clone()).collect();
|
|
|
|
// exclude request generation, it appears it takes significant time vs call (3us vs 1us)
|
|
|
|
let start = std::time::Instant::now();
|
|
|
|
// benchmark body
|
2020-07-22 05:32:13 +02:00
|
|
|
rt.block_on(async move { join_all(srvs.iter_mut().map(|srv| srv.call(()))).await });
|
2020-02-26 08:45:23 +01:00
|
|
|
// check that at least first request succeeded
|
2020-08-07 10:16:45 +02:00
|
|
|
start.elapsed()
|
2020-02-26 08:45:23 +01:00
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn service_benches() {
|
|
|
|
let mut criterion: ::criterion::Criterion<_> =
|
|
|
|
::criterion::Criterion::default().configure_from_args();
|
2020-07-22 05:32:13 +02:00
|
|
|
bench_async_service(
|
|
|
|
&mut criterion,
|
|
|
|
AndThenUC::new(svc1.into_service(), svc2.into_service()),
|
|
|
|
"AndThen with UnsafeCell",
|
|
|
|
);
|
|
|
|
bench_async_service(
|
|
|
|
&mut criterion,
|
|
|
|
AndThenRC::new(svc1.into_service(), svc2.into_service()),
|
|
|
|
"AndThen with RefCell",
|
|
|
|
);
|
|
|
|
bench_async_service(
|
|
|
|
&mut criterion,
|
|
|
|
AndThenUC::new(svc1.into_service(), svc2.into_service()),
|
|
|
|
"AndThen with UnsafeCell",
|
|
|
|
);
|
|
|
|
bench_async_service(
|
|
|
|
&mut criterion,
|
|
|
|
AndThenRC::new(svc1.into_service(), svc2.into_service()),
|
|
|
|
"AndThen with RefCell",
|
|
|
|
);
|
|
|
|
bench_async_service(
|
|
|
|
&mut criterion,
|
|
|
|
AndThenRCFuture::new(svc1.into_service(), svc2.into_service()),
|
|
|
|
"AndThen with RefCell via future::and_then",
|
|
|
|
);
|
2020-02-26 08:45:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
criterion_main!(service_benches);
|