-
Notifications
You must be signed in to change notification settings - Fork 340
Description
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?