Separated simple benchmark

This commit is contained in:
2026-01-29 10:38:07 +01:00
parent 059f8f07c1
commit 74c4a976dd
4 changed files with 106 additions and 3 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ use std::sync::Arc;
use osui::prelude::*;
pub fn main() {
let engine = Benchmark::new(Console::new());
let engine = SimpleBenchmark::new(Console::new());
engine.run(app);
}
+15
View File
@@ -0,0 +1,15 @@
use std::sync::Arc;
use osui::prelude::*;
pub fn main() {
let engine = SimpleBenchmark::new(Console::new());
engine.run(app);
}
fn app(cx: &Arc<Context>) -> View {
rsx! {
"Hello, world!"
}
.view(cx.clone())
}
+2 -2
View File
@@ -1,8 +1,8 @@
pub mod benchmark;
pub mod simple_benchmark;
pub mod commands;
pub mod console;
pub use benchmark::*;
pub use simple_benchmark::*;
pub use console::*;
use std::{any::Any, sync::Arc};
+88
View File
@@ -0,0 +1,88 @@
use std::{io::stdout, sync::Arc, time::Instant};
use crossterm::{cursor::MoveTo, execute, terminal::Clear};
use crate::{prelude::Context, render::Area, DrawContext, View};
use super::Engine;
pub struct SimpleBenchmark<T: Engine>(T);
impl<T: Engine> SimpleBenchmark<T> {
pub fn new(engine: T) -> Self {
Self(engine)
}
}
impl<T: Engine> Engine for SimpleBenchmark<T> {
fn run<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(&self, component: F) {
let mut times: Vec<u64> = Vec::new();
let cx = self.init(component);
let start = Instant::now();
for _ in 0..100 {
let start = Instant::now();
self.render(&cx);
let end = Instant::now();
times.push(end.duration_since(start).as_nanos() as u64);
self.0.render_delay();
}
let end = Instant::now();
execute!(stdout(), Clear(crossterm::terminal::ClearType::Purge)).unwrap();
execute!(stdout(), Clear(crossterm::terminal::ClearType::All)).unwrap();
execute!(stdout(), MoveTo(0, 0)).unwrap();
println!(
"Average time: {} nanoseconds",
if times.len() > 0 {
times.iter().sum::<u64>() / (times.len() as u64)
} else {
0
}
);
println!("Min time: {} nanoseconds", times.iter().min().unwrap_or(&0));
println!("Max time: {} nanoseconds", times.iter().max().unwrap_or(&0));
println!(
"Median time: {} nanoseconds",
times.iter().nth(times.len() / 2).unwrap_or(&0)
);
println!(
"Total time: {} nanoseconds",
end.duration_since(start).as_nanos()
);
println!(
"Total render time: {} nanoseconds",
times.iter().sum::<u64>()
);
}
fn init<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
&self,
component: F,
) -> Arc<Context> {
self.0.init(component)
}
fn draw_context(&self, ctx: &DrawContext) {
self.0.draw_context(ctx)
}
fn render(&self, cx: &Arc<Context>) {
self.0.render(cx)
}
fn render_view(&self, area: &Area, view: &View) -> DrawContext {
self.0.render_view(area, view)
}
fn render_delay(&self) {
self.0.render_delay();
}
fn executor(&self) -> Arc<dyn super::CommandExecutor> {
self.0.executor()
}
}