use std::{
cell::RefCell,
collections::VecDeque,
rc::Rc,
sync::{
Arc,
atomic::{AtomicUsize, Ordering},
},
thread::ThreadId,
time::{Duration, Instant},
};
use crate::{
AnyWindowHandle, App, AppCell, AppContext, BackgroundExecutor, Context, Empty, Entity,
ForegroundExecutor, PlatformDispatcher, Priority, Render, RunnableVariant, Task, TestPlatform,
ThreadTaskTimings, Window, WindowOptions, platform::priority_pool::ThreadPool,
};
use parking_lot::{Condvar, Mutex};
struct BenchmarkDispatcher {
thread: ThreadId,
foreground: Mutex<VecDeque<RunnableVariant>>,
ready: Condvar,
background: Arc<AtomicUsize>,
workers: ThreadPool<Priority>,
}
impl BenchmarkDispatcher {
fn new() -> Self {
Self {
thread: std::thread::current().id(),
foreground: Mutex::new(VecDeque::new()),
ready: Condvar::new(),
background: Arc::new(AtomicUsize::new(0)),
workers: ThreadPool::new(num_cpus::get().max(1)),
}
}
fn drain(&self) {
let deadline = Instant::now() + Duration::from_secs(30);
loop {
let runnable = self.foreground.lock().pop_front();
if let Some(runnable) = runnable {
runnable.run();
continue;
}
let mut queue = self.foreground.lock();
if queue.is_empty() && self.background.load(Ordering::Acquire) == 0 {
return;
}
assert!(
Instant::now() < deadline,
"benchmark background work did not settle within 30 seconds"
);
self.ready.wait_for(&mut queue, Duration::from_millis(1));
}
}
}
impl PlatformDispatcher for BenchmarkDispatcher {
fn get_all_timings(&self) -> Vec<ThreadTaskTimings> {
ThreadTaskTimings::convert(&crate::GLOBAL_THREAD_TIMINGS.lock())
}
fn get_current_thread_timings(&self) -> Vec<crate::TaskTiming> {
crate::THREAD_TIMINGS.with(|timings| timings.lock().timings.iter().cloned().collect())
}
fn is_main_thread(&self) -> bool {
std::thread::current().id() == self.thread
}
fn dispatch(&self, runnable: RunnableVariant, priority: Priority) {
self.background.fetch_add(1, Ordering::Release);
let background = self.background.clone();
self.workers.queue_job(&priority, move || {
let completed = util::defer(move || {
background.fetch_sub(1, Ordering::Release);
});
runnable.run();
drop(completed);
});
}
fn dispatch_on_main_thread(&self, runnable: RunnableVariant, _priority: Priority) {
self.foreground.lock().push_back(runnable);
self.ready.notify_one();
}
fn dispatch_after(&self, duration: Duration, runnable: RunnableVariant) {
self.workers
.queue_delayed(&Priority::Low, duration, runnable);
}
fn spawn_realtime(&self, callback: Box<dyn FnOnce() + Send>) {
std::thread::spawn(callback);
}
fn wakeup(&self) {
self.ready.notify_one();
}
}
pub struct BenchAppContext<'context, 'bencher> {
app: Rc<AppCell>,
background: BackgroundExecutor,
foreground: ForegroundExecutor,
dispatcher: Arc<BenchmarkDispatcher>,
bencher: Option<&'context mut criterion::Bencher<'bencher>>,
}
impl<'context, 'bencher> BenchAppContext<'context, 'bencher> {
pub fn new(bencher: &'context mut criterion::Bencher<'bencher>) -> Self {
let dispatcher = Arc::new(BenchmarkDispatcher::new());
let background = BackgroundExecutor::new(dispatcher.clone());
let foreground = ForegroundExecutor::new(dispatcher.clone());
let platform = TestPlatform::with_platform(
background.clone(),
foreground.clone(),
crate::platform::text_system::new(),
Some(Box::new(crate::current_headless_renderer)),
);
let app = App::new_app(
platform,
Arc::new(()),
http_client::FakeHttpClient::with_404_response(),
);
Self {
app,
background,
foreground,
dispatcher,
bencher: Some(bencher),
}
}
pub fn update<R>(&mut self, callback: impl FnOnce(&mut App) -> R) -> R {
self.app.borrow_mut().update(callback)
}
pub fn read<R>(&self, callback: impl FnOnce(&App) -> R) -> R {
callback(&self.app.borrow())
}
pub fn background_executor(&self) -> &BackgroundExecutor {
&self.background
}
pub fn foreground_executor(&self) -> &ForegroundExecutor {
&self.foreground
}
pub fn run_until_idle(&self) {
self.dispatcher.drain();
self.draw_pending_frames();
self.dispatcher.drain();
}
pub fn settle(&mut self) {
self.run_until_idle();
}
pub fn add_empty_window(&mut self) -> BenchWindowContext {
let window = self
.update(|cx| cx.open_window(WindowOptions::default(), |_, cx| cx.new(|_| Empty)))
.expect("benchmark window must open");
BenchWindowContext {
app: self.app.clone(),
window: window.into(),
}
}
fn draw_pending_frames(&self) {
let handles = self.app.borrow().windows();
for handle in handles {
self.app
.borrow_mut()
.update_window(handle, |_, window, cx| window.benchmark_frame(cx))
.expect("benchmark window must remain open while drawing");
}
}
pub fn bench_iter(&mut self, mut benchmark: impl FnMut(&mut Self)) {
self.run_until_idle();
let bencher = self
.bencher
.take()
.expect("a benchmark measurement cannot be nested");
bencher.iter(|| {
benchmark(self);
self.run_until_idle();
});
self.bencher = Some(bencher);
}
pub fn bench_task<Output: 'static>(
&mut self,
mut benchmark: impl FnMut(&mut Self) -> Task<Output>,
) {
self.bench_iter(|cx| {
let output = Rc::new(RefCell::new(None));
let task = benchmark(cx);
cx.foreground
.spawn({
let output = output.clone();
async move {
*output.borrow_mut() = Some(task.await);
}
})
.detach();
let deadline = Instant::now() + Duration::from_secs(30);
while output.borrow().is_none() {
cx.run_until_idle();
assert!(
Instant::now() < deadline,
"benchmark task did not complete within 30 seconds"
);
std::thread::yield_now();
}
std::hint::black_box(output.borrow_mut().take());
});
}
pub fn bench_renderer<V: Render + 'static>(
&mut self,
view: Entity<V>,
mut update: impl FnMut(&mut V, &mut Window, &mut Context<V>),
) {
self.run_until_idle();
let entity_id = view.entity_id();
let handles = self.app.borrow().windows();
let handle = handles
.into_iter()
.find(|handle| {
self.app
.borrow_mut()
.update_window(*handle, |root, window, _| {
root.entity_id() == entity_id
|| window.rendered_frame.dispatch_tree.contains_view(entity_id)
})
.expect("benchmark window must remain open")
})
.expect("render benchmark entity must be attached to a window");
self.bench_iter(|cx| {
cx.app
.borrow_mut()
.update_window(handle, |_, window, cx| {
view.update(cx, |view, cx| update(view, window, cx));
})
.expect("render benchmark window must remain open");
});
}
pub fn teardown(mut self) {
self.update(App::shutdown);
self.dispatcher.drain();
}
}
#[derive(Clone)]
pub struct BenchWindowContext {
app: Rc<AppCell>,
window: AnyWindowHandle,
}
impl BenchWindowContext {
pub fn window_handle(&self) -> AnyWindowHandle {
self.window
}
pub fn update<R>(&mut self, callback: impl FnOnce(&mut Window, &mut App) -> R) -> R {
self.app
.borrow_mut()
.update_window(self.window, |_, window, cx| callback(window, cx))
.expect("benchmark window must remain open during setup")
}
}