pub fn bench_async_service<S>(c: &mut Criterion, srv: S, name: &str)
where
    S: Service<ServiceRequest, Response = ServiceResponse, Error = Error> + 'static,
{
    let rt = actix_rt::System::new();
    let srv = Rc::new(RefCell::new(srv));

    let req = TestRequest::default().to_srv_request();
    assert!(rt
        .block_on(srv.borrow_mut().call(req))
        .unwrap()
        .status()
        .is_success());

    // start benchmark loops
    c.bench_function(name, move |b| {
        b.iter_custom(|iters| {
            let srv = srv.clone();
            // exclude request generation, it appears it takes significant time vs call (3us vs 1us)
            let futs = (0..iters)
                .map(|_| TestRequest::default().to_srv_request())
                .map(|req| srv.borrow_mut().call(req));

            let start = std::time::Instant::now();
            // benchmark body
            rt.block_on(async move {
                for fut in futs {
                    fut.await.unwrap();
                }
            });
            // check that at least first request succeeded
            start.elapsed()
        })
    });
}
/// Criterion Benchmark for async Service
/// Should be used from within criterion group:
/// ```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]


Usable for benching Service wrappers:
Using minimum service code implementation we first measure
time to run minimum service, then measure time with wrapper.


Usable for benching Service wrappers : Using minimum service code implementation we first measure time to run minimum service then measure time with wrapper .
pub fn bench_async_service<S>(c: &mut Criterion, srv: S, name: &str)
where
    S: Service<ServiceRequest, Response = ServiceResponse, Error = Error> + 'static,
{
    let rt = actix_rt::System::new();
    let srv = Rc::new(RefCell::new(srv));
    let req = TestRequest::default().to_srv_request();
    assert!(rt
        .block_on(srv.borrow_mut().call(req))
        .unwrap()
        .status()
        .is_success());
    // start benchmark loops
    c.bench_function(name, move |b| {
        b.iter_custom(|iters| {
            let srv = srv.clone();
            // exclude request generation, it appears it takes significant time vs call (3us vs 1us)
            let futs = (0..iters)
                .map(|_| TestRequest::default().to_srv_request())
                .map(|req| srv.borrow_mut().call(req));
            let start = std::time::Instant::now();
            // benchmark body
            rt.block_on(async move {
                for fut in futs {
                    fut.await.unwrap();
                }
            });
            // check that at least first request succeeded
            start.elapsed()
        })
    });
}
pub fn bench_async_service < S > ( c : & mut Criterion , srv : S , name : & str ) where S : Service < ServiceRequest , Response = ServiceResponse , Error = Error > + ' static , { let rt = actix_rt :: System :: new ( ) ; let srv = Rc :: new ( RefCell :: new ( srv ) ) ; let req = TestRequest :: default ( ) . to_srv_request ( ) ; assert ! ( rt block_on ( srv borrow_mut ( ) call ( req ) ) unwrap ( ) status ( ) is_success ( ) ) ; // start benchmark loops c . bench_function ( name , move | b | { b . iter_custom ( | iters | { let srv = srv . clone ( ) ; // exclude request generation, it appears it takes significant time vs call (3us vs 1us) let futs = ( 0 .. iters ) . map ( | _ | TestRequest :: default ( ) . to_srv_request ( ) ) . map ( | req | srv . borrow_mut ( ) . call ( req ) ) ; let start = std :: time :: Instant :: now ( ) ; // benchmark body rt . block_on ( async move { for fut in futs { fut . await . unwrap ( ) ; } } ) ; // check that at least first request succeeded start . elapsed ( ) } ) } ) ; }
Criterion Benchmark for async Service
Should be used from within criterion group:
```ignore
let mut criterion: ::criterion::Criterion<_>
::criterion::Criterion::default().configure_from_args();
bench_async_service(&mut criterion, ok_service(), "async_service_direct");
```
Criterion Benchmark for async Service Should be used from within criterion group : ` ` ` ignore let mut criterion : :: criterion :: Criterion<_ > :: criterion :: Criterion :: default () . configure_from_args () ; bench_async_service ( &mut criterion ok_service () " async_service_direct " ) ; ` ` `
c
&mut Criterion
srv
S
name
&str
c
&mut Criterion
srv
S
name
&str