-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core/budget): time-based budget
- Loading branch information
Showing
13 changed files
with
337 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::{env, str::FromStr, thread::available_parallelism}; | ||
|
||
use tokio::runtime::{Builder, Runtime}; | ||
|
||
#[allow(dead_code)] | ||
pub(crate) fn tokio_worker_threads() -> u32 { | ||
env::var("TOKIO_WORKER_THREADS") | ||
.ok() | ||
.map(|s| u32::from_str(&s).expect("invalid value for TOKIO_WORKER_THREADS")) | ||
.unwrap_or_else(|| { | ||
usize::from(available_parallelism().expect("cannot get available parallelism")) as u32 | ||
}) | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub(crate) fn max_parallelism() -> u32 { | ||
env::var("ELFO_BENCH_MAX_PARALLELISM") | ||
.ok() | ||
.map(|s| u32::from_str(&s).expect("invalid value for ELFO_BENCH_MAX_PARALLELISM")) | ||
.unwrap_or_else(|| { | ||
usize::from(available_parallelism().expect("cannot get available parallelism")) as u32 | ||
}) | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub(crate) fn make_mt_runtime(worker_threads: u32) -> Runtime { | ||
use std::sync::atomic::{AtomicU32, Ordering}; | ||
|
||
// To make it easier to check in the profiler. | ||
let test_no = { | ||
static BENCH_NO: AtomicU32 = AtomicU32::new(0); | ||
BENCH_NO.fetch_add(1, Ordering::Relaxed) | ||
}; | ||
|
||
let next_worker_no = AtomicU32::new(0); | ||
|
||
Builder::new_multi_thread() | ||
.enable_all() | ||
.worker_threads(worker_threads as usize) | ||
.thread_name_fn(move || { | ||
let worker_no = next_worker_no.fetch_add(1, Ordering::Relaxed); | ||
format!("b{test_no}-w{worker_no}") | ||
}) | ||
.build() | ||
.unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use std::time::{Duration, Instant}; | ||
|
||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
|
||
use elfo::{config::AnyConfig, prelude::*, topology::Topology}; | ||
|
||
mod common; | ||
|
||
#[message(ret = Duration)] | ||
struct Summarize; | ||
|
||
fn make_yielder(iter_count: u32) -> Blueprint { | ||
ActorGroup::new().exec(move |mut ctx| async move { | ||
let token = msg!(match ctx.recv().await.unwrap() { | ||
(Summarize, token) => token, | ||
_ => unreachable!(), | ||
}); | ||
|
||
let start = Instant::now(); | ||
for _ in 0..iter_count { | ||
elfo::coop::consume_budget().await; | ||
} | ||
|
||
ctx.respond(token, start.elapsed()); | ||
}) | ||
} | ||
|
||
async fn run(iter_count: u32) -> Duration { | ||
let topology = Topology::empty(); | ||
let yielder = topology.local("yielder"); | ||
let configurers = topology.local("system.configurers").entrypoint(); | ||
|
||
let yielder_addr = yielder.addr(); | ||
|
||
yielder.mount(make_yielder(iter_count)); | ||
configurers.mount(elfo::batteries::configurer::fixture( | ||
&topology, | ||
AnyConfig::default(), | ||
)); | ||
|
||
elfo::_priv::do_start(topology, false, |ctx, _| async move { | ||
ctx.request_to(yielder_addr, Summarize) | ||
.resolve() | ||
.await | ||
.unwrap() | ||
}) | ||
.await | ||
.unwrap() | ||
} | ||
|
||
fn by_count(c: &mut Criterion) { | ||
c.bench_function("count", |b| { | ||
b.iter_custom(|iter_count| { | ||
let rt = common::make_mt_runtime(common::tokio_worker_threads()); | ||
rt.block_on(run(iter_count as u32)) | ||
}) | ||
}); | ||
} | ||
|
||
criterion_group!(cases, by_count); | ||
criterion_main!(cases); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.