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

how to combine async iters and iter_custom? #814

Open
deitch opened this issue Sep 19, 2024 · 0 comments
Open

how to combine async iters and iter_custom? #814

deitch opened this issue Sep 19, 2024 · 0 comments

Comments

@deitch
Copy link

deitch commented Sep 19, 2024

I am trying to start a server (setup), then run some calls to it, benchmarking the calls. Because some of these calls can cause writes (think of it like key-value store), I want each call to have a unique value. The easiest thing to do is to just use the iteration count as the key.

I tried iter_batched, but that has no way to pass the current iteration count:

            b.to_async(&rt).iter_batched(
                || {
                    // simple example of data
                    let data = vec![0u8; size].into_boxed_slice();
                    data
                },
                |input| async {
                    let put_tx = srv.put_tx();

                    let key = format!("key{}", count); // <--- THIS doesn't work; no way to get the current iteration count

                    // do work with key to send to server

                    let _ = put_tx.send(PutMsg {
                        key,
                        value: input,
                        resp_tx,
                    });

                    let result = resp_rx.recv().await;

                    black_box(result)
                },
                BatchSize::SmallInput,
            );

Next effort was to use a counter in setup:

            let mut iteration_count = 0;
            b.to_async(&rt).iter_batched(
                || {
                    iteration_count += 1;
                    // simple example of data
                    let data = vec![0u8; size].into_boxed_slice();
                    (data, iteration_count)
                },
                |(input, count)| async {
                    let put_tx = srv.put_tx();

                    let key = format!("key{}", count); // <--- THIS now has a value for `count`

                    // do work with key to send to server

                    let _ = put_tx.send(PutMsg {
                        key,
                        value: input,
                        resp_tx,
                    });

                    let result = resp_rx.recv().await;

                    black_box(result)
                },
                BatchSize::SmallInput,
            );

This doesn't work, because I am passing a mut into an async fn.

So I tried to switch to using iter_custom:

            b.to_async(&rt).iter_custom(|iters| async {
                // sample data
                let data = vec![0u8; size].into_boxed_slice();
                let start = Instant::now();
                for i in 0..iters {
                    let put_tx = srv.put_tx();
                    let key = format!("key{}", i);

                    let _ = put_tx.send(PutMsg {
                        key,
                        value: data,
                        resp_tx,
                    });

                    let result = resp_rx.recv().await;

                    black_box(result);
                }
                start.elapsed()
            });

Looks simple enough, but this also fails, because the async fn borrows iters, and the async may outlive the calling function.

But I cannot declare it move, because it doesn't like the move of srv, or data, or a whole bunch of things., like the srv, which cannot be moved. Actually, that is the whole reason for this approach, to have the srv set up before.

So, what is the correct approach to this? How do I call iter_custom on AsyncBencher and actually reference the iters inside the function?

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

No branches or pull requests

1 participant