Trouble Benchmarking actix_web api with Criterion

437 Views Asked by At

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!

2

There are 2 best solutions below

3
Ibraheem Ahmed On BEST ANSWER

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())
        }
    }
0
Marcus Ruddick On

There doesn't seem like a good way to do this. There are no stable actix versions supporting actix_rt 2.0.x (though it would fix the mutability conflicts in the AsyncExecutor). As of right now actix-web 4.0.x is in beta and too unstable for my existing application (causes all sorts of issues when used). For now I will wait until actix-web 4.0.0 is released and stable to implement benchmarks for my api.