Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncBencher::iter_batched() setup closure to return a Future #576

Open
arnauorriols opened this issue May 1, 2022 · 2 comments · May be fixed by #656
Open

AsyncBencher::iter_batched() setup closure to return a Future #576

arnauorriols opened this issue May 1, 2022 · 2 comments · May be fixed by #656

Comments

@arnauorriols
Copy link

Just like the Routine, the Setup is also likely to be async, when benching async code. Is there any particular reason why the setup is not async?

@davidblewett
Copy link

davidblewett commented Nov 2, 2022

@arnauorriols the result of setup() is passed directly to routine, which simplifies the type signature:

    pub fn iter_batched<I, O, S, R, F>(&mut self, mut setup: S, mut routine: R, size: BatchSize)
    where
        S: FnMut() -> I,
        R: FnMut(I) -> F,
        F: Future<Output = O>,

I'm guessing to allow setup to be async would require yet another iter_batched_async_setup method:

    pub fn iter_batched_async_setup<I, O, S, R, F>(&mut self, mut setup: S, mut routine: R, size: BatchSize)
    where
        S: FnMut() -> Future<Output = I>,
        R: FnMut(I) -> F,
        F: Future<Output = O>,

Probably easier to spin up your own runtime in setup, do what you need and return the value directly.

@howardjohn
Copy link

Its a bit tricky to spawn the runtime since you can't make a new one (in Tokio at least), but this seems to work:

   c.bench_function("async setup", |b| {
        b.to_async(Runtime::new().unwrap()).iter_batched(
            || {
                let (mut tx, rx) = std::sync::mpsc::channel();
                let s = Handle::current().spawn(async move {
                    let s = TcpStream::connect(addr).await.unwrap();
                    tx.send(s).unwrap();
                });
                rx.recv().unwrap()
            },
            |r| {
                async move {
                    println!("call {a}: {}", r.local_addr().unwrap());
                }
            },
            BatchSize::PerIteration,
        )
    });

@samtay samtay linked a pull request Feb 16, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants