1

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!

Marcus Ruddick
  • 9,795
  • 7
  • 28
  • 43

2 Answers2

0

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())
        }
    }
Ibraheem Ahmed
  • 11,652
  • 2
  • 48
  • 54
  • axtix_rt 2.0 fixes the integration with Criterion but it apparently is not compatible with actix-web 3.3.2 (or any stable actix-web version as of this time) – Marcus Ruddick Feb 21 '21 at 17:10
  • If upgrading to v4.0-beta is causing dependency conflicts, you can [pin you dependencies](https://github.com/actix/actix-web/issues/1944) – Ibraheem Ahmed Feb 21 '21 at 18:24
  • I am probably going to wait for 4.x to be stable, Attempting to use RefCell doesn't work due to lifetime mismatch (compiler want's the future to have a static lifetime). But thank you for your help! – Marcus Ruddick Feb 21 '21 at 19:31
0

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.

Marcus Ruddick
  • 9,795
  • 7
  • 28
  • 43