Search code examples
rustasync-awaitrust-tokioactix-webrust-criterion

Trouble Benchmarking actix_web api with Criterion


I have been trying to add benchmarking using the Criterion crate to my actix_web application. I have been struggling to get it to work because the AsyncExecutor trait is not implemented for tokio 0.2.x. I tried implementing the trait for the actix_rt runtime but ran into issues there as well, code below

impl AsyncExecutor for ActixRuntime {
    fn block_on<T>(&self, future: impl Future<Output=T>) -> T {
        self.rt.block_on(future)
    }
}

pub struct ActixRuntime {
    rt: actix_rt::Runtime,
}

impl ActixRuntime {
    pub fn new() -> Self {
        ActixRuntime {
            rt: actix_rt::Runtime::new().unwrap(),
        }
    }
}

this errors out because the block_on function for actix_rt (and tokio 0.2.x) is has a signature of block_on(&mut self, ...) -> ... { ... } So I cannot implement the trait since the trait has an immutable reference.

Before I dig myself further into trying to make this work I want to ask if what I am attempting is possible. Is there a way to use Criterion with actix? Or is this impossible as of now? If it is not possible are there any other frameworks available or should I look at solutions outside the rust toolchain?

Thanks for any advice or help, links and examples welcome!

Cheers!


Solution

  • actix-rt v2's block_on method takes &self now after upgrading to tokio 1.0. Upgrading should fix your issue:

    // pin your deps: https://github.com/actix/actix-web/issues/1944
    actix-rt = "=2.0.0-beta.2"
    actix-web = "4.0.0-beta.1"
    actix-service = "=2.0.0-beta.3"
    

    If upgrading is not an option, you can use an interior mutability wrapper, such as RefCell:

    impl AsyncExecutor for ActixRuntime {
        fn block_on<T>(&self, future: impl Future<Output=T>) -> T {
            self.rt.borrow_mut().block_on(future)
        }
    }
    
    pub struct ActixRuntime {
        rt: RefCell<actix_rt::Runtime>,
    }
    
    impl ActixRuntime {
        pub fn new() -> Self {
            ActixRuntime {
                rt: RefCell::new(actix_rt::Runtime::new().unwrap())
            }
        }